我知道之前可能已经提到过,但我找不到任何话题,所以我打开这个话题。
在这段代码中,我有一个字符串的ArrayList( coda )和一个String数组,我用它来分割ArrayList的每个元素。在for的每次迭代中,我都有一个给予类的元素(WorkerRSR,WorkerLRSR或两者),这些类将执行不相关的东西。我喜欢的是在for the'''''''''''''''''''''''''''''''''''''''''''''''''''''''''-
Do-While细节: 这需要确保每个字符串都已由类完全处理。每次运行后,每个类都会将变量数增加一个,以便我知道所有元素何时完成。不幸的是,这样做会冻结整个计划。
有没有办法在循环期间检查变量?我知道我应该使用线程;我也检查了它们,但它没有那么多帮助......
//Let's pretend I have a fixed number or elements (3)
public static Semaphore sem = new Semaphore(-2,true);
[...]
if (src == execoda){
if (coda.size()==0){
JOptionPane.showMessageDialog(null,"La coda è vuota. Aggiungere elementi alla coda", "Attenzione",JOptionPane.INFORMATION_MESSAGE);
}
else{
System.out.println("inizio for "+sem.availablePermits());
codatof = false;
for (int i = 0; i<coda.size(); i++){
String[] tmp = ((String)coda.get(i)).split("-");
algo = tmp[6];
if(algo.equals("RSR")){
File[] folders = {new File(urlFrames+"\\finalRSR"),};
folders[0].mkdir();
//parametri WorkerRSR: String urlImg, int dist, int numI, int spra, String algo (6), String urlFrames (7)
WorkerRSR workerRSR = new WorkerRSR(tmp[0],Integer.parseInt(tmp[1]),Integer.parseInt(tmp[2]),Integer.parseInt(tmp[3]),tmp[6],tmp[7]);
workerRSR.execute();
}
else if(algo.equals("LRSR")){
File[] folders = {new File(urlFrames+"\\finalLRSR"),};
folders[0].mkdir();
//parametri WorkerLRSR: String urlImg, int dist, int numI, int spra, String algo, String urlFrames
WorkerLRSR workerLRSR = new WorkerLRSR(tmp[0],Integer.parseInt(tmp[1]),Integer.parseInt(tmp[2]),Integer.parseInt(tmp[3]),tmp[6],tmp[7],Integer.parseInt(tmp[4]),Integer.parseInt(tmp[5]));
workerLRSR.execute();
}
else if(algo.equals("R+L")){
//eseguo entrambi
File[] folders = {new File(urlFrames+"\\finalRSR"),new File(urlFrames+"\\finalLRSR")};
//eseguo RSR
folders[0].mkdir();
WorkerRSR workerRSR = new WorkerRSR(tmp[0],Integer.parseInt(tmp[1]),Integer.parseInt(tmp[2]),Integer.parseInt(tmp[3]),tmp[6],tmp[7]);
workerRSR.execute();
//eseguo LRSR
folders[1].mkdir();
WorkerLRSR workerLRSR = new WorkerLRSR(tmp[0],Integer.parseInt(tmp[1]),Integer.parseInt(tmp[2]),Integer.parseInt(tmp[3]),tmp[6],tmp[7],Integer.parseInt(tmp[4]),Integer.parseInt(tmp[5]));
workerLRSR.execute();
}
System.out.println("fine for "+sem.availablePermits());
}
codatof = true;
}
//trying to acquire
try {
sem.acquire();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null,"Coda Eseguita", "Attenzione",JOptionPane.WARNING_MESSAGE);
}
编辑:这是两名工人的尸体。它不是完整的代码,我不包括构建器和其他会使它毫无意义地变长的东西。
两个工人之间的主要区别在于RSR将称为一个名为&#34; MakeRSR&#34;而LRSR会调用名为&#34; LRSR.exe&#34; 的exe文件
WorkerRSR(完成方法):
@Override
protected void done() {
PanelRSR_LRSR.getProgessbar().setIndeterminate(false);
if (codatof) //codatof will be false
JOptionPane.showMessageDialog(null,"Finito RSR", "Attenzione",JOptionPane.WARNING_MESSAGE);
else{
CreateOption.sem.release();
System.out.println("Semaforo liberato");
}
System.out.println("Algoritmo RSR esguito");
System.out.println("L'output e' stato salvato in: "+ urlFrames+"\\finalRSR ");
System.out.println("RSR sem "+CreateOption.sem.availablePermits());
}
答案 0 :(得分:1)
如果您的Worker类正在同步处理数据(即在同一个线程上),那么保证在退出for循环时所有字符串都已处理完毕。如果工作类正在执行异步操作,最好的解决方案是使用ExecutorService,您可以使用它来等待并报告所有作业何时完成。实际情况是ExecutorService是一个围绕Thread.join的框架,您也可以使用它来等待所有异步操作完成。
-----编辑-------
AH,所以你使用的是SwingWorkers。在这种情况下,您不能使用Thread.join(),我建议您使用信号量(docs.oracle.com/javase/8/docs/api/java/util/concurrent/...)启动它否定计数(1 - coda.size()),在主线程中调用acquire()并在SwingWorker线程中调用release()
答案 1 :(得分:1)
如果我正确理解您要实现的目标,请使用:
//this will restart the for loop until the stop criteria
//is met
while (true){
//the for loop
for (int i = 0; i<coda.size(); i++){
//...........
}
//stop criteria
if (variable == coda.size()) break ;
}//end of while loop