sleep()和 wait()有什么区别

来源:互联网 发布:淘宝返利网什么意思 编辑:程序博客网 时间:2024/06/05 23:52
package work.action.workflow;import java.util.Arrays;import java.util.Date;public class TestJava extends Date{    TestJava(){}    /**    * @paramargs    */    public static void main(String[] args) {        new Thread(new Thread1()).start();        try {            System.out.println("mainTime:"+new Date());            Thread.sleep(5000);            System.out.println("mainTime:"+new Date()+"main");        } catch (InterruptedException e) {        e.printStackTrace();        }        new Thread(new Thread2()).start();    }    private static class Thread1 implements Runnable    {        @Override        public void run() {            //由于这里的Thread1和下面的Thread2内部run方法要用同一对象作为监视器,我们这里不能用this,因为在Thread2里面的this和这个Thread1的this不是同一个对象。我们用TestJava.class这个字节码对象,当前虚拟机里引用这个变量时,指向的都是同一个对象。            synchronized (TestJava.class){                System.out.println("thread1Time:"+new Date());                System.out.println("enter thread1...");                System.out.println("thread1 is waiting");                try {                    //释放锁有两种方式,第一种方式是程序自然离开监视器的范围,也就是离开了synchronized关键字管辖的代码范围,另一种方式就是在synchronized关键字管辖的代码内部调用监视器对象的wait方法。这里,使用wait方法释放锁。                    TestJava.class.wait();                } catch(InterruptedException e) {                    e.printStackTrace();                }                System.out.println("thread1Time:"+new Date());                System.out.println("thread1 is going on...");                System.out.println("thread1 is being over!");            }        }    }    private static class Thread2 implements Runnable    {        @Override        public void run() {            synchronized (TestJava.class){                System.out.println("thread2Time:"+new Date());                System.out.println("enter thread2...");                System.out.println("thread2 notify other thread can release wait status..");                //由于notify方法并不释放锁,即使thread2调用下面的sleep方法休息了10毫秒,但thread1仍然不会执行,因为thread2没有释放锁,所以Thread1无法得不到锁。                TestJava.class.notify();                System.out.println("thread2 is sleeping ten millisecond...");                try {                    Thread.sleep(5000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                System.out.println("thread2Time:"+new Date());                System.out.println("thread2 is going on...");                System.out.println("thread2 is being over!");            }        }    }}

运行结果
mainTime:Thu Nov 05 17:26:39 CST 2015
thread1Time:Thu Nov 05 17:26:39 CST 2015
enter thread1…
thread1 is waiting
mainTime:Thu Nov 05 17:26:44 CST 2015main
thread2Time:Thu Nov 05 17:26:44 CST 2015
enter thread2…
thread2 notify other thread can release wait status..
thread2 is sleeping ten millisecond…
thread2Time:Thu Nov 05 17:26:49 CST 2015
thread2 is going on…
thread2 is being over!
thread1Time:Thu Nov 05 17:26:49 CST 2015
thread1 is going on…
thread1 is being over!

0 0
原创粉丝点击