나는 약 10 개 Thread
의 호출 시나리오를 가지고 있고, Notifier
클래스 I 특정 Thread
의 통지 할 통지까지 Thread
이있는 각 기다릴, 내가 무엇을하고 있는가하는 것은 내가 key
로 Thread
ID와 Thread
인스턴스와 함께 HashMap
을 사용하고있다 값. 나중에 Notifier
나는 Thread
인스턴스를 제공하는지도 map.get(threadId)
을 가로 지르면서이를 알리려고하고 있습니다. 그리고 그걸 알리려고 노력하고 있지만, IllegalmonitorException
을 던지고 있습니다. YES : 나는 당신의 질문에 대한 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");
}
}
* "해시 맵에 스레드 개체를 저장할 수 있습니까?"* 예,하지만 그건 문제가 아닙니다. 문제는 [notify' 문서] (http://docs.oracle.com/javase/8/docs/api)와 같이 객체의 모니터 소유자가 아닌 스레드에서 객체에 대해 '통지 (notify)'를 호출하는 것입니다. /java/lang/Object.html#notify--)에서 알려줍니다. 'notify' 호출을 표시하지 않았으므로, 우리는 당신을 도와 줄 수 없습니다. –
안녕하세요. 크롤러 알리미 클래스에서 알리겠습니다. 코드를 수정했습니다.보세요 !!!!! – Tirumalesh
Thread 객체에서 동기화를 피해야하며'thread.wait()'또는'thread.notify()'를 호출하면 안됩니다. 그 이유는'Thread' 클래스는 자체적으로'wait()'와'notify()'를 사용하기 때문입니다. 가장 좋은 방법은 _any_ 라이브러리 객체에서 결코 동기화하지 않는 것입니다. 대신에,'private final Object lock = new Object();'를 만들 수 있고 대신에 그것을 동기화 할 수 있습니다. –