import java.io.*;public class MethodTest{
    static class FirstThread extends Thread {
        public void run(){
        try {
            System.out.println("First thread starts running.");
            sleep(10000);
            System.out.println("First thread finishes running.");
        }catch(InterruptedException e){
        System.err.println("Error in thread: "+e);
        }
    }
}static class SecondRunnable implements Runnable{
    public synchronized void run(){
    try{
        System.out.println("Second thread starts running.");
        System.out.println("Second thread suspends itself.");
        wait();
        System.out.println("Second thread runs again and finishes.");
    }catch(InterruptedException e){
        System.err.println("Error in thread: "+e);
        }
    }
}public static void main(String args[]){
    Thread first=new FirstThread();
    Runnable secondRunnable=new SecondRunnable();
    Thread second=new Thread(secondRunnable);
    first.start();
    second.start();
    try{
        System.out.println("Waiting for first thread to finish...");
        first.join();
        System.out.println("Waking up second thread...");
        synchronized(secondRunnable){
            secondRunnable.notify();
        }
        System.out.println("Waiting for second thread to finish...");
        second.join();
    }catch(InterruptedException e){
        System.err.println("Error in thread: "+e);
        }
    System.out.println("I'm ready to finish too.");
    }
}这是java 2从入门到精通的一个例子,说明线程间通讯的。我运行后证明书上的结果是错的。
1.谁可以帮我解释一下程序的运行结果?
2. synchronized(secondRunnable){
            secondRunnable.notify();
        }

解决方案 »

  1.   

    1、运行结果
    Waiting for first thread to finish...
    First thread starts running.
    Second thread starts running.
    Second thread suspends itself.
    First thread finishes running.
    Waking up second thread...
    Waiting for second thread to finish...
    Second thread runs again and finishes.
    I'm ready to finish too.2、唤醒secondRunnable
      

  2.   

    首先,楼上,你运行了吗?结果是你写的那样吗?那和书上的结果是一样的。
    第2个问题没写完,关于synchronized,这是什么用法,我搞不懂。
    谁能再帮我解释一下这个程序的运行结果,万分感谢!(我可没10000分)
      

  3.   

    程序运行结果是:
    First thread starts running.
    Waiting for first thread to finish...
    Second thread starts running.
    Second thread suspends itself.
      

  4.   

    不对  落了点 
    结果是这样的:
    First thread starts running.
    Waiting for first thread to finish...
    Second thread starts running.
    Second thread suspends itself.
    First thread finishes running.
    Waking up second thread...
    Waiting for second thread to finish...
    Second thread runs again and finishes.
    I'm ready to finish too.
      

  5.   

    Synchronizes:
    Synchronizes two or more lines. Any subsequent command that starts or stops audio playback or capture for one of these lines will exert the same effect on the other lines in the group, so that they start or stop playing or capturing data simultaneously.
    起同步作用
    意思是说当执行secondRunnable时,对几个线程同时用notify()方法。
      

  6.   

    这种格式是synchoronized(aThread){aThread.notify();}在语法上可以这么用吗?这个不是函数调用,到底是什么?头疼
      

  7.   

    ...
        Thread first=new FirstThread();
        Runnable secondRunnable=new SecondRunnable();
        Thread second=new Thread(secondRunnable);
        first.start();
        second.start();
    ...
    到这里为止,程序有三个线程在运行.
    在此之前它们输出的字符串顺序完全是随机的,天知道谁先执行(系统是抢占式的)synchronized(secondRunnable){
                secondRunnable.notify();
            }
    锁住secondRunnable对象,注意,是对象,如果想锁住类的东东的话,用
    synchronize(对象.getClass()){
     ...
    }
    对于单处理器来说,处理时间才是唯一的.
    对于被锁的对象,在同一个时刻中,只有一个线程在这个语句块中运行.nofity()应该是用来对付wait()的吧.^_^
      

  8.   

    首先程序启动了2个线程
    因为2个线程不能同时运行,线程2等待,所以先运行first线程打印出
    First thread starts running.
    Waiting for first thread to finish...
    然后遇到sleep(10000);线程1等待10秒
    线程2运行,即使线程1的10秒过了1也要等待
    所以打印出
    Second thread starts running.
    Second thread suspends itself.
    然后线程2遇到wait();
    所以2等待 
    1接着运行,所以打印出:
    First thread finishes running.
    然后遇到
    synchronized(secondRunnable){
                secondRunnable.notify();
            }
    同时对2个线程进行notify
    所以打印出了Waking up second thread...
    Waiting for second thread to finish...
    Second thread runs again and finishes.
    I'm ready to finish too.
      

  9.   

    在多线程的程序中,为了防止多个线程同时使用同一个资源,而用synchronized修饰。
      

  10.   

    我运行了一下 程序结果是以下这样子的:
    Waiting for first thread to finish...
    First thread starts running.
    Second thread starts running.
    Second thread suspends itself.
    First thread finishes running.
    Waking up second thread...
    Second thread runs again and finishes.
    Waiting for second thread to finish...
    I'm ready to finish too.
      

  11.   

    2楼的结果是我运行得出的结果。synchronized 是资源保护锁,所有同一对象下的 synchronized 区块同一时只能执行一个。
    synchronized 里的 notify()是为了唤醒该对象的wait()函数,从而继续执行该对象。
      

  12.   

    我的运行结果和tanghanmeng(冰山一角) 的是一样的。
    blue009(无缺) 你的是什么系统?
      

  13.   

    我是xp+jdk1.5,线程的启动顺序是无发预料的。所以有这种差异也是合理的。
      

  14.   

    线程start()后,谁也不能控制它什么时候开始运行(执行run()方法)(操作系统除外),线程没有运行状态,只有可运行状态.
    main()方法中synchronized锁定了secondRunnable对象,而SecondRunnable类的run方法是同步的,要是线程second开始运行后,他会锁定run方法,但运行到wait()后,它又把锁给开了,main()方法中的synchronized就是要等到wait()开锁后他才拥有对象锁,运行notify()方法唤醒second线程完成它的生命周期.在winxp+jdk1.4下运行有如下结果.First thread starts running.
    Waiting for first thread to finish...
    Second thread starts running.
    Second thread suspends itself.
    First thread finishes running.
    Waking up second thread...
    Waiting for second thread to finish...
    Second thread runs again and finishes.
    I'm ready to finish too.Waiting for first thread to finish...
    Second thread starts running.
    Second thread suspends itself.
    First thread starts running.
    First thread finishes running.
    Waking up second thread...
    Waiting for second thread to finish...
    Second thread runs again and finishes.
    I'm ready to finish too.Waiting for first thread to finish...
    First thread starts running.
    Second thread starts running.
    Second thread suspends itself.
    First thread finishes running.
    Waking up second thread...
    Second thread runs again and finishes.
    Waiting for second thread to finish...
    I'm ready to finish too.
    要使该程序死锁也是很easy,修改源程序如下.但操作系统不同,可能情况也不同.
    import java.io.*;class MethodTest{
        static class FirstThread extends Thread {
            public void run(){
            //try {
                System.out.println("First thread starts running.");
                //sleep(1000);
                System.out.println("First thread finishes running.");
            //}catch(InterruptedException e){
            //System.err.println("Error in thread: "+e);
           //}
        }
    }static class SecondRunnable implements Runnable{
        public synchronized void run(){
        try{
            System.out.println("Second thread starts running.");
            System.out.println("Second thread suspends itself.");
            wait();
            System.out.println("Second thread runs again and finishes.");
        }catch(InterruptedException e){
            System.err.println("Error in thread: "+e);
            }
        }
    }public static void main(String args[]){
        Thread first=new FirstThread();
        Runnable secondRunnable=new SecondRunnable();
        Thread second=new Thread(secondRunnable);
        first.setPriority(10);
        second.setPriority(1);
        first.start();
        second.start();
        try{
            System.out.println("Waiting for first thread to finish...");
            first.join();
            System.out.println("Waking up second thread...");
            synchronized(secondRunnable){
                secondRunnable.notify();
            }
            System.out.println("Waiting for second thread to finish...");
            second.join();
        }catch(InterruptedException e){
            System.err.println("Error in thread: "+e);
            }
        System.out.println("I'm ready to finish too.");
        }
    }secondRunnable.notify();在second线程处理可运行状态时这个语句就运行了,所以等到run方法运行到wait()后就永久性的休息了.