Buy my ebook for complete question bank. Buy from Google Books. Book you may be interested in.. Last updated 1 week ago. Subscribe to Interview Questions. Do you like cookies? Learn more I agree.
It makes our class tightly coupled with Thread. Multiple threads can share the same Runnable object. Runnable Tasks can be used in Executor Framework as well. This subclass should override the run method of the Thread class. An instance of the subclass can then be allocated and started.
The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started. Every thread has a name for identification purposes. If you have one thread working on a specific task, without ever changing that task, there is probably no point in making the tasks separate classes, and your code remains simpler. In the context of Java , since the facility is already there , it is probably easier to start directly with stand alone Runnable classes, and pass their instances to Thread or Executor instances.
Once used to that pattern, it is not harder to use or even read than the simple runnable thread case. One reason you'd want to implement an interface rather than extend a base class is that you are already extending some other class. You can only extend one class, but you can implement any number of interfaces. If you extend Thread, you're basically preventing your logic to be executed by any other thread than 'this'.
If you only want some thread to execute your logic, it's better to just implement Runnable. Can we re-visit the basic reason we wanted our class to behave as a Thread? There is no reason at all, we just wanted to execute a task, most likely in an asynchronous mode, which precisely means that the execution of the task must branch from our main thread and the main thread if finishes early, may or may not wait for the branched path task.
If this is the whole purpose, then where do I see the need of a specialized Thread. This can be accomplished by picking up a RAW Thread from the System's Thread Pool and assigning it our task may be an instance of our class and that is it. So let us obey the OOPs concept and write a class of the type we need.
There are many ways to do things, doing it in the right way matters. We do not want the thread's property, instead we want our class to behave as a task which can be run. Yes, If you call ThreadA call , then not need to call the start method and run method is call after call the ThreadA class only.
But If use the ThreadB call then need to necessary the start thread for call run method. If you have any more help, reply me. I find it is most useful to use Runnable for all the reasons mentioned, but sometimes I like to extend Thread so I can create my own thread stopping method and call it directly on the thread I have created.
Java does not support multiple inheritence so if you extends Thread class then no other class will be extended. For Example: If you create an applet then it must extends Applet class so here the only way to create thread is by implementing Runnable interface. Difference between Thread and runnable. If we are creating Thread using Thread class then Number of thread equal to number of object we created.
If we are creating thread by implementing the runnable interface then we can use single object for creating multiple thread. So single object is shared by multiple Thread. So it will take less memory. So depending upon the requirement if our data is not senstive.
So It can be shared between multiple Thread we can used Runnable interface. Adding my two cents here - Always whenever possible use implements Runnable. Below are two caveats on why you should not use extends Thread s. Ideally you should never extend the Thread class; the Thread class should be made final.
At least its methods like thread. See this discussion for a bug related to extending Thread s. Those who like to solve puzzles can see another side effect of extending Thread. The below code will print unreachable code when nobody is notifying them.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Asked 12 years, 9 months ago. Active 2 months ago. Viewed k times. Improve this question. İsmail Y. Thanks for this question, the answers cleared up a lot of misconceptions I had. Again, it's an idea, it might be useful in the right case, however I do not recommend it. Please see also the answer, nicely explained: stackoverflow. Are you trying to override the method? As per the code,class Thread A can extend any class whereas class Thread B cant extend any other class — mani deepak.
Show 8 more comments. Active Oldest Votes. Improve this answer. Jon Skeet Jon Skeet 1. Exactly, well put. What behavior are we trying to overwrite in Thread by extending it? I would argue most people are not trying to overwrite any behavior, but trying to use behavior of Thread. One minor advantage of Runnable is that, if in certain circumstances you don't care about, or don't want to use threading, and you just want to execute the code, you have the option to simply call run.
To paraphrase Sierra and Bates, a key benefit of implementing Runnable is that you are architecturally seperating the "job" from the "runner". Show 29 more comments. However, the caveat is important In general, I would recommend using something like Runnable rather than Thread because it allows you to keep your work only loosely coupled with your choice of concurrency. Bob Cross Bob Cross 22k 12 12 gold badges 55 55 silver badges 95 95 bronze badges.
This is better than the accepted answer IMHO. One thing: the snippet of code you have doesn't close down the executor and I see millions of questions where people get this wrong, creating a new Executor every time they want to spawn a task.
I don't disagree on the new Executor we do what you suggest in our code. In writing the original answer, I was trying to write minimal code analagous to the original fragment. We have to hope that many readers of these answers use them as jumping off points.
I'm not trying to write a replacement for the javadoc. I'm effectively writing marketing material for it: if you like this method, you should see all the other great things we have to offer! I know I'm a bit late commenting on this, but dealing with FutureTask directly is generally not what you want to do. Powerlord, my intention was to make code fragments that matched the OP's as closely as possible. I agree that new FutureTask isn't optimal but it is clear for the purposes of explanation.
Add a comment. Moral of the story: Inherit only if you want to override some behavior. Or rather it should be read as: Inherit less, interface more. This should always be the question if you start making a concurrent running Object!
Do you even need the Thread Object funtions? When inheriting from Thread, one nearly always wants to override the behavior of the run method. You cannot override the behavior of a java. Thread by overriding the run method. In that case you need to override the start method I guess. Normally you just reuse the behavior of the java. Thread by injecting your execution block in to the run method. The inheritance is not just for overriding some behavior, is also to use common behaviors.
And it is the opposite, the more overrides, the worser hierarchy. When you implement Runnable, you can save space for your class to extend any other class in the future or now. I hope this will help! Rupesh Yadav Rupesh Yadav Your code is patently wrong.
I mean, it does what it does, but not what you intended to show. To clarify: for the runnable case you've used the same ImplementsRunnable instance to start multiple threads, whereas for the Thread case you're creating different ExtendsThread instances which obviously leads to the behavior you showed. I don't yet understand your intent, but my point was that if you create multiple instances of ExtendsThread -- they will all return 1 as you've shown.
You can get the same results for Runnable by doing the same thing there, i. Given that your version of the code has Thread incrementing as well, is the statement by extending Thread, each of your threads has a unique object associated with it, whereas implementing Runnable, many threads can share the same object instance then wrong? If not, then what is a case which demonstrates this?
EvilWashingMachine: Been inactive for long.. I've added the object's hashcode to the print statements Show 13 more comments. Andrew Tobilko 45k 12 12 gold badges 80 80 silver badges bronze badges. Herms Herms Well, you can actually do the same thing with a Thread object too because Thread implements Runnable … ;- But it "feels better" doing this things with a Runnable than doing them with a Thread! True, but Thread adds a lot of extra stuff that you don't need, and in many cases don't want.
You're always better off implementing the interface that matches what you're actually doing. However, the significant difference is. Stuphan 21 7 7 bronze badges. Nidhish Krishnan Nidhish Krishnan 20k 6 6 gold badges 59 59 silver badges 73 73 bronze badges. I would say, there is only one way for multi-threading with two steps.
Let me make my point.
0コメント