/*
* i dont understand totally what your point is, but assume
* we have 3 classes that needs to use one Runnable task ...
* Threads are Runnable ... any way, so
*/
// create your runnable task
class ReadDataOnline implements Runnable{
@Override
public void run() {
// read data from the internet and update something or what ever
}
}
// create your classes that will use this task
class ClassOne{
private ReadDataOnline runnable = null;
private Thread reader = null;
ClassOne(ReadDataOnline runnable){
this.runnable = runnable;
reader = new Thread(runnable);
}
void useTask(){
// start your new Thread in the background, which will use
// the Runnable task in the parameter
reader.start();
}
}
class ClassTwo {
private ReadDataOnline runnable = null;
private Thread reader = null;
ClassTwo(ReadDataOnline runnable) {
this.runnable = runnable;
reader = new Thread(runnable);
}
void useTask() {
// start your new Thread in the background, which will use
// the Runnable task in the parameter
reader.start();
}
}
class ClassThree {
private ReadDataOnline runnable = null;
private Thread reader = null;
ClassThree(ReadDataOnline runnable) {
this.runnable = runnable;
reader = new Thread(runnable);
}
void useTask() {
// start your new Thread in the background, which will use
// the Runnable task in the parameter
reader.start();
}
}
class MainClass{
public void doStuff(){
ReadDataOnline runnable = new ReadDataOnline();
// use the same runnable task in the three classes
ClassOne classOne = new ClassOne(runnable);
ClassTwo classTwo = new ClassTwo(runnable);
ClassThree classThree = new ClassThree(runnable);
// let classOne start using the Task
classOne.useTask();
// now the Task status is updated/modified by classOne,
// let classTwo use the task in it's new state
classTwo.useTask();
// and so on
classThree.useTask();
/*
* since we are not talking about a specefic case, i cant tell
* what is the task or what to do with it, but put in mind that
* synchronizing your methods is very important since multi-threading
* doesnt guarantee the order of the threads, and also they wont
* be synchronized with each other unless your methods are synchronized
* (or you use synchronization blocks)
*/
// i know this example isnt very good, but at least it shows how to use
// the same task in multiple Threads or classes that are backed with
// different threads
}
}