博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

多线程一例

Posted on 2012-08-09 17:33  紫冰龙  阅读(130)  评论(0编辑  收藏  举报
public class TestThread implements Runnable{

    public TestThread() {
    }

    public static void main(String[] args) {
        Thread[] t = new Thread[10];
        TestThread r = new TestThread();
        for(int i=0;i<10;i++) {
            t[i] = new Thread(r);
            t[i].start();
        }
        Boolean over = false;
        while (!over){
            over = true;
            for(int i=0;i<10;i++) {
                if (t[i].isAlive()) over = false;
                break;
            }
            
        }
        System.out.println("ok");
    }

    @Override
    public void run() {
        for(int i=0;i<=10;i++) {
            System.out.println(Thread.currentThread().getName()+" "+i);
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}