-2

如果同时从两个不同的线程调用这些方法,结果会是什么?

public class FirstWord {
       public static synchronized void writeFirstWord(boolean fromSecondWord) throws Exception {
              if(fromSecondWord == false)
                     SecondWord.writeSecondWord();
              System.out.print("Redflex"); }}

public class SecondWord {
       public static synchronized void writeSecondWord() throws Exception {
              Thread.sleep(100);
              FirstWord.writeFirstWord(true);  
              System.out.print(" Traffic Systems"); }}
4

4 回答 4

1

在您拥有的代码示例中很可能出现死锁 - 如果您有 2 个线程,例如 ThreadA 和 ThreadB,那么在这种情况下:

ThreadA 调用FirstWord.writeFirstWord(false),线程在 ThreadB 调用SecondWord.writeSecondWord()中暂停一次,线程在其中暂停一次

现在 ThreadA 继续它会停止,SecondWord.writeSecondWord();因为 ThreadB 有锁 SecondWord。

ThreadB 无法继续,因为 ThreadA 已锁定 FirstWord。

结果是死锁。

请注意,这不是此代码的唯一可能结果 - 根据时间等,代码可能会在一段时间内正常运行,但您很可能会在某个时候遇到死锁。

于 2013-02-18T09:51:15.050 回答
0
package com.blt;

    public class ThreadsExample implements Runnable { 
       public static void main(String args[]) {     
        Thread t=new Thread(new ThreadsExample());
        Thread t1=new Thread(new ThreadsExample());     
        t.start();
        t1.start();      
}

    public void run()  {    
    try { 
        writeSecondWord();  
    }   
    catch(Exception e)  {   
      e.printStackTrace();  
    }
 }

    public static synchronized void writeSecondWord() throws Exception {
        Thread.sleep(100);
        writeFirstWord(true);  
        System.out.print(" Traffic Systems"); 
        } 

    public static synchronized void writeFirstWord(boolean fromSecondWord) throws Exception {
        if(fromSecondWord == false)
               writeSecondWord();
        System.out.print("Redflex"); 
     }
 }

输出是: Redflex 交通系统Redflex 交通系统

运行在代码之上是好的,但是代码在某个时间点很可能会产生死锁。

于 2013-02-18T10:14:08.490 回答
0

由于 . 的不可预测性,结果是不可预测的sleep()

此外,这取决于有多少核心,以及您在writeFirstWord(boolean)调用中提供的参数。

我会把它留给你弄清楚细节:-)


提示:一种可能性是死锁。

于 2013-02-18T09:29:53.320 回答
0

示例 SSCCE(修改):

public class ThreadTest {

    public static void main(String[] args) {
        new Thread() {
            public void run() {
                try {
                    FirstWord.writeFirstWord(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }  
            }
        }.start();

        new Thread() {
            public void run() {
                try {
                    SecondWord.writeSecondWord(false);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

}

class FirstWord {
    public static synchronized void writeFirstWord(boolean fromSecondWord) throws Exception {
        System.out.println("FirstWord start");
        Thread.sleep(100);
        if (!fromSecondWord) SecondWord.writeSecondWord(true);
        System.out.println("FirstWord end");
    }
}

class SecondWord {
    public static synchronized void writeSecondWord(boolean fromFirstWord) throws Exception {
        System.out.println("SecondWord start");
        Thread.sleep(100);
        if (!fromFirstWord) FirstWord.writeFirstWord(true);  
        System.out.println("SecondWord end");
    }
}

如您所见,控制台显示:

FirstWord start
SecondWord start

第一个线程“进入”FirstWord同步块,而第二个线程进入同步块SecondWord。然后,在休眠一点之后,第一个线程尝试进入 SecondWord 方法,并且必须等待,因为第二个线程拥有该方法的锁。

当这种情况发生时,第二个线程也在等待获得另一个锁,这就是为什么两个线程都阻塞并且永远不会到达“结束”,因为它们永远不会获得两个锁

如果您Thread.sleep在这两种方法中删除它可能会起作用。如前所述,这是不可预测的。你不知道哪个线程会先进入。

于 2013-02-18T09:41:37.830 回答