整个流程是这样的,循环从一个文件中读取数据,每读6万条后就要暂停,启动三个线程(每次仅允许三个线程同时处理)来处理这6万条数据,处理结束后,再继续读。。。循环这样直到文件中数据全部处理完。大家帮忙看看,有什么问题没,例如处理流程、并发。。。等方面,多谢!
public class CopyOfTest {
public static void main(String[] args) {
Producer p = new Producer();
while(p.producer() > 0){
p.cunsumer();
}
}
}
class Producer {
private Lock lock = new ReentrantLock();
private Condition notEmpty = lock.newCondition();
private Condition notFull = lock.newCondition();
private List<List<String>> dataList = null;
private BufferedReader reader;
private static ExecutorService executor = Executors.newFixedThreadPool(3);
public Producer() {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/11.txt")), "GBK"));
dataList = new ArrayList<List<String>>();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
public int producer() {
String text = "";
List<String> tmp = new ArrayList<String>();
int count = 0;
lock.lock();
try {
try {
System.out.println("+++++++++++");
if (dataList.size() == 0) {
notEmpty.signalAll();
}
if (dataList.size() == 3) {
try {
notFull.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
while ((text = reader.readLine()) != null) {
count++;
System.out.println(count + "+++" + text);
tmp.add(text);
if (count % 20000 == 0) {
dataList.add(tmp);
tmp = new ArrayList<String>();
}
if (dataList.size() == 3) {
break;
}
}
if (count % 60000 != 0) {
dataList.add(tmp);
}
tmp = null;
} catch (IOException e1) {
e1.printStackTrace();
}
if (dataList.size() < 1) {
executor.shutdown();
}
return dataList.size();
} finally {
lock.unlock();
}
}
public void cunsumer() {
lock.lock();
int len = dataList.size();
Future<?>[] futureArr = new Future<?>[len];
try {
if (dataList.size() == 0) {
try {
notEmpty.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (dataList.size() == 3) {
notFull.signalAll();
}
for (int i = 0; i < len; i++) {
futureArr[i] = executor.submit(new SingleThread12(dataList.get(i)));
}
boolean flag = true;
while (flag) {
for (Future<?> f : futureArr) {
flag = flag && f.isDone();
}
flag = !flag;
}
dataList = new ArrayList<List<String>>();
} finally {
lock.unlock();
}
}
}