Java基础—续多线程

来源:互联网 发布:rarzip解压软件下载 编辑:程序博客网 时间:2024/06/05 02:33
        

Java多线程知识很重要,并且这部分知识,再次博客中不在讲,其中网上有很多系列文章。

 

 推荐Java多线程系列文章:

 http://blog.csdn.net/column/details/wangyuetingtao.html

  

 其中上述系列文章中,从初步介绍多线程理论以及相应的实践,个人推荐。

 上述系列文章中最后一篇讲的是sleepwait的区别,那这篇继续讲解wait,既然了解了sleep和wait区别,但是何时唤醒wait呢?

  

 测试代码如下:

package com.multiThread;public class MultiThread {        public static void main(String[] args) {                new Thread(new Thread1()).start();                try{                        Thread.sleep(10);                }catch(Exception e){                        e.printStackTrace();                }                                new Thread(new Thread2()).start();        }                private static class Thread1 implements Runnable{                public void run() {                        // TODO Auto-generated method stub                        synchronized (MultiThread.class){                                System.out.println("enter thread1...");                                System.out.println("thread1 is waiting...");                                                                try{                                        MultiThread.class.wait();                                }catch(Exception e){                                        e.printStackTrace();                                }                                System.out.println("thread1 is going on...");                                System.out.println("thread1 is being over...");                                                        }                }        }                private static class Thread2 implements Runnable{                public void run() {                        // TODO Auto-generated method stub                        synchronized (MultiThread.class){                                System.out.println("enter thread2...");                                System.out.println("thread2 notify other thread ");                            MultiThread.class.notify();                            System.out.println("thread2 is sleeping ten millionscond...");                                                        try{                                Thread.sleep(10);                            }catch(Exception e){                                e.printStackTrace();                            }                                                        System.out.println("thread2 is going on...");                            System.out.println("thread2 is being over..");                                                    }                }        }}

   其中上述代码中,用到了实现多线程的方式,实现Runnable而不是使用Thread,至于原因,请细看上述系列文章。

   以及代码中再次区别了sleep和wait。

   虽然wait失去了锁,放在了阻塞队列。但是何时被唤醒呢,何时继续执行线程呢?

  

   根据上述代码,可以猜出运行结果吗?

   

   运行结果如下:

enter thread1...thread1 is waiting...enter thread2...thread2 notify other thread thread2 is sleeping ten millionscond...thread2 is going on...thread2 is being over..thread1 is going on...thread1 is being over...
 

分析如下:

   即使:thread2拥有对象的锁,当执行notify时,会唤醒阻塞队列的线程,即Thread1.但是,请注意:notify后,Thread1并没有拥有对象锁。而是Thread2继续执行。说明notify唤醒线程后,并不会放弃本身的锁。而是等notify执行完毕后,唤醒的阻塞队列中的线程才可以获得锁,继续执行。

 

  多线程知识应用很多方面,正如本博客中socket中就用到了匿名线程。同时java基础务必夯实。


原创粉丝点击