Java基础多线程之join抢夺CPU执行权直到该线程结束。

class JoinThreadDemo
{
    public static void main(String[] args)
    {
        JoinThread joinThread = new JoinThread();
        
        Thread t1 = new Thread(joinThread,"t1");
        Thread t2 = new Thread(joinThread,"t2");
        
        t1.start();
        
        try
        {
            t1.join();
        }
        catch(Exception e)
        {
            System.out.println(Thread.currentThread().getName()+" Exception");
        }
        
        t2.start();
        
        for(int x = 0;x<80;x++)
        {
            System.out.println(Thread.currentThread().getName() + " run:"+x);
        }
        
        System.out.println("over");
    }
}

class JoinThread implements Runnable
{
    public void run()
    {
        for(int x = 0;x<70;x++)
        {
            System.out.println(Thread.currentThread().getName()+" run:"+x);
        }
    }
}
posted @ 2013-01-15 14:10  陈晓明  阅读(455)  评论(0编辑  收藏  举报