多线程处理Excel思路代码.

摘要: import java.util.concurrent.BlockingQueue;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;


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 queue = new LinkedBlockingQueue();
private ConcurrentHashMap cacheList;
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();
}
}
}



}

上一篇: Java中比较两个SET是否相同
下一篇: 一个模拟html下拉多选框的原生实现.
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章来源于互联网, 若有侵权, 联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布文章.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号