多线程处理Excel思路代码.
By:Roy.LiuLast updated:2016-11-16
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class MyImport {
private ExecutorService executorService;
private BlockingQueue
private ConcurrentHashMap
private boolean isFinish = false;
public void begin() throws InterruptedException {
executorService = Executors.newCachedThreadPool();
for (int i=0; i< 3; i++) {
ConsumerThread consumerThread = new ConsumerThread();
executorService.execute(consumerThread);
}
executorService.shutdown();
}
public procedure() {
if (!queue.offer(data)) {
System.err.println("failed to put data��" + data);
}
}
public void end() throws Throwable {
while (true) {
if (executorService.isTerminated()) {
System.out.println("结束了!");
break;
}
Thread.sleep(200);
}
}
class ConsumerThread implements Runnable{
@Override
public void run() {
try {
while(true){
PCData data = queue.poll();
System.out.println("数据:" + data);
if(data == null && isFinish) {
System.out.println("没有任务了,退出");
break;
} else {
System.out.println("=====>customer" + data.getData() + ": " + Thread.currentThread().getId());
int re = data.getData() * data.getData();
// 处理任务吧
Thread.sleep(5);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
Thread.currentThread().interrupt();
}
}
}
}
From:一号门
Previous:Java中比较两个SET是否相同
Next:一个模拟html下拉多选框的原生实现.
COMMENTS