code_junkie

Why does synchronized notifyAll result in a IllegalMonitorStateException?

Nov 12th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. public class test {
  2. static Integer foo = new Integer(1);
  3. public static void main(String[] args) {
  4. synchronized(foo) {
  5. foo++;
  6. foo.notifyAll();
  7. }
  8. System.err.println("Success");
  9. }
  10. }
  11.  
  12. Exception in thread "main" java.lang.IllegalMonitorStateException
  13. at java.lang.Object.notifyAll(Native Method)
  14. at test.main(test.java:6)
  15.  
  16. static Integer foo = new Integer(1);
  17.  
  18. public static void main(String[] args) {
  19. synchronized (foo) {
  20. foo.notifyAll();
  21. }
  22. System.out.println("Success");
  23. }
  24.  
  25. import java.util.concurrent.atomic.AtomicInteger;
  26.  
  27. public class SynchronizeOnAPrimitive {
  28. static AtomicInteger foo = new AtomicInteger(1);
  29. public static void main(String[] args) {
  30. synchronized (foo) {
  31. foo.incrementAndGet();
  32. foo.notifyAll();
  33. }
  34. System.out.println("foo is: " + foo);
  35. }
  36. }
  37.  
  38. foo is: 2
Add Comment
Please, Sign In to add comment