【发布时间】:2016-10-25 00:14:22
【问题描述】:
我遵循的代码将一些数据放入阻塞队列,并基于固定线程池将线程任务提交给 Java 执行器服务。当我尝试关闭执行程序但它没有关闭时,有什么想法吗?
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadExecutir {
public static ArrayBlockingQueue<String> files;
public static void main(String[] args) {
// TODO Auto-generated method stub
shutdownExec();
}
private static void shutdownExec() {
int size = 10;
files = new ArrayBlockingQueue<String>(100);
for (int i = 0; i < 5; i++) {
files.add("Java " + i);
}
ThreadExecutir outer = new ThreadExecutir();
ExecutorService executor = Executors.newFixedThreadPool(size);
for (int i = 0; i < 3 * size; i++) {
executor.submit(outer.new myThread());
}
System.out.println(executor.isShutdown());
executor.shutdown();
try {
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
System.out.println("Awaiting for threads to complete!");
} catch (InterruptedException e) {
e.printStackTrace();
}
if (files.isEmpty())
executor.shutdownNow();
System.out.println(executor.isShutdown());
}
class myThread extends Thread {
public void run() {
String threadName = Thread.currentThread().getName();
System.out.println("Thread " + threadName + "started running! ");
try {
String uploadThis = files.take();
System.out.println("I'm working " + threadName + " "
+ uploadThis);
// this.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("Thread " + threadName
+ " finished running! ");
}
}
}
}
【问题讨论】:
标签: java multithreading threadpool executorservice