Java中join和yield的作用

1、 A.join,在API中的解释是,堵塞当前线程B,直到A执行完毕并死掉,再执行B。
用一个小例子来说明吧

static class ThreadA extends Thread {  
        @Override  
        public void run() {  
            // TODO Auto-generated method stub  
            super.run();  
            for (int i = 0; i < 10; i++) {  
                System.out.println("ThreadA" + i);  
            }  
        }  
    }  

    static class ThreadB extends Thread {  
        ThreadA a;  

        public ThreadB(ThreadA a) {  
            // TODO Auto-generated constructor stub  
            this.a = a;  
        }  

        @Override  
        public void run() {  
            // TODO Auto-generated method stub  
            super.run();  
            System.out.println("ThreadB start");  
            try {  
                a.join();  
            } catch (InterruptedException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            System.out.println("ThreadB end");  
        }  
    }  

    public static void main(String[] args) {  
        ThreadA a = new ThreadA();  
        ThreadB b = new ThreadB(a);  
        b.start();  
        a.start();  
    }  

执行结果:

ThreadB start  
ThreadA0  
ThreadA1  
ThreadA2  
ThreadA3  
ThreadA4  
ThreadA5  
ThreadA6  
ThreadA7  
ThreadA8  
ThreadA9  
ThreadB end  

首先b线程执行,a线程join后,直接执行完a,然后才执行b,证实上述说法。

2、A.yield,A让出位置,给B执行,B执行结束A再执行。跟join意思正好相反!

static class ThreadA extends Thread {  
    @Override  
    public void run() {  
        // TODO Auto-generated method stub  
        super.run();  
        for (int i = 0; i < 10; i++) {  
            System.out.println("ThreadA " + i);  
        }  
    }  
}  

static class ThreadB extends Thread {  
    ThreadA a;  

    public ThreadB(ThreadA a) {  
        // TODO Auto-generated constructor stub  
        this.a = a;  
    }  

    @Override  
    public void run() {  
        // TODO Auto-generated method stub  
        super.run();  
        System.out.println("ThreadB start");  
        try {  
            for (int i = 0; i < 10; i++) {  
                if(i==2){  
                    a.yield();  
                }  
                System.out.println("ThreadB " + i);  
            }  
        } catch (Exception e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
        System.out.println("ThreadB end");  
    }  
}  

public static void main(String[] args) {  
    ThreadA a = new ThreadA();  
    ThreadB b = new ThreadB(a);  
    b.start();  
    a.start();  
}  

执行结果:

ThreadB start  
ThreadA 0  
ThreadB 0  
ThreadA 1  
ThreadB 1  
ThreadA 2  
ThreadB 2  
ThreadB 3  
ThreadB 4  
ThreadB 5  
ThreadB 6  
ThreadB 7  
ThreadB 8  
ThreadB 9  
ThreadB end  
ThreadA 3  
ThreadA 4  
ThreadA 5  
ThreadA 6  
ThreadA 7  
ThreadA 8  
ThreadA 9  

首先B执行,然后A执行;在B的循环中,i=2时,A执行yield;接着B执行完,才轮到A执行。

本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值