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();
System.out.println("ready close resource.");
bufferedReader.close();
fileInputStream.close();
}
}