-1
我有一個Theadpool,創建並創建一組函數,我想停下來,用按鈕暫停和恢復這組Theads,所以我可以看到模擬的結果。下面是代碼:如何使用線程池,暫停,播放,恢復和停止所有線程?
無效神祕(){
// create a pool of threads, 10 max jobs will execute in parallel
/// start
ExecutorService threadPool = Executors.newFixedThreadPool(network.mobiles.size());
// submit jobs to be executing by the pool
for (int i=0; i<network.mobiles.size(); i++) {
// for (int i=0; i<1; i++) {
final int j = i ;
threadPool.submit(new Runnable() {
public void run() {
int value = 100 ;
for (int z=0; z<value; z++){
value++;
int x = (mobnod[j].getLocation().x) +7;
int y = (mobnod[j].getLocation().y) +7 ;
System.out.println("x="+x+" , y="+y);
double r = Math.random();
System.out.println(r);
if (r<0.25) x=x-5;
if (r>=0.25 && r<0.50) x=x+5;
if (r>=0.5 && r<0.75) y=y-5;
if (r>=0.75 && r<1.00) y=y+5;
System.out.println("x'="+x+" , y'="+y);
if ((0 < x && x<1300) || (0 <y && y <800)){
mobnod[j].setLocation(x-7, y-7);
network.mobiles.get(j).findClosestAP(network.nodes);
network.setConnections();
}
try {
threadPool.awaitTermination(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
// once you've submitted your last job to the service it should be shut down
threadPool.shutdown();
// wait for the threads to finish if necessary
}
請幫助我,謝謝:)
HTTP ://stackoverflow.com/questions/9748710? – assylias
幫助什麼?你發佈了一些代碼,一些要求;但沒有什麼能夠告訴我們你究竟在哪裏陷入困境;或者你對我們的期望。 – GhostCat
從提交的任務中對執行程序調用'awaitTermination'是沒有意義的。你如何期待執行者在其中一項工作等待時完成終止? – Holger