diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/Data.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/Data.java new file mode 100644 index 0000000000..b5867d15e7 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/Data.java @@ -0,0 +1,14 @@ +package com.baeldung.exceptions.illegalmonitorstate; + +public class Data { + private String message; + + public void send(String message) { + this.message = message; + } + + public String receive() { + return message; + } +} + diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedReceiver.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedReceiver.java new file mode 100644 index 0000000000..3dffb7b30a --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedReceiver.java @@ -0,0 +1,35 @@ +package com.baeldung.exceptions.illegalmonitorstate; + +public class SynchronizedReceiver implements Runnable { + private final Data data; + private String message; + private boolean illegalMonitorStateExceptionOccurred; + + public SynchronizedReceiver(Data data) { + this.data = data; + } + + @Override + public void run() { + synchronized (data) { + try { + data.wait(); + this.message = data.receive(); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + } catch (IllegalMonitorStateException e) { + e.printStackTrace(); + illegalMonitorStateExceptionOccurred = true; + } + } + } + + public boolean hasIllegalMonitorStateExceptionOccurred() { + return illegalMonitorStateExceptionOccurred; + } + + public String getMessage() { + return message; + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedSender.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedSender.java new file mode 100644 index 0000000000..04bac03e77 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/SynchronizedSender.java @@ -0,0 +1,33 @@ +package com.baeldung.exceptions.illegalmonitorstate; + +public class SynchronizedSender implements Runnable { + private final Data data; + private boolean illegalMonitorStateExceptionOccurred; + + public SynchronizedSender(Data data) { + this.data = data; + } + + @Override + public void run() { + synchronized (data) { + try { + Thread.sleep(1000); + + data.send("test"); + + data.notifyAll(); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + } catch (IllegalMonitorStateException e) { + e.printStackTrace(); + illegalMonitorStateExceptionOccurred = true; + } + } + } + + public boolean hasIllegalMonitorStateExceptionOccurred() { + return illegalMonitorStateExceptionOccurred; + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnSynchronizedReceiver.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnSynchronizedReceiver.java new file mode 100644 index 0000000000..a8e8befc4d --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnSynchronizedReceiver.java @@ -0,0 +1,33 @@ +package com.baeldung.exceptions.illegalmonitorstate; + +public class UnSynchronizedReceiver implements Runnable { + private final Data data; + private String message; + private boolean illegalMonitorStateExceptionOccurred; + + public UnSynchronizedReceiver(Data data) { + this.data = data; + } + + @Override + public void run() { + try { + data.wait(); + this.message = data.receive(); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + } catch (IllegalMonitorStateException e) { + e.printStackTrace(); + illegalMonitorStateExceptionOccurred = true; + } + } + + public boolean hasIllegalMonitorStateExceptionOccurred() { + return illegalMonitorStateExceptionOccurred; + } + + public String getMessage() { + return message; + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnSynchronizedSender.java b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnSynchronizedSender.java new file mode 100644 index 0000000000..eb6fa16649 --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/main/java/com/baeldung/exceptions/illegalmonitorstate/UnSynchronizedSender.java @@ -0,0 +1,31 @@ +package com.baeldung.exceptions.illegalmonitorstate; + +public class UnSynchronizedSender implements Runnable { + private final Data data; + private boolean illegalMonitorStateExceptionOccurred; + + public UnSynchronizedSender(Data data) { + this.data = data; + } + + @Override + public void run() { + try { + Thread.sleep(1000); + + data.send("test"); + + data.notifyAll(); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + } catch (IllegalMonitorStateException e) { + e.printStackTrace(); + illegalMonitorStateExceptionOccurred = true; + } + } + + public boolean hasIllegalMonitorStateExceptionOccurred() { + return illegalMonitorStateExceptionOccurred; + } +} diff --git a/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/exceptions/illegalmonitorstate/IllegalMonitorStateExceptionUnitTest.java b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/exceptions/illegalmonitorstate/IllegalMonitorStateExceptionUnitTest.java new file mode 100644 index 0000000000..800cdc4a7b --- /dev/null +++ b/core-java-modules/core-java-exceptions-3/src/test/java/com/baeldung/exceptions/illegalmonitorstate/IllegalMonitorStateExceptionUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung.exceptions.illegalmonitorstate; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class IllegalMonitorStateExceptionUnitTest { + + @Test + void whenSyncSenderAndSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldNotBeThrown() throws InterruptedException { + Data data = new Data(); + + SynchronizedReceiver receiver = new SynchronizedReceiver(data); + Thread receiverThread = new Thread(receiver, "receiver-thread"); + receiverThread.start(); + + SynchronizedSender sender = new SynchronizedSender(data); + Thread senderThread = new Thread(sender, "sender-thread"); + senderThread.start(); + + senderThread.join(1000); + receiverThread.join(1000); + + assertEquals("test", receiver.getMessage()); + assertFalse(sender.hasIllegalMonitorStateExceptionOccurred()); + assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred()); + } + + @Test + void whenSyncSenderAndUnSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldNotBeThrown() throws InterruptedException { + Data data = new Data(); + + UnSynchronizedReceiver receiver = new UnSynchronizedReceiver(data); + Thread receiverThread = new Thread(receiver, "receiver-thread"); + receiverThread.start(); + + SynchronizedSender sender = new SynchronizedSender(data); + Thread senderThread = new Thread(sender, "sender-thread"); + senderThread.start(); + + + receiverThread.join(1000); + senderThread.join(1000); + + assertNull(receiver.getMessage()); + assertFalse(sender.hasIllegalMonitorStateExceptionOccurred()); + assertTrue(receiver.hasIllegalMonitorStateExceptionOccurred()); + } + + @Test + void whenUnSyncSenderAndSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldBeThrown() throws InterruptedException { + Data data = new Data(); + + SynchronizedReceiver receiver = new SynchronizedReceiver(data); + Thread receiverThread = new Thread(receiver, "receiver-thread"); + receiverThread.start(); + + UnSynchronizedSender sender = new UnSynchronizedSender(data); + Thread senderThread = new Thread(sender, "sender-thread"); + senderThread.start(); + + + receiverThread.join(1000); + senderThread.join(1000); + + assertNull(receiver.getMessage()); + assertFalse(receiver.hasIllegalMonitorStateExceptionOccurred()); + assertTrue(sender.hasIllegalMonitorStateExceptionOccurred()); + } +}