首页 > 代码库 > 多线程1

多线程1

1 判断单线程:在程序中如果能用一条线穿过所有调用方法,就是单线程

package lianxi1;class SubThread extends Thread{    public void run(){    for(int i=1;i<=50;i++){        try {            Thread.currentThread().sleep(1000);  //使当前线程暂停,不丢失控制权        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getPriority()+" "+i);    }    }}public class TestThread {    public static void main(String[] args) {        SubThread sub = new SubThread();        sub.setPriority(Thread.MIN_PRIORITY);        sub.start();        for(int i=1;i<=50;i++){            System.out.println(Thread.currentThread().getName()+" "+Thread.currentThread().getPriority()+" "+i);//            if(i%5==0){//                Thread.currentThread().yield();  // yield方法暂停当前执行的线程,转到其他线程//            }            if(i%10==0){                try {                    sub.join();  //等待该线程终止                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }        System.out.println(sub.isAlive());    }}

多线程1