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 class ReadWriteBlockingQueue {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) throws InterruptedException {
|
||||||
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
|
|
||||||
String inputFileName = "src/main/resources/read_file.txt";
|
|
||||||
String outputFileName = "src/main/resources/write_file.txt";
|
|
||||||
|
|
||||||
Thread producerThread = new Thread(new FileProducer(queue, inputFileName));
|
BlockingQueue<String> queue = new LinkedBlockingQueue<>();
|
||||||
Thread consumerThread = new Thread(new FileConsumer(queue, outputFileName));
|
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();
|
producerThread.start();
|
||||||
consumerThread.start();
|
Thread.sleep(100); // Give producer a head start
|
||||||
|
consumerThread1.start();
|
||||||
try {
|
try {
|
||||||
producerThread.join(); // Wait for producer to finish
|
producerThread.join();
|
||||||
consumerThread.join(); // Wait for consumer to finish
|
consumerThread1.join();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,6 +48,8 @@ class FileProducer implements Runnable {
|
|||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
queue.offer(line);
|
queue.offer(line);
|
||||||
|
System.out.println("Producer added line: " + line);
|
||||||
|
System.out.println("Queue size: " + queue.size());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -70,6 +74,9 @@ class FileConsumer implements Runnable {
|
|||||||
while ((line = queue.poll()) != null) {
|
while ((line = queue.poll()) != null) {
|
||||||
writer.write(line);
|
writer.write(line);
|
||||||
writer.newLine();
|
writer.newLine();
|
||||||
|
System.out.println(Thread.currentThread()
|
||||||
|
.getId() + " - Consumer processed line: " + line);
|
||||||
|
System.out.println("Queue size: " + queue.size());
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -28,7 +28,7 @@ public class ReadWriteThread {
|
|||||||
Thread thread = new Thread(new Runnable() {
|
Thread thread = new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try (FileWriter fileWriter = new FileWriter("file.txt")) {
|
try (FileWriter fileWriter = new FileWriter(filePath)) {
|
||||||
fileWriter.write("Hello, world!");
|
fileWriter.write("Hello, world!");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -39,16 +39,15 @@ public class ReadWriteThread {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String readFile = "src/main/resources/read_file.txt";
|
String file = "src/main/resources/text.txt";
|
||||||
String writeFile = "src/main/resources/write_file.txt";
|
|
||||||
|
|
||||||
writeFile(writeFile, "Hello, world!");
|
writeFile(file, "Hello, world!");
|
||||||
|
|
||||||
readFile(readFile);
|
readFile(file);
|
||||||
|
|
||||||
// Sleep for a while to allow the threads to complete
|
// Sleep for a while to allow the threads to complete
|
||||||
try {
|
try {
|
||||||
Thread.sleep(2000);
|
Thread.sleep(1000);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -2,4 +2,4 @@ Hello,
|
|||||||
Baeldung!
|
Baeldung!
|
||||||
Nice to meet you!
|
Nice to meet you!
|
||||||
My name is
|
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