java 子线程不能终止
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletionService;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
public class ThreadMessageV03 {
public static void main(String args[]) throws InterruptedException, ExecutionException {
ExecutorService exe = Executors.newFixedThreadPool(2);
SyncTest3 st = new SyncTest3();
CyclicBarrier cb = new CyclicBarrier(2);
List<Future<Integer>> futures = new ArrayList<Future<Integer>>();
for (int i=0; i<2; i++) {
Future<Integer> future = exe.submit(st);
futures.add(future);
}
//exe.shutdownNow();
//等待所有线程执行完
try {
cb.await();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (BrokenBarrierException e) {
e.printStackTrace();
}
for (int i=0; i<futures.size(); i++) {
System.out.println(futures.get(i).get());
}
System.out.println("程序结束!");
}
}
class SyncTest3 implements Callable<Integer> {
private int flag = 1;
public synchronized int getFlag() {
return flag;
}
public synchronized void setFlag(int flag) {
this.flag = flag;
}
private static int sum = 0;
public Integer call() {
if (getFlag() == 1) {
System.out.println(Thread.currentThread().getName() + "-flag:" + getFlag());
for (int i=0; i<20; i++) {
//System.out.println(Thread.currentThread().getName() + "-i=" + i);
synchronized (this) {
if (sum >= 10) {
setFlag(0);
System.out.println(Thread.currentThread().getName() + "-flag:" + getFlag());
break;
}
sum ++;
}
System.out.println(Thread.currentThread().getName() + "-sum=" + sum);
}
}
System.out.println(Thread.currentThread().getState());
return sum;
}
}
java
Executor
Future
------解决方案--------------------试了下代码,这样可以结束,
1 不需要设置cb.await(),线程池里的线程如果没执行完,futures.get(i).get()会阻塞的。所以不要那段代码。
2 程序结束还是要加上语句:exe.shutdown();
修改后的代码:
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;