timer or thread?

ok I would like to schedule a task that depends on a specific object, meaning there are x amount of objects, and they all have to perform a task once in a while, now i dont want to write the public void run() class x amount of times, since they all have to do the same thing but they do it for "their" objects. Each object has a field that is a queue so each object has to be different but do the same if you know what i mean.

So, my question is, is there a way to pass an object with the timer? So i want to do something like this.

timer =new Timer();

timer.schedule(new Task(),1*1000);

class Taskextends TimerTask{

publicvoid run(){

// in here i want to know which object invoked the timer...

}

}

and secondly should i use timers or threads?

Thanks

[1154 byte] By [hochia] at [2007-10-2 6:50:02]
# 1
Can you give some more information about what exactly you're trying to do?
tjacobs01a at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 2

If they have to do the same thing, then you don't need to write run() multiple times. Write it once, and have it operate on different objects.

I think this could be the strategy pattern. You can do the same thing whether Timer or Runnable. Which one you "should" do I can't say.

class Foo implements Runnable or Extends TimerTask {

Something operand;

Foo(Something operand) {

this.operand = operand;

}

public void run() {

doStuffWith(operand);

}

}

jverda at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 3

ok i have an object, a node, which has a queue of messages. Now i have around 1000 nodes which in a periodic manner create messages to pass around in the system. So i thought if i create the timer class for this task and then somehow know which node created the message(the task that will be carried out is creation or passing on of messages)

so that a message can be added to the queue of that node.

So when the timer expires i need to know which node created that message, am i clear?

hochia at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 4

> ok i have an object, a node, which has a queue of

> messages. Now i have around 1000 nodes which in a

> periodic manner create messages to pass around in the

> system. So i thought if i create the timer class for

> this task and then somehow know which node created

> the message(the task that will be carried out is

> creation or passing on of messages)

> so that a message can be added to the queue of that

> node.

> So when the timer expires i need to know which node

> created that message, am i clear?

Um, no. Not to me at least.

But if you want to know "which node created the message" then pass the node to the message's ctor. Or pass both the node and the message (or an object that wraps them both up) to the ctor or a method of whatever is going to handle them. Or rather than passing messages around the system, pass around objects that contain both a message and the node that created it.

jverda at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 5

I'd create some sort of singleton class (notice my alliteration?) for sending the messages. Create a queue in the singleton class for the messages, and make sure that adding/removing messages from the queue is thread-safe. One thread should be sufficient to run the entire messaging system then.

tjacobs01a at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 6

The problem is not that im not sure how to create the messages and such but rather that when a message is created the way it propergates through the system is time based. And at each node some variables for that message change, so I would need to know which message caused the timer to expire, not that any message did it.

So i was thinking of something where i can pass an object together with the timer or thread so that its easily identifyable...

hochia at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 7

ok now i have this.

Timer timer[] = new Timer[10];

for (int i = 0; i<10;i++){

timer[i] = new Timer(Integer.toString(i));

timer[i].schedule(new Task(), 10000);

}

}

class Task extends TimerTask{

public void run(){

System.out.println(this.getName());

}

}

is fails because it doesnt know what this.getName() should do. It kind makes sense that it doesnt find it since its for the timer object. But how can i tell in the run part which timer it was that made it go there...

thanks

hochia at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 8

For one thing, use the Schedulers from [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html]java.util.concurrent[/url]. This way you don't have to extend TimerTask for your tasks, just implement Runnable. Also the scheduler is much more flexible allowing callbacks and return values (See [url http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html]Future<V>[/url]).

Caffeine0001a at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 9

> ok now i have this.

> >Timer timer[] = new Timer[10];

>for (int i = 0; i<10;i++){

>timer[i] = new Timer(Integer.toString(i));

>timer[i].schedule(new Task(), 10000);

>}

>

> }

> class Task extends TimerTask{

>public void run(){

>System.out.println(this.getName());

>}

> }

>

Just as comment to your code above. Every Timer class has one thread associated that it schedules the task on. If you create 10 Timers you have 10 threads.With your code above then you have 1 task for each timer (and thread). You might as well have just created 10 threads and not used the Timer class at all. Usually you create 1 Timer class and scheduler multiple Timer Tasks on it. That way all the tasks share one thread and you save the time needed to create and destroy a thread with each task. If you need higher concurrency you could create 1 or more additionaly Timers, but if you going to that complexity, use the java.concurrent packaged I suggested in my earlier post.

Caffeine0001a at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 10

Maybe this is what you want?

Task task[] = new Task[10];

Timer timer = new Timer(Integer.toString(i));

for (int i = 0; i<10;i++){

task[i] = new Task(Integer.toString(i));

timer.schedule(task[i], 10000);

}

}

class Task extends TimerTask{

String name;

Task(String name) {

this.name = name;

}

public void run(){

System.out.println(getName());

}

public String getName() {

return name;

}

}

Note: This was hand typed and not checked with a compiler

Caffeine0001a at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...
# 11
thank you, had to change the name of the timer, but other than that its working. I actually never understood the idea behind this. but now it actually makes more sense...Thanks
hochia at 2007-7-16 13:58:59 > top of Java-index,Java Essentials,Java Programming...