import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.*;
public class HelloWord {
public static void main(String[] args) throws Exception {
String filepath = "C:\\test.txt";
FileInputStream fileInputStream = new FileInputStream(new File(filepath));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
// 容器閾值
int limit = 100000;
// 資料容器
List<String> container = new ArrayList<>(limit);
// 定義執行緒鎖
Lock lock = new ReentrantLock();
Condition producer = lock.newCondition();
Condition consumer = lock.newCondition();
CountDownLatch latch = new CountDownLatch(2);
// 檔案結束標識
AtomicBoolean eof = new AtomicBoolean(false);
// 生產者執行緒
Thread producerThread = new Thread(() -> {
String str;
try {
lock.lock();
while ((str = bufferedReader.readLine()) != null) {
container.add(str);
if (container.size() == limit) {
consumer.signal();
producer.await();
}
}
System.out.println("Producer thread name is:" + Thread.currentThread().getName() + ", data read end.");
eof.set(true);
latch.countDown();
consumer.signal();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
// 資料消費執行緒
Thread consumerThread = new Thread(() -> {
try {
lock.lock();
while (true) {
if (container.size() > 0) {
// 模擬消費資料
for (String var0 : container) {
System.out.println(var0);
}
// 清空資料容器
container.clear();
}
System.out.println("Consumer thread name is:" + Thread.currentThread().getName() + ", this batch data consume finish, current container size:" + container.size() + ", EOF:" + eof + ".");
if (!eof.get()) {
producer.signal();
consumer.await();
} else {
System.out.println("Consumer thread name is:" + Thread.currentThread().getName() + " file data consume end.");
break;
}
}
latch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
});
// 啟動執行緒
producerThread.setName("producer");
producerThread.start();
consumerThread.setName("consumer");
consumerThread.start();
// 阻塞執行緒
latch.await();
// 關閉IO流
System.out.println("ready close resource.");
bufferedReader.close();
fileInputStream.close();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/379425.html
標籤:其他
上一篇:第2講:質量占據C位
下一篇:數倉開發那些事(4)
