我有场景,我的一些调用10Thread
秒,每个Thread
都有等到Notifier
类通知我要通知具体Thread
S,我做的是我使用HashMap
与Thread
id作为key
和Thread
实例作为值。后来Notifier
我试图通过遍历map.get(threadId)
给出Thread
实例的地图来通知它,我试图调用通知但它正在抛出IllegalmonitorException
. 我有疑问怎么是否同步HashMap
或Thread
在双方Waiter
和Notifier
类..
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");
}
}
您的问题的答案是:YES。因为线程是一个对象,它可以存储到 HashMap 中。
另外,@TJCrowder 告诉您,您使用通知的方式是错误的。这是因为您正在对没有 Thread ( map.get(entry.getKey()).notify();
)锁定的对象调用通知。您应该调用拥有线程锁notify
的对象Runner
( instance.notify()
) 的 ,而不是您的代码向我们展示的:instance.wait();
首先!您必须instance
将块与将使用该wait
方法阻止线程的对象同步,例如:
synchronized(instance){
try{
instance.wait()
}catch(...)
{
//Do Something else
}
}
并且,调用notify
对象的方法Runner
停止等待并继续线程并离开同步块,如下所示:
map.get(entry.getKey()).getRunnerInstance().notify();
但是,请考虑一下,使用您当前的代码很难实现此提示,因为您必须重写几个部分,例如:您使用处于 for 循环上下文中的对象来阻塞线程的代码。
for (int i = 0; i < 2; i++) {
Runner instance = new Runner();
//...
synchronized (map) {
map.put(threadId, Thread.currentThread());
try {
instance.wait(); //You are blocking over a instance declared into the For Loop.
}catch(..){
//..
};
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句