10 개의 스레드 그룹을 시작하고 싶습니다. 내 주요 프로그램 생성자에서 내가 사용 :ExecutorService에 대한 오해 -
executor = Executors.newFixedThreadPool(NTHREADS);
Callable<String> poller;
for (int i = 0; i < NTHREADS; ++i) {
Future<String> future = executor.submit(new Poller(0x3A, m_socket, ds_in, ds_out, socketLock));
set.add(future);
}
을 클래스 폴러의() 메서드 호출을 위해 내가 가진 :
public String call()
{
// This has to be set here, otherwise all threads will have a name of "main".
myID = Thread.currentThread().getName();
boolean shutup_loop = true;
do {
System.out.println("Hey, I'm thread " + myID);
System.out.println("Hey, I'm thread " + Thread.currentThread().getName());
try {
Thread.sleep(10);
}
catch (java.lang.InterruptedException e) {
System.out.println("thread " + myID + ": " + e);
}
// Do if you want the printing to all match up on one line
synchronized (this) {
ByteArrayOutputStream baos = SendReceive(pollPacket);
System.out.print(myID + ": ");
if (baos != null) {
printStuff(baos);
System.out.println();
}
notify();
}
} while (shutup_loop);
return "poller is finished";
}
이 폴러 스레드 SendReceive(), 폴러 클래스의 일부를 요구하고있다 :
public synchronized ByteArrayOutputStream SendReceive(byte[] toSend)
{
System.out.println("START");
System.out.println("SendReceive()1 " + myID);
System.out.println("SendReceive()2 " + Thread.currentThread().getName());
System.out.println("END");
try {
ds_out.write(toSend, 0, toSend.length);
ds_out.flush();
}
catch (java.io.IOException e) {
System.out.println("thread " + myID + ": " + e);
}
try {
m_socket.setSoTimeout(200); // <-- might need tweaking
}
catch (java.net.SocketException e) {
System.out.println("thread " + myID + ": " + e);
}
ByteArrayOutputStream baos = null;
try {
baos = getResponse(ds_in);
}
catch (java.io.IOException e) {
System.out.println("thread " + myID + ": " + e);
}
return baos;
}
이 내가 출력이 닮은 기대 동기화 방법이기 때문에 :
가 403,210대신이하고있다 : 무엇이 제공
START
START
START
START
START
START
SendReceive()1 pool-1-thread-2
START
START
START
SendReceive()1 pool-1-thread-6
SendReceive()1 pool-1-thread-7
SendReceive()2 pool-1-thread-2
SendReceive()1 pool-1-thread-3
SendReceive()2 pool-1-thread-6
SendReceive()1 pool-1-thread-1
SendReceive()1 pool-1-thread-9
SendReceive()1 pool-1-thread-8
SendReceive()2 pool-1-thread-9
END
...
를?
귀하의 질문은'Exec'에 관한 것이지,'ExecutorService'에 관한 것이 아닙니다. – immibis
당신의 질문은'Exec'에 관한 것이지'ExecutorService'에 관한 것이 아닙니다. – immibis
'SendReceive' 선언에서'synchronized'을 제거합니다 (어쨌든 아무것도하지 않습니다). 'Send (동기화) (Poller.class)'로'SendReceive'를 호출 할 때'synchronized (this)'를 변경하십시오. 그러면 의도 한대로 작동합니다. 이유를 이해하는 데 필요한 assillias의 답변을 참조하십시오. – Dima