JavaSa
JavaSa

Reputation: 27

Multithreaded programming in Java -- diffences in approaches?

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

Answers (5)

Mike Samuel
Mike Samuel

Reputation: 120496

You should not use SwingWorker unless you are using swing.

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

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

  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.

Upvotes: 3

user425367
user425367

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.

  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.

Upvotes: 1

Derek
Derek

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

Alexis King
Alexis King

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

Related Questions