验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
MultiThread.java
原始数据 复制代码
package com.soc.event.main;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MultiThread {
public static void main(String[] args) {
ScheduledExecutorService scheduledThreadPool = Executors
.newScheduledThreadPool(5); // 线程池大小5个
// 每次要处理的文件数
final BlockingQueue<String> fileNameQuene = new ArrayBlockingQueue<String>(9);
fileNameQuene.add("1.txt");
fileNameQuene.add("2.txt");
fileNameQuene.add("3.txt");
fileNameQuene.add("4.txt");
fileNameQuene.add("5.txt");
fileNameQuene.add("6.txt");
fileNameQuene.add("7.txt");
fileNameQuene.add("8.txt");
fileNameQuene.add("9.txt");
scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
String fileName = fileNameQuene.take();
System.out.println("fileName:" + fileName);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 1, 1, TimeUnit.SECONDS); // 延迟1秒后每1秒执行一次
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助

12_float_left_people 12_float_left_close