Last active
October 17, 2015 12:30
-
-
Save codinko/f1d65b11a5ad8f6ec840 to your computer and use it in GitHub Desktop.
Threads and Locks - Object.wait() method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.codinko.example; | |
/** | |
* | |
* Feel free to play around with this code to try various topics covered under Threads & Locks: | |
* | |
* Theory reference: https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html | |
* | |
*/ | |
public class ThreadWaitDemo { | |
Employee employeeObj1 = new Employee(); | |
Runnable r1 = new Runnable() { | |
@Override | |
public void run() { | |
System.out.println(String.format("running thread : %s", Thread.currentThread().getName())); | |
/* | |
* try { employeeObj1.wait(); // I expect // "Exception in thread "Thread-0 | |
* " java.lang.IllegalMonitorStateException" // and u'll get it :) | |
*//** | |
* Let thread t be the thread executing the wait method on object | |
* m, and let n be the number of lock actions by t on m that have | |
* not been matched by unlock actions. One of the following actions | |
* occurs: | |
* | |
* If n is zero (i.e., thread t does not already possess the lock | |
* for target m), then an IllegalMonitorStateException is thrown. | |
* | |
*/ | |
/* | |
* } catch (InterruptedException e) { e.printStackTrace(); } | |
*/ | |
employeeObj1.changeMyName("Harley"); | |
} | |
}; | |
Runnable r2 = new Runnable() { | |
@Override | |
public void run() { | |
System.out.println(String.format("running thread : %s", Thread.currentThread().getName())); | |
//employeeObj1.notify(); //Exception in thread "Thread-1" java.lang.IllegalMonitorStateException | |
employeeObj1.changeMyName("Harley"); | |
} | |
}; | |
public static void main(String[] args) { | |
new ThreadWaitDemo().startThread(); | |
} | |
public void startThread() { | |
new Thread(r1).start(); | |
new Thread(r2).start(); | |
} | |
class Employee { | |
String name; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public synchronized void changeMyName(String newName) { | |
try { | |
employeeObj1.wait(5000); | |
//employeeObj1.wait(); // wait for ever if wait() or wait for milliseconds if wait(long ms) . this time there is no | |
// java.lang.IllegalMonitorStateException as thread | |
// has the lock on employeeObj1's monitor | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(String.format("Changing the name to : %s by thread : %s ", newName, Thread.currentThread().getName())); | |
this.name = newName; | |
//employeeObj1.notify(); | |
} | |
} | |
} | |
//OUTPUT | |
/* | |
running thread : Thread-0 | |
Exception in thread "Thread-1" java.lang.IllegalMonitorStateException | |
running thread : Thread-1 | |
at java.lang.Object.notify(Native Method) | |
at com.example.compare.ThreadWaitDemo$2.run(ThreadWaitDemo.java:37) | |
at java.lang.Thread.run(Thread.java:662) | |
Changing the name to : Harley by thread : Thread-0 | |
*/ | |
/* | |
running thread : Thread-0 | |
running thread : Thread-1 | |
Changing the name to : Harley by thread : Thread-0 | |
Changing the name to : Harley by thread : Thread-1 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment