• 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

Thread made out of Thread

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the Osborne Java2 certified programmer and developer book, Thread can be made out of Thread instance because Thread implements Runnable. Then why the code below won't compile if uncomment Line 1?

[ June 01, 2003: Message edited by: Kaz Yosh ]
[ June 01, 2003: Message edited by: Kaz Yosh ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change realThread.start() to newThread.start()
Kaz, it compiles for me.
[ June 01, 2003: Message edited by: Marlene Miller ]
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it compiles but still not showing two print outs.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what is happening is the second thread is given the first thread as the Runnable object, so the second thread executes the run method of the first thread. The run method of the first thread does nothing. public void run() {}
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

thread subclass A is running
thread subclass B is running
Hmm. I am going to have to think about this one for a while.
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is funny but
thread.start();
newThread.start();
Prints:This is running
//thread.start();
newThread.start();
Prints:This is running
thread.start();
//newThread.start();
Prints:This is running
I know thread instance cant call start method twice but they are technically different instances. This is very interesting. I hope this will not be on the exam this friday.
[ June 01, 2003: Message edited by: Kaz Yosh ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a suggestion:
System.out.println(Thread.currentThread().getName() + " This is runnning");
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, this is a really interesting problem!
Do this: reverse the order.
newThread.start();
thread.start();
Now you will get
Thread-2 This is runnning
Thread-1 This is runnning
(Kaz, we'll figure this out before Friday. Maybe even before the end of today.)
[ June 01, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when put Thread.sleep(100) in the run()
I always get two output printing regardless the order.
Seems to me the newThread depends on the previous thread. (ie, if it can get cpu time beore thread-1 finished ... then we see the output from newThread)
 
Author
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If I keep the run() method in class B, it prints "thread subclass B is running"
But if I comment out that run() method in class B, it prints "thread subclass A is running"
So I am assuming that the start() method of the Thread class calls its own run() method which in turn calls the run() method of the supplied target. Something like this

When B overrides run(), the start() method actaully calls the overridden run() method of class B and not that of Thread. Thus, the run() method of target is never called. (but i 'm not sure)
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a few pieces of code from class Thread

I have a �suspicion� which I am trying to verify that when the run method returns, the VM sets the target to null.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am going to make the bold assumption that when the run method completes, the Java virtual machine sets the target reference in the Thread object to null.

Here is what is happening:
Thread-1 object has a target variable that refers to the Runnable subclass object.
Thread-2 object has a target variable that refers to the Thread-1 object.
Thread-1 is started.
Thread-2 is started.
The JVM invokes thread1.run() which invokes target.run(). The JVM executes the run method of the Runnable object. When run() completes, the JVM sets the target variable in Thread1 to null.
The JVM invokes thread2.run() which would invoke target.run(), except the target is null. Instead thread2.run() returns.

*If* you add Thread.sleep(1000) to the run method, then what happens instead is, while Thread1 is sleeping, the JVM invokes thread2.run() which invokes target.run(). The JVM executes the run method of Thread1 which invokes its target.run(). The JVM executes the run method of the Runnable object.
It is also possible to explain the output when the starts are reversed. Try it!
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jignesh. I agree with you.
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Exception in thread "main" java.lang.AssertionError: thread1 is dead
at SCJP.main(SCJP.java:17)
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a time line.

At time t1 the main thread is the current running thread.
The JVM executes the thread.start() and schedules Thread-1.
At time t2 the main thread is still the current running thread.
Thread-1 is in the queue of runnable threads.
Thread-1 is not alive.
At time t3 the JVM swaps out the main thread.
The JVM selects Thread-1 as the current running thread.
Now Thread-1 is alive.
The JVM executes the run method of Thread-1.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kaz, I ran your program with the assert statements. I did not get an assertion error. I got the output: This is running
 
Kaz Yosh
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is all about timing thread scheduler decides. and it all depends on the JVM. I tested the code with assertion on java 1.4 on Mac OSX and java 1.4 on Win XP. and result is different. What is your environment?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My environment is Windows 2K and SDK 1.4.1
Yes, I think the OS and the scheduler affect the result. I think it is a timing issue. It depends on when the thread becomes alive after you call start(). There is a window of time, where the thread is not alive (but it is not dead, it is waiting to become alive).
 
reply
    Bookmark Topic Watch Topic
  • New Topic