• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

synchronized block?

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true. Select the two correct answers.

a. The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state.
b. The wait(), notify(), and notifyAll() methods must be executed in synchronized code.
c. The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.
d. The Thread class is an abstract class.

the answer is b and c. I don't know why b is correct.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From API:
wait()
The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

notify()
This method should only be called by a thread that is the owner of this object's monitor. A thread becomes the owner of the object's monitor in one of three ways:

* By executing a synchronized instance method of that object.
* By executing the body of a synchronized statement that synchronizes on the object.
* For objects of type Class, by executing a synchronized static method of that class.

Only one thread at a time can own an object's monitor.

notifyAll:
This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.
 
jaman tai
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but why i got no compilation error with the following code. can you give me any example?

class T1 extends Thread{
public void run1(){ //no synchronized
System.out.println("T1 Run");
try{
wait();
System.out.println("Wait end");
}
catch(InterruptedException ie){

}
}
}

class T2 extends Thread{
public void run1(){ //no synchronized
System.out.println("T2 Run");
try{
notifyAll();
System.out.println("notifyAll end");
}
catch(Exception ie){

}
}
}
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll never get a compilation error by having a wait() or notify() statement because checking that the calling Thread hold the object's monitor is done at runtime. In your case you'll get (at Runtime) a java.lang.IllegalMonitorStateException: current thread not owner.



In this ewemple, calling w() on a A object will cause an exception, calling s() will not because due to the synchronized block, the Thread will hold the monitor on the object.
 
jaman tai
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, good reply. thanx a lot!
 
Sara Olsen
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiles does not check this, but you will get an IllegalMonitorStateException at runtime.
Please refer to API: http://java.sun.com/j2se/1.5.0/docs/api/index.html
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic