File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread made out of Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread made out of Thread" Watch "Thread made out of Thread" New topic
Author

Thread made out of Thread

Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
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 ]

"If anything can go wrong, it will"
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Change realThread.start() to newThread.start()
Kaz, it compiles for me.
[ June 01, 2003: Message edited by: Marlene Miller ]
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63
it compiles but still not showing two print outs.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391

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

Joined: May 22, 2003
Posts: 63
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

Joined: Mar 05, 2003
Posts: 1391
Here is a suggestion:
System.out.println(Thread.currentThread().getName() + " This is runnning");
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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 ]
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
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)


not so smart guy still curious to learn new stuff every now and then
Jignesh Malavia
Author
Ranch Hand

Joined: May 18, 2001
Posts: 81
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
Hi Jignesh. I agree with you.
Kaz Yosh
Ranch Hand

Joined: May 22, 2003
Posts: 63

Exception in thread "main" java.lang.AssertionError: thread1 is dead
at SCJP.main(SCJP.java:17)
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
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

Joined: Mar 05, 2003
Posts: 1391
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

Joined: May 22, 2003
Posts: 63
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

Joined: Mar 05, 2003
Posts: 1391
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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread made out of Thread