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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With