• 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

Java Thread Question

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't quite understand the following Java Thread question posted at:

http://www.certgear.com/products/preview/SCJP/index.html?java
http://www.certgear.com/products/info/scjp.htm?java


My understanding is that the this.notify () method will un-block a thread that is waiting due to a this.wait (). However, this does NOT appear to be the case in the following thread code. Can someone explain?



---------------------------------------------------------------
Consider the following multi-threaded code that users inner-class declarations to create and execute multiple threads. What will be the output of the code?

1: public class test
2: {
3: public static void main (String args[])
4: {
5: final String s="s";
6: final Thread t= new Thread (new Runnable ()
7: {
8: public void run () {
9: try
10: {
11: for (int i=1; i < 5; i++)
12: {
13: synchronized (this)
14: {
15: System.out.print (" " + i);
16: this.wait ();
17: }
18: }
19: } catch (Exception ex) { ex.printStackTrace (); }
20: }
21: });
22:
23: Thread t2 = new Thread (new Runnable ()
24: {
25: public void run () {
26: while (true)
27: {
28: synchronized (this) { this.notify (); }
29: }
30: }
31: });
32: t.start ();
33: t2.start ();
34: }
35:}
A) A compilation error will occur
B) 1 2 3 4 will be printed
C) A run-time error will be thrown
D) Nothing will be printed to the output
E) 1 will be printed
[Ans: E]
[ February 24, 2005: Message edited by: Sunil Krishnamuthy ]
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to your code,
The thread t goes into runnable state and prints "1" before going to WAIT state. After that it starts waiting for the acquisition of its own lock.

The thread t2 then goes into runnable state too, and it calls notify on itself, notifying anyone waiting for the lock of this object i.e. t2
But in ur code no one is waiting for the acquisition of lock of the thread object t2. Instead u have thread t waiting for the acquisition of its own lock.

So after printing 1, nothing is printed. But remember here the code goes on for an infinite loop as u have


Now even if u comment the above line, i mean u have

Even then the code gets stuck as thread t is waiting for the acquistion of its own lock and no one is there to notify it.

Hope u getting my point.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic