File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes synchronized block? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "synchronized block?" Watch "synchronized block?" New topic
Author

synchronized block?

jaman tai
Ranch Hand

Joined: Sep 26, 2005
Posts: 37
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.
Sara Olsen
Ranch Hand

Joined: Oct 12, 2005
Posts: 59
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

Joined: Sep 26, 2005
Posts: 37
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){

}
}
}
Seb Mathe
Ranch Hand

Joined: Sep 28, 2005
Posts: 225
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.


Regards,<br />Seb<br /> <br />SCJP 1.4
jaman tai
Ranch Hand

Joined: Sep 26, 2005
Posts: 37
oh, good reply. thanx a lot!
Sara Olsen
Ranch Hand

Joined: Oct 12, 2005
Posts: 59
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
 
jQuery in Action, 2nd edition
 
subject: synchronized block?