所以我有一个小问题。我在一个循环中检查很多数字,其中数字只是一个AtomicInteger
每次递增的数字。问题是有时会出现“错误”(错误与服务器有关,而不是程序)并且线程需要休眠。在他们睡觉后,我希望所有线程返回并重新检查由于错误而错过的数字,但它只会重新检查最后一个,因为AtomicInteger
已经增加了。这是我的代码:
public class Red extends java.lang.Thread {
private final LinkedBlockingQueue<Integer> queue;
public Red(LinkedBlockingQueue<Integer> queue) {
this.queue = queue;
}
public void run()
{
while(true){
try{
int i = queue.take();
Main.code.setValueAttribute(""+i);
HtmlPage reply = (HtmlPage)Main.btnFinal.click();
Main.webClient.waitForBackgroundJavaScript(Main.r.nextInt(500));
if ( reply.asText().indexOf( "Error" ) > -1 || reply.asText().indexOf( "unavailable" ) > -1) {
int sleep = Main.r.nextInt(900000);
System.out.println(" error, on "+i+" sleeping for "+ sleep);
Thread.sleep(sleep);
continue;
}
System.out.println("Code "+i+" checked.");
}catch(Exception e){
e.printStackTrace();
continue;
}
}
}
}
这是我为线程加注星标的代码:
List<LinkedBlockingQueue<Integer>> queuelist = new ArrayList<LinkedBlockingQueue<Integer>>();
for (int n = 0; n < threads; n++) {
queuelist.add(new LinkedBlockingQueue<Integer>());
java.lang.Thread t = new Red(queuelist.get(n));
t.start();
}
java.lang.Thread a = new MainThread(queuelist);
a.start();
这是我的 MainThread 代码:
public class MainThread extends java.lang.Thread {
final private List<LinkedBlockingQueue<Integer>> queues;
final private AtomicInteger atomicInteger = new AtomicInteger(10000000);
public MainThread(List<LinkedBlockingQueue<Integer>> queues) {
this.queues = queues;
}
public void run() {
while (true) {
int temp = atomicInteger.getAndIncrement();
for(LinkedBlockingQueue<Integer> queue : queues) {
queue.offer(temp);
}
}
}
}