BAEL-4297: example of IllegalMonitorStateException
This commit is contained in:
+14
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+35
@@ -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;
|
||||
}
|
||||
}
|
||||
+33
@@ -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;
|
||||
}
|
||||
}
|
||||
+33
@@ -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;
|
||||
}
|
||||
}
|
||||
+31
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user