Reputation: 27
I would like to know what are the differences between each of the following implementations for threads, and when should I use each option.
1- Implementing Runnable
public class ClientThread implements Runnable
{
new Thread(this).start();
public void run()
{...}
}
2- Extending Thread
class ServerThread extends Thread
{
this.start();
public void run()
{...}
}
3- Worker Threads and SwingWorker
which I'm really not familiar with...
Thank you very much
Okay guys thanks for all the info..
But, what should I use if I want to implement a count down timer for swing game which will run on screen parallel to the game without blocking the flow of the game because of the consistent timer in the background which will be shown there and probably will need to be run on the event dispatch thread...
Can I use the Runnable implementation or I must use swing worker?
Upvotes: 1
Views: 462
Reputation: 120496
You should not use SwingWorker
unless you are using swing.
Implementing Runnable
(option 1) is generally preferable to extending Thread
.
Upvotes: 0
Reputation: 346260
Runnable
instance) and the job of managing the thread (the Thread
instance), and also allows to have the Runnable
be a subclass of something elseSwingWorker
is a class designed to implement a worker tread in a Swing application. It offers an API for the worker thread to communicate its status to the Swing event thread so that it can e.g. update a progress bar.Note that it's generally very hard to work with threads manually. If you need multiple threads for performance reasons, it's much better to work with a thread pool via the Executors
class.
Upvotes: 3
Reputation:
A very near duplicate from "implements Runnable" vs. "extends Thread" except the third point which I'm really not familiar with either. I would recommend reading that post as it contains a lot of very good info.
Almost all of the time you should use #1 due to it's semantics. You use it as a runnable you do not generally extend the thread class functions.
Upvotes: 1
Reputation: 749
The official Java lesson on concurrency covers this very topic.
Defining and Starting a Thread
Which of these idioms should you use?
The first idiom, which employs a Runnable object, is more general, because the Runnable object can subclass a class other than Thread.
The second idiom is easier to use in simple applications, but is limited by the fact that your task class must be a descendant of Thread.
Upvotes: 1
Reputation: 43842
In general, it's preferred to implement Runnable rather than extend thread. There are a few reasons for this. First of all, since Java doesn't support multiple inheritance, sometimes extending Thread isn't an option. Also, from an OOP perspective, it usually makes more sense to implement Runnable instead; you're not adding functionality to the Thread class, you're creating something to be run.
However, if you actually are adding functionality to Thread (in which case you will probably need to make lots of these Thread subclasses), then just extend Thread.
Upvotes: 2