This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, A Thread(say, t) that has been issued a "t.start()" runs the thread once and completes the thread to 'dead' state. Now, when we again issue a "t.start()", there is no compiler error but a IllegalThreadStateException is thrown at runtime. My Questions are:
1. When the 'start' method calls 'run' internally, why does multiple 't.run()' methods when issued on the code, doesnt produce the same exception, BUT RUNS THE CODE PERFECTLY?
2. Javac being very efficient COMPILER, why CAN'T the compiler developers trap this error at compile time itself? [ October 04, 2004: Message edited by: Manikandan Jayaraman ]
Originally posted by Manikandan Jayaraman: 1. When the 'start' method calls 'run' internally, why does multiple 't.run()' methods when issued on the code, doesnt produce the same exception, BUT RUNS THE CODE PERFECTLY?
Because calling run directly doest execute it in a different thread.
2. Javac being very efficient COMPILER, why CAN'T the compiler developers trap this error at compile time itself?
Because in many cases it is simply not possible to know at compile time.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
If you must have some code that runs on a thread and can be run more than once, you can create a new Thread with an existing Runnable.
Or you can do some cool stuff with making the Thread wait instead of ending. Thread pools do this and there have been neat posts in the past about doing it outside of a pool. This shows how a thread pool might wait for the next available command to execute and you could send the same command twice:
If either of those approaches sound useful we can get into more details.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com