SANJU 난 당신이 다음 라운드 로빈 방식으로이를 실행하려는 생각하지 않습니다 1,6하고 그렇게 Thread2에 : 2,7 Thread3 : 3,8 Thread4 : 4,9 Thread5 : 5,10
먼저 스레드를 실행하려면 무엇을해야합니까? 그런 순서로. 둘째 스레드 풀에서는 가능하지 않다고 생각합니다. 만약 당신이 여전히 그들을 원한다면 여기에 당신이 필요에 따라 변경할 수 있습니다 라운드 로빈 방식으로 스레드를 실행하기위한 내 솔루션입니다 : public class EmailRoundRobin { public Object [] locks; 내가 [이 질문] (Running threads in round robin fashion in java)에 내 대답에서이 촬영 한
private static class EmailProcessor implements Runnable {
private final Object currentLock;
private final Object nextLock;
private UserEmail userEmail;
public EmailProcessor (UserEmail userEmail,, Object currentLock, Object nextLock) {
this.userEmail = userEmail;
this.currentLock = currentLock;
this.nextLock = nextLock;
}
@Override
public void run() {
try {
work = //reading email
while (work != null) {
try {
currentLock.wait();
// Do your work here.
}
catch(InterruptedException e) {}
synchronized(nextLock) {
nextLock.notify();
}
}//while ends
} catch (IOException e) {
e.printStackTrace();
}
synchronized(nextLock) {
nextLock.notify(); /// Ensures all threads exit at the end
}
}
public EmailRoundRobin(int numberOfAccountsToRead) {
locks = new Object[numberOfAccountsToRead];
//Initialize lock instances in array.
for(i = 0; i < numberOfAccountsToRead; ++i) locks[i] = new Object();
//Create threads
int j;
for(j=0; j<(numberOfAccountsToRead-1); j++){
Thread linePrinterThread = new Thread(new EmailProcessor(emailInfo + "Temp" + j,locks[j],locks[j+1]));
linePrinterThread.start();
}
Thread lastLinePrinterThread = new Thread(new EmailProcessor(emailInfo + "Temp" + j,locks[numberOfFilesToRead-1],locks[0]));
lastLinePrinterThread.start();
}
public void startProcessing() {
synchronized (locks[0]) {
locks[0].notify();
}
}
public static void main(String[] args) {
EmailRoundRobin emailRoundRobin = new EmailRoundRobin(4);
emailRoundRobin.startPrinting();
}
}! 비슷한 요구 사항을 가지고 있었다. Phaser를 사용하는 옵션도 있습니다.
@Sanju의 문제에 대한 구체적인 내용을 여전히 이해할 수 없습니다. 더 자세한 내용을 추가 할 수 있습니까? 아래 내 대답에서 사서함은 1-5, 6-10으로 처리됩니다. 그런 다음 지연 후 1-5 번을 처리하고 처음 처리 한 후에 1-5 번을 원하니? 6-10과 동일합니까? – Gray
도움이된다면 제 대답을 수락하는 것을 잊지 마십시오. – Gray