Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WaitNNotifiyExample {
- public static void main(String[] args) {
- String lockObject = "lock Object";
- RunnerClass runnerClass_1 = new RunnerClass("First Thread", lockObject);
- RunnerClass runnerClass_2 = new RunnerClass("Second Thread", lockObject);
- new Thread(runnerClass_1).start();
- new Thread(runnerClass_2).start();
- try {
- Thread.currentThread().sleep(100);//To ensure all threads in waiting stage
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- synchronized (lockObject) {
- lockObject.notify();//Line number 15
- //lockObject.notifyAll();
- }
- }
- }
- class RunnerClass implements Runnable {
- public String runnerName;
- public String locObject;
- public RunnerClass(String runnerName, String lockObject){
- this.runnerName = runnerName;
- this.locObject = lockObject;
- }
- public void run() {
- try {
- synchronized (locObject) {
- System.out.println("The thread "+runnerName+" before wait executing");
- locObject.wait();
- System.out.println("The thread "+runnerName+" got notified");
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment