This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes here is a good practice question re: Threads (big topic on jdk 1.4 test) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "here is a good practice question re: Threads (big topic on jdk 1.4 test)" Watch "here is a good practice question re: Threads (big topic on jdk 1.4 test)" New topic
Author

here is a good practice question re: Threads (big topic on jdk 1.4 test)

david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158

/* Which statement is true of the code above?
1) it will cause a compile error.
2) compile succeeds; output is >Thread.currentThread() + "waiting for b to complete"
3) compile succeeds; output is >Thread.currentThread() + "waiting for b to complete"
and Thread.currentThread() + "Total is: " + b.total
4) runtime error; exception thrown is IllegalMonitorStateException
[ November 14, 2002: Message edited by: david eberhardt ]
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
answer is

C:\BIN>javac ThreadA.java
C:\BIN>java ThreadA
java.lang.IllegalMonitorStateException: current thread not ownerThread[main,5,ma
in]Waiting for b to complete ...
Exception in thread "main" at java.lang.Object.notify(Native Method)
at ThreadB.run(ThreadA.java:33)
java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:426)
at ThreadA.main(ThreadA.java:13)
C:\BIN>
david eberhardt
Ranch Hand

Joined: Jul 02, 2002
Posts: 158
here is how to fix the program:

here is the output :

C:\BIN>javac ThreadA.java
C:\BIN>java ThreadA
Thread[main,5,main]Waiting for b to complete ...
Thread[Thread-1,5,main]finishing up in the other thread now ...
Thread[main,5,main]Total is: 4950
C:\BIN>
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hello everybody,
i was going through the code in this post:
And I noticed a few things -
1.When is the run() executed ? Is it not directly after the start().
2.What is the purpose of synchronized(this).
3.A print statement says, "finshing up the other thread now". Is the other thread referring to the subclass thread ?
ThreadA is the main Thread and ThreadB is th child thread?
Thank you very much .
Pallavi
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
1) Exactly. "start" only prepares the JVM for executing a new thread. The precise moment "run" begins is up to scheduler. You cannot asume that run is started before the execution of the sentence next to "start".
2)A thread before tampering with the lock of an object must own it. This why wait and notify must be placed within synchronized blocks or methods that make sure the thread has adquired the lock of the object on which wait or notify are called:
The thread executing synchronized(this) adquires the lock of the object pointed by this (b). Then notify is called on this (*). Thus no IllegalMonitorStateException is thrown.
3) Yes, it is. The code a new thread executes begins with the code within run, and the print statement is there.
In the output you can see the names of such threads: main, and Thread-1
(*) In an instance method a call like notify() is the same as this.notify()
You can find interesting this material about threads:
The Java Tutorial


SCJP2. Please Indent your code using UBB Code
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Hello Jose,
Thank you very much. I got the concept now.
But I do have a question.
The synchronized method at both places is acting on an object of ThreadB. When within the class ThreadB, the parameter is this and in the other case an instance of ThreadB is created.
The thread which is working on the object b waits while the other thread in class ThreadB does its work. After completion theis thread notifies.
Am I thinking right ?
Pallavi
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
main thread starts a new Thread with "b.start()" and then waits for Thread-1 to notify it. However it could happpen that Thread-1 ends and notifies before main begins to wait (before it reaches b.wait(); ). In that case main will wait for ever. Though I think is not likely, you cannot ensure it will not happen.
Pallavi Chakraborty
Ranch Hand

Joined: Jan 18, 2003
Posts: 93
Wow, This is really interesting.
Thank you very much , Jose
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: here is a good practice question re: Threads (big topic on jdk 1.4 test)
 
Similar Threads
notify(); doesn't work?
wait and notify help needed
Question On Object Locks.
Multithreading: How can total be nearly 5,000 even though loop is up to 100?
Problem On using wait() in THreads