問題 次のプログラムでデッドロックが発生するか調べて説明しなさい。 class X { synchronized void x1() { x2(); } synchronized void x2() { x1(); } } class ThreadX extends Thread { X x; ThreadX(X x){ this.x = x; } public void run() { for (int i = 0; i < 100000; i++) x.x1(); } } class DeadlockQuestion { private final static int NUMTHREADS = 10; public static void main(String args[]){ //オブジェクトの作成 X x = new X(); //スレッドの作成 ThreadX threads[] = new ThreadX[NUMTHREADS]; for (int i = 0; i < NUMTHREADS; i++) { threads[i] = new ThreadX(x); threads[i].start(); } 解答例 デッドロックは発生する。 メソッドx1()とメソッドx2()は互いの処理が終わるまでロックを解除しないため発生する。また、デッドロックを回避するwait()なども設定されていない。