Merge pull request #9965 from maciejglowka/BAEL-4297

BAEL-4297: example of IllegalMonitorStateException
This commit is contained in:
bfontana 2020-09-02 21:23:29 -03:00 committed by GitHub
commit 3a2853cdf6
6 changed files with 232 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,39 @@
package com.baeldung.exceptions.illegalmonitorstate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SynchronizedReceiver implements Runnable {
private static Logger log = LoggerFactory.getLogger(SynchronizedReceiver.class);
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) {
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true;
}
}
}
public boolean hasIllegalMonitorStateExceptionOccurred() {
return illegalMonitorStateExceptionOccurred;
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.exceptions.illegalmonitorstate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SynchronizedSender implements Runnable {
private static Logger log = LoggerFactory.getLogger(SynchronizedSender.class);
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) {
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true;
}
}
}
public boolean hasIllegalMonitorStateExceptionOccurred() {
return illegalMonitorStateExceptionOccurred;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.exceptions.illegalmonitorstate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UnsynchronizedReceiver implements Runnable {
private static Logger log = LoggerFactory.getLogger(UnsynchronizedReceiver.class);
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) {
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true;
}
}
public boolean hasIllegalMonitorStateExceptionOccurred() {
return illegalMonitorStateExceptionOccurred;
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.exceptions.illegalmonitorstate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UnsynchronizedSender implements Runnable {
private static Logger log = LoggerFactory.getLogger(UnsynchronizedSender.class);
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) {
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
log.error("illegal monitor state exception occurred", e);
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());
}
}