Pregunta

I am building a multi-threaded project in Java, I have entities and DAO packages to wrap the database tables and manipulating them. I have processing package which contains Runnables. The way i have implemented the Runnables so far is like this:

Class Thread1 implements Runnable{
  Thread t;
   parameters
  ...

  public Thread1(){
    t = new Thread(this,"Thread1");
    ....
    t.start();
  }

  public int method1(){
  ...
  return x;
  }

  public double method2(){
  ...
  return y;
  }

  public void run(){
    // some processing using DAO methods
    ....
    method1();
    ... 
    method2();
    ...
  }

}

The code works this way, but i need to use the same processing in the run() method as part of the processing in Thread2 class. The way i have structured my code does not allow to reuse the code. what would be a better structure to resolve this?

¿Fue útil?

Solución

You could either:

  1. Make Thread1 and Thread2 extend the same abstract base class, and move shared logic to the parent (inheritance).
  2. Create a common interface that both classes implement and that contains the common methods, then create a separate class that implements Runnable, and implement run() there (composition).

You should always favour composition over inheritance, so the second option is usually better, because it also gives you the flexibility of changing behaviour at runtime.

For example: Create a shared interface first

public interface SharedTask {
    public void method1();
    public void method2();
}

Make both classes implement it: public class Thread1 implements SharedTask and public class Thread2 implements SharedTask.

public class Worker implements Runnable {

    public Worker (SharedTask task) {
        this.task = task;
        ...
    }

    public void run() {
        task.method1();
        task.method2();
    }
}

Elsewhere in your code: new Worker().start();

Otros consejos

You almost never want to create and launch the Threads yourself. Just implement Runnable (or, if you prefer, Callable). Then submit them to an ExecutorService, which provides all sorts of cool options for how many threads it is running (and an nice central location to change this in the future should your needs change), tie-ins to a CompletionService, etc...

Create an abstract class that implements Runnable with the code you want in its run method - then extend that class as many times as you need the same functionality in your run method.

Before getting into the nifty details of multithreading: chances are that all your DAO calls will be run on one database connection, thus (almost) on one db thread. (especially if use some ORM framework, but a simple datasource pool will give you the same effect) You can definitely get rid of this issue, but it means that you have to use XA resources with two-phase commit to stay ACID compliant. That is a big chunk of work, so unless you're ready to do that, it's pointless to think about the appserver-side multithreading nuances.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top