粘貼的大致代碼如下:
public static void main(String[] args) {
int thread=0;
ExecutorService pool = Executors.newFixedThreadPool(15);// 創建一個線程池
for (int i =666; i <= 888; i++) {
for (int j = 0; j < 19; j++) {
MyClass t =new MyClass(i,j);//某個繼承了runnable的類,細節不重要
pool.execute(t);
}
}//外層for
thread++;
if(thread>=15){ //15個線程都運行結束了
pool.shutdown();
try {
if (!pool.awaitTermination(60, TimeUnit.SECONDS)){
pool.shutdownNow();
}
} catch (InterruptedException e) {
pool.shutdownNow();
Thread.currentThread().interrupt();
}
System.exit(0);
}
//每次運行時,等到功能完成了(如寫入文件或打印),程序確實停止工作了,但就是不退出,即eclipse控制台紅點沒變灰,要手動結束才行。是代碼的邏輯問題嗎?
沒看懂,thread++;
if(thread>=15){ //15個線程都運行結束了
這怎麼累加到15