Marco Zanini wrote: Can a thread have other methods to control his behaviour or it should have only the run method?
Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Ranganathan Kaliyur Mannar wrote:However, if you are talking about invoking those methods outside of the run() method, that means that particular Thread doesn't call those methods. You are propably calling those methods from main thread or some other thread - again, not right way of doing things.
Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Marco Zanini wrote:Yes, I agree too. But the methods I'm talking about are real operations that the object has to do.
Aditya Jha wrote:If the whole purpose of your class is just to provide the run() method, you may as well extend the Thread class, just like EventDispatchThread.
Stephan van Hulst wrote:
Aditya Jha wrote:If the whole purpose of your class is just to provide the run() method, you may as well extend the Thread class, just like EventDispatchThread.
No. This is a gross misuse of OO. As a matter of fact, I consider it an absolute blunder by the language designers that they had Thread implement Runnable. A Thread is in no way a Runnable.
Anyway, you have no business using the Thread class anyway, unless you need to use the interrupt() or sleep() methods. At the risk of sounding like a broken record, use ExecutorService and Future instead.
[edit]
Just never use Thread. You can replace interrupt() with Future.cancel(), join() with Future.get() and sleep() with a timer of some sort.
Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
When you pass a Sensor to a Thread and start it, the Sensor will start collecting and sending data. Now, later on in the thread you used to call the start() method, you can call the stop() method when you have enough data.
Marco Zanini wrote:2) I create an object of that class by calling the constructor, at this point in my program there are 2 objects running: the main class and the Runnable one.
Stephan van Hulst wrote:
Aditya Jha wrote:If the whole purpose of your class is just to provide the run() method, you may as well extend the Thread class, just like EventDispatchThread.
No. This is a gross misuse of OO. As a matter of fact, I consider it an absolute blunder by the language designers that they had Thread implement Runnable. A Thread is in no way a Runnable.
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|