| <発生環境> | |
|---|---|
| OS | RedHatLinux6.1/6.2 |
| JDK | JDK1.3 |
| Vender | Sun |
- <A7-1>
- 1つのlockオブジェクトに対して、複数のThreadがwait()していることが原因です。
lockで一度に待機するThreadを1つにすることで解決できます。
また、lockに複数のThreadが待機している場合、lock.notify()で処理を再開するThreadは任意となることに注意してください。
| <発生環境> | |
|---|---|
| OS | RedHatLinux6.1/6.2 |
| JDK | JDK1.3 |
| Vender | Sun |
| <発生環境> | |
|---|---|
| OS | Solaris2.5.1 |
| JDK | JDK1.2.1 |
| Vender | Sun |
マシンが高負荷である状態で、サウンド鳴動が停止せずアプリケーションが操作不能になりました。アプリケーションを再起動すると回復します。
| <発生環境> | |
|---|---|
| OS | Windows NT/2000/XP |
| JDK | JDK1.3 |
| Vender | Sun |
私のプログラムが止まっている様です。コンソールにもログファイルにも何も出力しないため、何が起こっているかわかりません。
解析方法を教えてください。
| <発生環境> | |
|---|---|
| OS | Any |
| JDK | JDK1.2以降 |
| Vender | Sun |
Full thread dump Java HotSpot(TM) Client VM (1.4.1_01-b01 mixed mode):
"DestroyJavaVM" prio=5 tid=0x00034788 nid=0x764 waiting on condition [0..7fadc]
"Thread-2" prio=5 tid=0x00A12430 nid=0xaf4 waiting for monitor entry [acef000..acefd8c]
at sample.deadlock.DeadLock.lock(DeadLock.java:29)
- waiting to lock <02AA3B28> (a java.lang.Object)
at sample.deadlock.DeadLock.run(DeadLock.java:47)
- locked <02AA3B30> (a java.lang.Object)
"Thread-1" prio=5 tid=0x00A12EB0 nid=0x5cc waiting for monitor entry [acaf000..acafd8c]
at sample.deadlock.DeadLock.lock(DeadLock.java:29)
- waiting to lock <02AA3B30> (a java.lang.Object)
at sample.deadlock.DeadLock.run(DeadLock.java:47)
- locked <02AA3B28> (a java.lang.Object)
==== 中略 ====
"Suspend Checker Thread" prio=10 tid=0x009A8530 nid=0xb08 runnable
Found one Java-level deadlock:
=============================
"Thread-2":
waiting to lock monitor 0x9a4b7c (object 0x2aa3b28, a java.lang.Object),
which is held by "Thread-1"
"Thread-1":
waiting to lock monitor 0x9a4b5c (object 0x2aa3b30, a java.lang.Object),
which is held by "Thread-2"
Java stack information for the threads listed above:
=================================
"Thread-2":
at sample.deadlock.DeadLock.lock(DeadLock.java:29)
- waiting to lock <02AA3B28> (a java.lang.Object)
at sample.deadlock.DeadLock.run(DeadLock.java:47)
- locked <02AA3B30> (a java.lang.Object)
"Thread-1":
at sample.deadlock.DeadLock.lock(DeadLock.java:29)
- waiting to lock <02AA3B30> (a java.lang.Object)
at sample.deadlock.DeadLock.run(DeadLock.java:47)
- locked <02AA3B28> (a java.lang.Object)
Found 1 deadlock.
上の例では、"Thread-1"と"Thread-2"がlock()メソッドでロックしており、そのデッドロックをJavaVMが検出/指摘していることが分かります。JDK1.5以降で導入されたConcurrent APIを利用して、スレッドプールを実装し、
3つの処理を並列処理させようとしましたが、
以下のように、それぞれのタスクが順次に実行されます。
Task0-0 Start (31)
Task0-1 Start (1046)
Task0-2 Start (2060)
Task1-0 Start (3075)
Task1-1 Start (4090)
Task1-2 Start (5105)
Task2-0 Start (6120)
Task2-1 Start (7134)
Task2-2 Start (8149)| <発生環境> | |
|---|---|
| OS | Any |
| JDK | JDK5.0以降 |
| Vender | Sun |
Task0-0 Start (203)
Task1-0 Start (203)
Task2-0 Start (204)
Task1-1 Start (1218)
Task0-1 Start (1218)
Task2-1 Start (1219)
Task1-2 Start (2233)
Task0-2 Start (2233)
Task2-2 Start (2234)
参考:long start = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool(3);
for (int index = 0; index < 3; index++){
executor.execute(new Task(index, start));
}
executor.shutdown();
public class Task implements Runnable{
int index_;
long start_;
public Task(int index, long start) {
this.index_ = index;
this.start_ = start;
}
public void run(){
for (int index = 0; index < 3; index++){
long time = System.currentTimeMillis() - this.start_;
System.out.println("Task" + this.index_
+ "-" + index + " Start (" + time + ")");
try{
Thread.sleep(1000);
}catch (InterruptedException ignore){
ignore.printStackTrace();
}
}
}
}
JDK1.5以降で導入されたLinkedBlockingQueueを用いて、
以下のような処理を実行したいと考えましたが、
処理を待ち受けてくれません。
実行結果のように、Serverが処理を待ち受けずにnullを取得してしまいます。
[実行結果]
Server handles null
Client requests [ Request No. 0 ]
Client requests [ Request No. 1 ]
Client requests [ Request No. 2 ]
Client requests [ Request No. 3 ]
Server handles [ Request No. 0 ]
(略)
Client requests [ Request No. 410 ]
Server handles [ Request No. 408 ]
Client requests [ Request No. 411 ]
Server handles [ Request No. 409 ]
Server handles [ Request No. 410 ]
Server handles [ Request No. 411 ]
Server handles null
Client requests [ Request No. 412 ]
RequestQueue queue = new RequestQueue();
new ClientThread(queue).start();
new ServerThread(queue).start();
public class RequestQueue{
private BlockingQueue queue_ = new LinkedBlockingQueue();
public Request getRequest(){
Request req = queue_.poll();
return req;
}
public void putRequest(Request request){
queue_.offer(request);
}
}
※ServerThread、ClientThreadはそれぞれ、| <発生環境> | |
|---|---|
| OS | Any |
| JDK | JDK5.0以降 |
| Vender | Sun |
public class RequestQueue{
private BlockingQueue queue_ = new LinkedBlockingQueue();
public Request getRequest(){
Request req = null;
try{
req = queue_.take();
}catch (InterruptedException ignore){
ignore.printStackTrace();
}
return req;
}
public void putRequest(Request request){
try{
queue_.put(request);
}catch (InterruptedException ignore){
ignore.printStackTrace();
}
}
}
この修正の結果、実行結果は以下のようになりました。Client requests [ Request No. 0 ]
Server handles [ Request No. 0 ]
Client requests [ Request No. 1 ]
Client requests [ Request No. 2 ]
Client requests [ Request No. 3 ]
Server handles [ Request No. 1 ]
(略)
Server handles [ Request No. 430 ]
Client requests [ Request No. 432 ]
Server handles [ Request No. 431 ]
Client requests [ Request No. 433 ]
Server handles [ Request No. 432 ]
Client requests [ Request No. 434 ]
注意:本文書の内容に誤りがあり、またこの文書によって不利益を被っても、
エスエムジー株式会社は一切関知いたしません。