illegalmonitorstateexcep

Threads synchronizing in Java, IllegalMonitorStateException

左心房为你撑大大i 提交于 2019-12-13 07:04:51
问题 I am trying to synchronize two threads - the "Main" thread, and a runnable. I get the IllegalMonitorStateException, but I do not completelty understand what "you do not have the lock of the object" means. Here is my code: public class ThreadsTest { private static ThreadsTest instance; public volatile boolean flag = false; public void doStuff() { System.out.println("first"); this.flag = true; } public Runnable mDrawer = new Runnable() { public void run() { synchronized (ThreadsTest.getInstance

IllegalMonitorStateException notify() and wait() [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-04 22:33:21
问题 This question already has answers here : Java Wait and Notify: IllegalMonitorStateException (2 answers) Closed last year . I have a problem. When I use notify() in synchronized block I have IllegalMonitorStateException. Can anyone help me to solve this problem? I have to do that, one thread will send to second thread char, then this thread has to wait and second thread print this char. After that second thread wait, and first one again send next char Main.java: import java.util.logging.Level;

IllegalMonitorStateException in code

落花浮王杯 提交于 2019-12-01 11:21:54
问题 class Test { public static void main(String[] args) { System.out.println("1.. "); synchronized (args) { System.out.println("2.."); try { Thread.currentThread().wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("3.."); } } } I am getting IllegalMonitorStateException monitor exception in this code. As per my understanding, because of synchronized block around args which is string array object, current thread must have

Why does notifyAll() raise IllegalMonitorStateException when synchronized on Integer?

自闭症网瘾萝莉.ら 提交于 2019-11-28 05:43:35
Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test { static Integer foo = new Integer(1); public static void main(String[] args) { synchronized(foo) { foo++; foo.notifyAll(); } System.err.println("Success"); } } Result: Exception in thread "main" java.lang.IllegalMonitorStateException at java.lang.Object.notifyAll(Native Method) at test.main(test.java:6) You have noted correctly that notifyAll must be called from a synchronized block. However, in your case, because of auto-boxing, the object you synchronized on is not the same instance that you

IllegalMonitorStateException on notify() when synchronized on an Integer

冷暖自知 提交于 2019-11-28 02:17:32
I'm new to using wait() and notify() in Java and I'm getting an IllegalMonitorStateException. Main Code public class ThreadTest { private static Integer state = 0; public static void main(String[] args) { synchronized(state) { System.out.println("Starting thread"); Thread t = new Thread(new AnotherTest()); t.start(); synchronized(state) { state = 0; while(state == 0) { try { state.wait(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("State is: " + state); } } } public static class AnotherTest implements Runnable {

Why does notifyAll() raise IllegalMonitorStateException when synchronized on Integer?

房东的猫 提交于 2019-11-27 01:01:51
问题 Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test { static Integer foo = new Integer(1); public static void main(String[] args) { synchronized(foo) { foo++; foo.notifyAll(); } System.err.println("Success"); } } Result: Exception in thread "main" java.lang.IllegalMonitorStateException at java.lang.Object.notifyAll(Native Method) at test.main(test.java:6) 回答1: You have noted correctly that notifyAll must be called from a synchronized block.

IllegalMonitorStateException on notify() when synchronized on an Integer

笑着哭i 提交于 2019-11-26 22:12:44
问题 I'm new to using wait() and notify() in Java and I'm getting an IllegalMonitorStateException. Main Code public class ThreadTest { private static Integer state = 0; public static void main(String[] args) { synchronized(state) { System.out.println("Starting thread"); Thread t = new Thread(new AnotherTest()); t.start(); synchronized(state) { state = 0; while(state == 0) { try { state.wait(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System