黑马程序员---多线程join()方法

来源:互联网 发布:java怎么查询数据库 编辑:程序博客网 时间:2024/05/16 04:51

------- android培训java培训、期待与您交流! ----------

 

自从接触Java多线程,一直对Join理解不了,Join()到底是干啥用的? 啥也不说,看代码:

public class JoinTest {    public static void main(String[] args) {        Thread t = new Thread(new RunnableImpl());        t.start();        try {            t.join(1000);            System.out.println("joinFinish");        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();             }    }}class RunnableImpl implements Runnable {    @Override    public void run() {        try {            System.out.println("Begin sleep");            Thread.sleep(1000);           System.out.println("End sleep");        } catch (InterruptedException e) {            e.printStackTrace();        }    }}结果是:Begin sleepEnd sleepjoinFinish


明白了,当main线程调用t.join时,main线程等待t线程,等待时间是1000,如果t线程Sleep 2000呢 ?

 public void run() {        try {            System.out.println("Begin sleep");            // Thread.sleep(1000);            Thread.sleep(2000);           System.out.println("End sleep");        } catch (InterruptedException e) {            e.printStackTrace();        }    }结果是:Begin sleepjoinFinishEnd sleep



也就是说main线程只等1000毫秒,不管T什么时候结束。

其实Join方法实现是通过wait(小提示:Object 提供的方法)。 当main线程调用t.join时候,main线程会获得线程对象t的锁(wait 意味着拿到该对象的锁),调用该对象的wait(等待时间),直到该对象唤醒main线程,比如退出后。

这就意味着main 线程调用t.join时,必须能够拿到线程t对象的锁,如果拿不到它是无法wait的,刚开的例子t.join(1000)不是说明了main线程等待1秒,如果在它等待之前,其他线程获取了t对象的锁,它等待时间可不就是1毫秒了。

 

 

 

 


另一段代码:

public class JoinTest {    public static void main(String[] args) {        Thread t = new Thread(new RunnableImpl());       new ThreadTest(t).start();        t.start();        try {            t.join();            System.out.println("joinFinish");        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();             }    }}class ThreadTest extends Thread {    Thread thread;    public ThreadTest(Thread thread) {        this.thread = thread;    }    @Override    public void run() {        holdThreadLock();    }    public void holdThreadLock() {        synchronized (thread) {            System.out.println("getObjectLock");            try {                Thread.sleep(9000);            } catch (InterruptedException ex) {             ex.printStackTrace();            }            System.out.println("ReleaseObjectLock");        }    }}class RunnableImpl implements Runnable {    @Override    public void run() {        try {            System.out.println("Begin sleep");            Thread.sleep(2000);           System.out.println("End sleep");        } catch (InterruptedException e) {            e.printStackTrace();        }    }}运行结果是:getObjectLockBegin sleepEnd sleepReleaseObjectLockjoinFinish


在main方法中 通过new ThreadTest(t).start();实例化ThreadTest 线程对象, 通过 synchronized (thread),获取线程对象t的锁,并Sleep(9000)后释放,这就意味着,即使main方法t.join(1000),等待一秒钟,它必须等待ThreadTest 线程释放t锁后才能进入wait方法中,它实际等待时间是9000+1000 MS

 

 

 

 

 


 

------- android培训java培训、期待与您交流! ----------  详细请查看:http://edu.csdn.net/heima/

原创粉丝点击