• 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

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

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, This is really interesting.
Thank you very much , Jose
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic