BAEL-4297: example of IllegalMonitorStateException

This commit is contained in:
Maciej Glowka 2020-09-01 21:15:01 +02:00
parent ba38797af0
commit 9d24bc1598
6 changed files with 216 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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());
}
}