Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
david eberhardt
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

/* 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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pallavi Chakraborty
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, This is really interesting.
Thank you very much , Jose
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic