From 2510402809b7c5f46262c234ca41e9bccc184bd7 Mon Sep 17 00:00:00 2001 From: SGWebFreelancer Date: Wed, 17 Jan 2024 16:01:47 +0800 Subject: [PATCH 1/4] BAEL-7490 read write file in separate thread --- .../ReadWriteBlockingQueue.java | 88 +++++++++++++++++++ .../readwritethread/ReadWriteThread.java | 62 +++++++++++++ .../src/main/resources/read_file.txt | 5 ++ .../src/main/resources/write_file.txt | 0 4 files changed, 155 insertions(+) create mode 100644 core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java create mode 100644 core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java create mode 100644 core-java-modules/core-java-io-5/src/main/resources/read_file.txt create mode 100644 core-java-modules/core-java-io-5/src/main/resources/write_file.txt diff --git a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java new file mode 100644 index 0000000000..06e5fd7946 --- /dev/null +++ b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java @@ -0,0 +1,88 @@ +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) { + BlockingQueue 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)); + Thread consumerThread = new Thread(new FileConsumer(queue, outputFileName)); + + producerThread.start(); + consumerThread.start(); + + try { + producerThread.join(); // Wait for producer to finish + consumerThread.join(); // Wait for consumer to finish + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} + +class FileProducer implements Runnable { + + private final BlockingQueue queue; + private final String inputFileName; + + public FileProducer(BlockingQueue 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); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} + +class FileConsumer implements Runnable { + + private final BlockingQueue queue; + private final String outputFileName; + + public FileConsumer(BlockingQueue queue, String outputFileName) { + this.queue = queue; + this.outputFileName = outputFileName; + } + + @Override + public void run() { + BufferedWriter writer = null; + try { + writer = new BufferedWriter(new FileWriter(outputFileName)); + String line; + while ((line = queue.poll()) != null) { + writer.write(line); + writer.newLine(); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + if (writer != null) { + try { + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java new file mode 100644 index 0000000000..7248e0f99d --- /dev/null +++ b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java @@ -0,0 +1,62 @@ +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 { + FileReader fileReader = new FileReader(filePath); + BufferedReader bufferedReader = new BufferedReader(fileReader); + String line; + while ((line = bufferedReader.readLine()) != null) { + System.out.println(line); + } + bufferedReader.close(); + fileReader.close(); + } 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(content); + fileWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }); + thread.start(); + } + + public static void main(String[] args) { + String readFile = "src/main/resources/read_file.txt"; + String writeFile = "src/main/resources/write_file.txt"; + + writeFile(writeFile, "Hello, world!"); + + readFile(readFile); + + // Sleep for a while to allow the threads to complete + try { + Thread.sleep(2000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } +} diff --git a/core-java-modules/core-java-io-5/src/main/resources/read_file.txt b/core-java-modules/core-java-io-5/src/main/resources/read_file.txt new file mode 100644 index 0000000000..91c461855b --- /dev/null +++ b/core-java-modules/core-java-io-5/src/main/resources/read_file.txt @@ -0,0 +1,5 @@ +Hello, +Baeldung! +Nice to meet you! +My name is +Wynn! \ No newline at end of file diff --git a/core-java-modules/core-java-io-5/src/main/resources/write_file.txt b/core-java-modules/core-java-io-5/src/main/resources/write_file.txt new file mode 100644 index 0000000000..e69de29bb2 From 006660c374ef0a33db173c88eeb5455004584e15 Mon Sep 17 00:00:00 2001 From: SGWebFreelancer Date: Thu, 18 Jan 2024 12:05:31 +0800 Subject: [PATCH 2/4] Change the to try resources --- .../readwritethread/ReadWriteBlockingQueue.java | 12 +----------- .../baeldung/readwritethread/ReadWriteThread.java | 12 +++--------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java index 06e5fd7946..b518b455ea 100644 --- a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java +++ b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java @@ -65,9 +65,7 @@ class FileConsumer implements Runnable { @Override public void run() { - BufferedWriter writer = null; - try { - writer = new BufferedWriter(new FileWriter(outputFileName)); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputFileName))) { String line; while ((line = queue.poll()) != null) { writer.write(line); @@ -75,14 +73,6 @@ class FileConsumer implements Runnable { } } catch (IOException e) { e.printStackTrace(); - } finally { - if (writer != null) { - try { - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } } } } \ No newline at end of file diff --git a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java index 7248e0f99d..ba8c5547e3 100644 --- a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java +++ b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java @@ -11,15 +11,11 @@ public class ReadWriteThread { Thread thread = new Thread(new Runnable() { @Override public void run() { - try { - FileReader fileReader = new FileReader(filePath); - BufferedReader bufferedReader = new BufferedReader(fileReader); + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } - bufferedReader.close(); - fileReader.close(); } catch (IOException e) { e.printStackTrace(); } @@ -32,10 +28,8 @@ public class ReadWriteThread { Thread thread = new Thread(new Runnable() { @Override public void run() { - try { - FileWriter fileWriter = new FileWriter(filePath); - fileWriter.write(content); - fileWriter.close(); + try (FileWriter fileWriter = new FileWriter("file.txt")) { + fileWriter.write("Hello, world!"); } catch (IOException e) { e.printStackTrace(); } From c0f26564743956d3064d449ada8038b8ea21b525 Mon Sep 17 00:00:00 2001 From: SGWebFreelancer Date: Tue, 23 Jan 2024 08:46:03 +0800 Subject: [PATCH 3/4] Update the code to sync with article --- core-java-modules/core-java-io-5/file.txt | 1 + .../ReadWriteBlockingQueue.java | 27 ++++++++++++------- .../readwritethread/ReadWriteThread.java | 11 ++++---- .../src/main/resources/read_file.txt | 2 +- .../src/main/resources/text.txt | 1 + .../src/main/resources/write_file.txt | 5 ++++ 6 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 core-java-modules/core-java-io-5/file.txt create mode 100644 core-java-modules/core-java-io-5/src/main/resources/text.txt diff --git a/core-java-modules/core-java-io-5/file.txt b/core-java-modules/core-java-io-5/file.txt new file mode 100644 index 0000000000..5dd01c177f --- /dev/null +++ b/core-java-modules/core-java-io-5/file.txt @@ -0,0 +1 @@ +Hello, world! \ No newline at end of file diff --git a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java index b518b455ea..ee0cc84955 100644 --- a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java +++ b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteBlockingQueue.java @@ -10,23 +10,25 @@ import java.util.concurrent.LinkedBlockingQueue; public class ReadWriteBlockingQueue { - public static void main(String[] args) { - BlockingQueue 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 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(); diff --git a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java index ba8c5547e3..47f6a79d5f 100644 --- a/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java +++ b/core-java-modules/core-java-io-5/src/main/java/com/baeldung/readwritethread/ReadWriteThread.java @@ -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(); } diff --git a/core-java-modules/core-java-io-5/src/main/resources/read_file.txt b/core-java-modules/core-java-io-5/src/main/resources/read_file.txt index 91c461855b..9193448ace 100644 --- a/core-java-modules/core-java-io-5/src/main/resources/read_file.txt +++ b/core-java-modules/core-java-io-5/src/main/resources/read_file.txt @@ -2,4 +2,4 @@ Hello, Baeldung! Nice to meet you! My name is -Wynn! \ No newline at end of file +Wynn. \ No newline at end of file diff --git a/core-java-modules/core-java-io-5/src/main/resources/text.txt b/core-java-modules/core-java-io-5/src/main/resources/text.txt new file mode 100644 index 0000000000..5dd01c177f --- /dev/null +++ b/core-java-modules/core-java-io-5/src/main/resources/text.txt @@ -0,0 +1 @@ +Hello, world! \ No newline at end of file diff --git a/core-java-modules/core-java-io-5/src/main/resources/write_file.txt b/core-java-modules/core-java-io-5/src/main/resources/write_file.txt index e69de29bb2..f1df68d0ad 100644 --- a/core-java-modules/core-java-io-5/src/main/resources/write_file.txt +++ b/core-java-modules/core-java-io-5/src/main/resources/write_file.txt @@ -0,0 +1,5 @@ +Hello, +Baeldung! +Nice to meet you! +My name is +Wynn. From 25638c9c788515b7c32d3407bf64fd01261c556a Mon Sep 17 00:00:00 2001 From: Wynn Date: Tue, 16 Apr 2024 08:30:45 +0800 Subject: [PATCH 4/4] improvement --- .../compareyesterday/CompareYesterdayUnitTest.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/core-java-modules/core-java-date-operations-4/src/test/java/com/baeldung/compareyesterday/CompareYesterdayUnitTest.java b/core-java-modules/core-java-date-operations-4/src/test/java/com/baeldung/compareyesterday/CompareYesterdayUnitTest.java index 75f63317a7..67a6e859c7 100644 --- a/core-java-modules/core-java-date-operations-4/src/test/java/com/baeldung/compareyesterday/CompareYesterdayUnitTest.java +++ b/core-java-modules/core-java-date-operations-4/src/test/java/com/baeldung/compareyesterday/CompareYesterdayUnitTest.java @@ -5,6 +5,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.time.Instant; import java.time.LocalDate; +import java.time.ZonedDateTime; import java.time.temporal.ChronoUnit; import java.util.Calendar; import java.util.Date; @@ -47,9 +48,8 @@ public class CompareYesterdayUnitTest { void givenYesterdayDate_whenCompareWithDateMilliseconds_returnTrue() { // Create a Date object representing yesterday Date expectedDate = new Date(System.currentTimeMillis() - (1000 * 60 * 60 * 24)); - long yesterdayMidnightMillis = Instant.now() - .minus(1, ChronoUnit.DAYS) - .toEpochMilli(); + ZonedDateTime yesterdayMidnight = ZonedDateTime.now().minusDays(1).truncatedTo(ChronoUnit.DAYS); + long yesterdayMidnightMillis = yesterdayMidnight.toInstant().toEpochMilli(); boolean isEqualToYesterday = expectedDate.getTime() >= yesterdayMidnightMillis && expectedDate.getTime() < yesterdayMidnightMillis + 86_400_000; assertTrue(isEqualToYesterday); @@ -59,9 +59,8 @@ public class CompareYesterdayUnitTest { void givenTodayDate_whenCompareWithDateMilliseconds_returnFalse() { // Create a Date object representing yesterday Date expectedDate = new Date(); - long yesterdayMidnightMillis = Instant.now() - .minus(1, ChronoUnit.DAYS) - .toEpochMilli(); + ZonedDateTime yesterdayMidnight = ZonedDateTime.now().minusDays(1).truncatedTo(ChronoUnit.DAYS); + long yesterdayMidnightMillis = yesterdayMidnight.toInstant().toEpochMilli(); boolean isEqualToYesterday = expectedDate.getTime() >= yesterdayMidnightMillis && expectedDate.getTime() < yesterdayMidnightMillis + 86_400_000; assertFalse(isEqualToYesterday);