This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Looping tasks and thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Looping tasks and thread" Watch "Looping tasks and thread" New topic
Author

Looping tasks and thread

Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
Hello,

I have a big problem; I am new with thread (maybe little newer in Java), and I need to do some serious work, so I am asking for help; I need to do that on some click (by the user) web app create thread witch will run two tasks (for that user)and those two tasks will be go to sleep for some time (define by the user) and run again (each task has its own time). I will repeat: 1 user, 1 thread, two tasks with diferent time of executing, and when user come off, everything still runing (I have database, this is not a problem). And then second user come in he has option to create same thread with two same tasks (but he can set diferent times for each). Normally these two tasks write and read something in/from database for his user (but these isnt the point; let we have that task1 do system.out.println("I am task"+one+"from"+first+"user"). and task2 do system.out.println("I am task"+two+"from"+first+"user"), and so on for seconod user.. )

I cant figure out how can I loop that for eternaly, pass new threads, and acces particulary thread for deleting her (bay the user id). Maybe I need some vector lists, but I dont now how to use it. I have looked at some thread books, but nobody have my exaple.

Does anybody has some similar example code, so I can study it?
Tnx in advance
Kind regardes
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
See if Timer and TimerTask do what you want. You create a TimerTask and pass it to a Timer to schedule execution. You could create Task1 and Task2 to do whatever they want to do.

Then again, if this is like a school assignment and you're being graded on making your own mechanism to use a single thread for a number of deferred tasks, we can talk through duplicating some of what Timer does.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
I have found the solution

http://www.ssw.uni-linz.ac.at/Services/Docs/Tutorial/essential/threads/timer.html

tnx
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Yup, that's the stuff. I noticed it's not on a Sun site, though. You'd be more sure to find the very latest versions of things there. Google for "sun anything tutorial", eg "sun timer tutorial", almost always comes up a winner.

Let us know how it works out!
Marko Debac
Ranch Hand

Joined: Aug 21, 2006
Posts: 121
I have an error where it saying: <identifier> expected; what the hack is that means?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looping tasks and thread
 
Similar Threads
Thread pools, paced and exclusive tasks
Multiple Thread Pool Framework - HowTo
Thread and Timer
Timer Schedule
thread pooling