Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with multiple threads in one class?

Threads are often designed in two ways (see java tutorials): either by extending the Thread class or by implementing the Runnable class. Either way, you need to specify what will run inside the thread.

I designed a class, an adapter towards an online resource, that retrieves different kinds of information. This class consists of methods like getInformationOfTypeA() and getInformationOfTypeB(). Both contain code to connect to online resources, so both need to be threaded to avoid deadlocks.

The question is: how should I design this? I can do it like below, but then I can only implement one method:

public class OnlineResourceAdapter implements Runnable {

  public void run() {
      //get stuff from resource
      getInformationOfTypeA();
  }

  public static void main(String args[]) {
      (new Thread(new OnlineResourceAdapter ())).start();
  }

  public void getInformationOfTypeA(){
      //get information of type A
  }

  public void getInformationOfTypeB(){
      //get information of type B
  }

}

Another way would be by creating separate classes for each method, but that seems unnatural to me.

Btw: I'm developing my application in j2me

UPDATE:

Thanks to your responses I think it is most suiting to use something like the following as methods:

What do you think of this:

public class OnlineResourceAdapter{
    public void getInformationOfTypeA(){
        Thread t = new Thread(new Runnable() {           
            public void run() { 
                //do stuff here
            } 
        });
        t.start();
    }

    public void getInformationOfTypeB(){
        Thread t = new Thread(new Runnable() {           
            public void run() { 
                //do stuff here
            } 
        });
        t.start();
    }
}

What do you think of this?

like image 765
hsmit Avatar asked Jan 17 '10 16:01

hsmit


People also ask

How do you handle multi threading?

Java Thread class Java provides Thread class to achieve thread programming. Thread class provides constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface.

Can 2 threads run at the same time?

Within a process or program, we can run multiple threads concurrently to improve the performance. Threads, unlike heavyweight process, are lightweight and run inside a single process – they share the same address space, the resources allocated and the environment of that process.

Can we write multiple main thread in single class in Java?

Multiple Threads acting on Single objectIt is also possible to create two or more threads on a single object. Let's create a Java program in which three threads will share the same object (same run() method). Explanation: As you can observe that three threads are sharing the same run method.

Is it better to run one thread or multi thread on one?

So when processing a task in a thread is trivial, the cost of creating a thread will create more overhead than distributing the task. This is one case where a single thread will be faster than multithreading.


1 Answers

It sounds to me like you should actually have two different classes: InformationOfTypeAFetcher and InformationOfTypeBFetcher, each of which should implement Runnable. Each of them may have a reference to an instance of your OnlineResourceAdapter (or something similar) but if they're doing different things, they should be different classes.

like image 58
Jon Skeet Avatar answered Sep 22 '22 14:09

Jon Skeet