Update the code to sync with article
This commit is contained in:
parent
006660c374
commit
c0f2656474
1
core-java-modules/core-java-io-5/file.txt
Normal file
1
core-java-modules/core-java-io-5/file.txt
Normal file
@ -0,0 +1 @@
|
||||
Hello, world!
|
@ -10,23 +10,25 @@ import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
public class ReadWriteBlockingQueue {
|
||||
|
||||
public static void main(String[] args) {
|
||||
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
|
||||
String inputFileName = "src/main/resources/read_file.txt";
|
||||
String outputFileName = "src/main/resources/write_file.txt";
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
|
||||
Thread producerThread = new Thread(new FileProducer(queue, inputFileName));
|
||||
Thread consumerThread = new Thread(new FileConsumer(queue, outputFileName));
|
||||
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
|
||||
String readFileName = "src/main/resources/read_file.txt";
|
||||
String writeFileName = "src/main/resources/write_file.txt";
|
||||
|
||||
Thread producerThread = new Thread(new FileProducer(queue, readFileName));
|
||||
Thread consumerThread1 = new Thread(new FileConsumer(queue, writeFileName));
|
||||
|
||||
producerThread.start();
|
||||
consumerThread.start();
|
||||
|
||||
Thread.sleep(100); // Give producer a head start
|
||||
consumerThread1.start();
|
||||
try {
|
||||
producerThread.join(); // Wait for producer to finish
|
||||
consumerThread.join(); // Wait for consumer to finish
|
||||
producerThread.join();
|
||||
consumerThread1.join();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,6 +48,8 @@ class FileProducer implements Runnable {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
queue.offer(line);
|
||||
System.out.println("Producer added line: " + line);
|
||||
System.out.println("Queue size: " + queue.size());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -70,6 +74,9 @@ class FileConsumer implements Runnable {
|
||||
while ((line = queue.poll()) != null) {
|
||||
writer.write(line);
|
||||
writer.newLine();
|
||||
System.out.println(Thread.currentThread()
|
||||
.getId() + " - Consumer processed line: " + line);
|
||||
System.out.println("Queue size: " + queue.size());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -28,7 +28,7 @@ public class ReadWriteThread {
|
||||
Thread thread = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try (FileWriter fileWriter = new FileWriter("file.txt")) {
|
||||
try (FileWriter fileWriter = new FileWriter(filePath)) {
|
||||
fileWriter.write("Hello, world!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@ -39,16 +39,15 @@ public class ReadWriteThread {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String readFile = "src/main/resources/read_file.txt";
|
||||
String writeFile = "src/main/resources/write_file.txt";
|
||||
String file = "src/main/resources/text.txt";
|
||||
|
||||
writeFile(writeFile, "Hello, world!");
|
||||
writeFile(file, "Hello, world!");
|
||||
|
||||
readFile(readFile);
|
||||
readFile(file);
|
||||
|
||||
// Sleep for a while to allow the threads to complete
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -2,4 +2,4 @@ Hello,
|
||||
Baeldung!
|
||||
Nice to meet you!
|
||||
My name is
|
||||
Wynn!
|
||||
Wynn.
|
@ -0,0 +1 @@
|
||||
Hello, world!
|
@ -0,0 +1,5 @@
|
||||
Hello,
|
||||
Baeldung!
|
||||
Nice to meet you!
|
||||
My name is
|
||||
Wynn.
|
Loading…
x
Reference in New Issue
Block a user