BAEL-4297: fixed unsynchronized classes naming, added slf4j logging

This commit is contained in:
Maciej Glowka 2020-09-02 23:56:02 +02:00
parent 9d24bc1598
commit 3fd8926145
5 changed files with 30 additions and 14 deletions

View File

@ -1,6 +1,10 @@
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;
@ -16,10 +20,10 @@ public class SynchronizedReceiver implements Runnable {
data.wait();
this.message = data.receive();
} catch (InterruptedException e) {
e.printStackTrace();
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
e.printStackTrace();
log.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true;
}
}

View File

@ -1,6 +1,10 @@
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;
@ -18,10 +22,10 @@ public class SynchronizedSender implements Runnable {
data.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
e.printStackTrace();
log.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true;
}
}

View File

@ -1,11 +1,15 @@
package com.baeldung.exceptions.illegalmonitorstate;
public class UnSynchronizedReceiver implements Runnable {
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) {
public UnsynchronizedReceiver(Data data) {
this.data = data;
}
@ -15,10 +19,10 @@ public class UnSynchronizedReceiver implements Runnable {
data.wait();
this.message = data.receive();
} catch (InterruptedException e) {
e.printStackTrace();
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
e.printStackTrace();
log.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true;
}
}

View File

@ -1,10 +1,14 @@
package com.baeldung.exceptions.illegalmonitorstate;
public class UnSynchronizedSender implements Runnable {
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) {
public UnsynchronizedSender(Data data) {
this.data = data;
}
@ -17,10 +21,10 @@ public class UnSynchronizedSender implements Runnable {
data.notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
log.error("thread was interrupted", e);
Thread.currentThread().interrupt();
} catch (IllegalMonitorStateException e) {
e.printStackTrace();
log.error("illegal monitor state exception occurred", e);
illegalMonitorStateExceptionOccurred = true;
}
}

View File

@ -30,7 +30,7 @@ public class IllegalMonitorStateExceptionUnitTest {
void whenSyncSenderAndUnSyncReceiverAreUsed_thenIllegalMonitorExceptionShouldNotBeThrown() throws InterruptedException {
Data data = new Data();
UnSynchronizedReceiver receiver = new UnSynchronizedReceiver(data);
UnsynchronizedReceiver receiver = new UnsynchronizedReceiver(data);
Thread receiverThread = new Thread(receiver, "receiver-thread");
receiverThread.start();
@ -55,7 +55,7 @@ public class IllegalMonitorStateExceptionUnitTest {
Thread receiverThread = new Thread(receiver, "receiver-thread");
receiverThread.start();
UnSynchronizedSender sender = new UnSynchronizedSender(data);
UnsynchronizedSender sender = new UnsynchronizedSender(data);
Thread senderThread = new Thread(sender, "sender-thread");
senderThread.start();