Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class test {
- static Integer foo = new Integer(1);
- public static void main(String[] args) {
- synchronized(foo) {
- foo++;
- foo.notifyAll();
- }
- System.err.println("Success");
- }
- }
- Exception in thread "main" java.lang.IllegalMonitorStateException
- at java.lang.Object.notifyAll(Native Method)
- at test.main(test.java:6)
- static Integer foo = new Integer(1);
- public static void main(String[] args) {
- synchronized (foo) {
- foo.notifyAll();
- }
- System.out.println("Success");
- }
- import java.util.concurrent.atomic.AtomicInteger;
- public class SynchronizeOnAPrimitive {
- static AtomicInteger foo = new AtomicInteger(1);
- public static void main(String[] args) {
- synchronized (foo) {
- foo.incrementAndGet();
- foo.notifyAll();
- }
- System.out.println("foo is: " + foo);
- }
- }
- foo is: 2
Add Comment
Please, Sign In to add comment