1

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

  • Hi I've added another question in this matter below, it was published as an answer cause I accidentally erased my cookies in the web browser thanks..

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
JavaSa
  • 27
  • 4
  • For your specific requirements, you definitely should use SwingWorker, because that's exactly what it's designed for. You cannot run a timer on the event dispatch thread, but you must update the UI only on the event dispatch thread. Coordination this is exacly what SwingWorker does. – Michael Borgwardt Aug 26 '11 at 13:54
  • thanks but somebody told me I can use the javax.swing.Timer class. This will fire action events at fixed intervals, which will prevent the GUI from becoming unresponsive. So can I solve it that way without SwingWorker? – JavaSa Aug 26 '11 at 14:55

5 Answers5

3
  1. This is the preferred way, since it separates the concerns of providing the code the thread should run (the job of the Runnable instance) and the job of managing the thread (the Thread instance), and also allows to have the Runnable be a subclass of something else
  2. Less clean, but works just as well
  3. "Worker Thread" is a conceptual name for threads that run parallel to the main application. SwingWorker 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.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • +1 - Spot on, especially for pointing out Executor. You need to be working up a level using java.util.concurrent classes. Read "Java Concurrency In Practice." – duffymo Aug 26 '11 at 01:24
2

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.

Alexis King
  • 43,109
  • 15
  • 131
  • 205
1

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.

  1. When you just want to run it in a separate thread. // The normal case
  2. When extending the thread class functions or behavior.
  3. Somebody else fill in :)

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.

Community
  • 1
  • 1
1

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.

Derek
  • 749
  • 1
  • 7
  • 19
0

You should not use SwingWorker unless you are using swing.

Implementing Runnable (option 1) is generally preferable to extending Thread.

Community
  • 1
  • 1
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • But why is implementing Runnable preferred over extending Thread? –  Aug 25 '11 at 21:09
  • Due to the semantics. You do not generally extend the threads behavior but rather use it as a runnable. –  Aug 25 '11 at 21:13
  • @pst, see http://stackoverflow.com/questions/541487/java-implements-runnable-vs-extends-thread/541506#541506 – Mike Samuel Aug 25 '11 at 21:35