我的情況,我引用了一些10 Thread
秒,每個Thread
必須等到Notifier
類通知我要通知具體Thread
S,我做的是我使用HashMap
與Thread
ID爲key
和Thread
實例作爲值。後來在Notifier
我試圖通過遍歷地圖map.get(threadId)
它給出Thread
實例通知它,我試圖打電話通知,但它是投擲IllegalmonitorException
。我有疑問,怎麼同步HashMap
或兩個Waiter
和Notifier
類Thread
..是否可以在hashmap中存儲線程對象?
package com.cgi.sample.jms.requestor;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RequestorApplication {
private final Object lock = new Object();
public static String correlationId;
public static String getCorrelationId() {
correlationId = UUID.randomUUID().toString();
return correlationId;
}
public static void main(String args[]) throws Exception {
Map<Long, Thread> map = new HashMap<Long, Thread>();
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 2; i++) {
Waiter waiter = new Waiter(map);
executor.execute(waiter);
Notifier notifier = new Notifier(map);
executor.execute(notifier);
}
System.out.println("All the threads are started");
}
}
class Waiter implements Runnable {
Map<Long, Thread> map;
ExecutorService executor = Executors.newFixedThreadPool(5);
public Waiter(Map<Long, Thread> map) {
this.map = map;
}
public void run() {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 2; i++) {
Runner instance = new Runner();
System.out.println("Executing thread " + " with " + Thread.currentThread().getName());
long threadId = Thread.currentThread().getId();
String threadname = Thread.currentThread().getName();
executor.execute(instance);
synchronized (map) {
map.put(threadId, Thread.currentThread());
try {
instance.wait();
System.out.println(threadname + " Thread entered into waiting state!!!");
// Thread.currentThread().wait();
System.out.println(threadname + " Thread woke up from wait!!!!!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
class Notifier implements Runnable {
Map<Long, Thread> map;
public Notifier(Map<Long, Thread> map)
{
this.map = map;
}
public synchronized void run() {
synchronized (map) {
for (Map.Entry<Long, Thread> entry : map.entrySet()) {
System.out.println("stored threads in map are--->" + map.get(entry.getKey()));
map.get(entry.getKey()).notify();
}
}
}
}
class Runner implements Runnable {
public void run() {
System.out.println("runner invoked");
}
}
*「是否有可能在hashmap中存儲線程對象?」*是的,但那不是你的問題。你的問題是從一個不是該對象監視器所有者的線程的對象調用'notify'作爲['notify'文檔](http://docs.oracle.com/javase/8/docs/api /java/lang/Object.html#notify--)告訴你。由於您尚未顯示「通知」電話,因此我們無法幫助您。 –
Hi.Crowder我打電話通知從Notifier類我修改了代碼你可以看看!!!!! – Tirumalesh
你應該避免同步Thread對象,並且你絕對不應該調用'thread.wait()'或'thread.notify()'。原因是,'Thread'類自身使用'wait()'和'notify()'來達到自己的目的。一個好的經驗法則是永遠不要在_any_庫對象上同步。相反,你可以創建一個私有的最終對象lock = new Object();'然後對其進行同步。 –