• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread question from KB

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone make this code to execute the line 3(How to add a notify() to get out of the wait() other than put second number as wait method's argument)?

public class Q9_9 {
public static void main(String [] args) {
System.out.print("1 ");//line 1
synchronized(args){
System.out.print("2 ");//line 2
try {
args.wait();
} catch(InterruptedException e){}
}
System.out.print("3 ");//line 3
}
}

Thanks.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by liqiang yang:
Can anyone make this code to execute the line 3(How to add a notify() to get out of the wait() other than put second number as wait method's argument)?

public class Q9_9 {
public static void main(String [] args) {
System.out.print("1 ");//line 1
synchronized(args){
System.out.print("2 ");//line 2
try {
args.wait();
} catch(InterruptedException e){}
}
System.out.print("3 ");//line 3
}
}

Thanks.



You are asking args to wait without adding any code to notify. Line 3 would never reach unless you specify a timeout for the wait. I am not quite sure what you are asking for. Do you want to write code so that the wait() would escape with a notify ?
 
liqiang yang
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for my expression.

My question is:
1.If I want to print out "3 ", which means notify the thread(main thread) that is waiting on the object args, how to write code to do it?

2.I know after change into "args.wait(1000);", then at least 1 second later, program will execute line 3 (main thread wake up and coninute its flow), but how can I use "notify()/notifyAll()" to get main thread wake up and coninute its flow?

Ok! Hopefully, it's clear about my asking. Thanks.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1.If I want to print out "3 ", which means notify the thread(main thread) that is waiting on the object args, how to write code to do it?



Basically, you need to create another thread, that has access to args object -- which sometime after the main threads calls wait, grabs the synchronization lock for the args object, and calls notify().

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic