BAEL-7490 read write file in separate thread (#15664)

* BAEL-7490 read write file in separate thread

* Change the to try resources

* Update the code to sync with article
This commit is contained in:
Wynn Teo 2024-01-30 10:41:53 +08:00 committed by GitHub
parent 5ce49b5ce9
commit 5b6d31feff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1 @@
Hello, world!

View File

@ -0,0 +1,85 @@
package com.baeldung.readwritethread;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
public class ReadWriteBlockingQueue {
public static void main(String[] args) throws InterruptedException {
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();
Thread.sleep(100); // Give producer a head start
consumerThread1.start();
try {
producerThread.join();
consumerThread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class FileProducer implements Runnable {
private final BlockingQueue<String> queue;
private final String inputFileName;
public FileProducer(BlockingQueue<String> queue, String inputFileName) {
this.queue = queue;
this.inputFileName = inputFileName;
}
@Override
public void run() {
try (BufferedReader reader = new BufferedReader(new FileReader(inputFileName))) {
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();
}
}
}
class FileConsumer implements Runnable {
private final BlockingQueue<String> queue;
private final String outputFileName;
public FileConsumer(BlockingQueue queue, String outputFileName) {
this.queue = queue;
this.outputFileName = outputFileName;
}
@Override
public void run() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName))) {
String line;
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();
}
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.readwritethread;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ReadWriteThread {
public static void readFile(String filePath) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
public static void writeFile(String filePath, String content) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try (FileWriter fileWriter = new FileWriter(filePath)) {
fileWriter.write("Hello, world!");
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
public static void main(String[] args) {
String file = "src/main/resources/text.txt";
writeFile(file, "Hello, world!");
readFile(file);
// Sleep for a while to allow the threads to complete
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,5 @@
Hello,
Baeldung!
Nice to meet you!
My name is
Wynn.

View File

@ -0,0 +1 @@
Hello, world!

View File

@ -0,0 +1,5 @@
Hello,
Baeldung!
Nice to meet you!
My name is
Wynn.