wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Thread and Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread and Timer" Watch "Thread and Timer" New topic
Author

Thread and Timer

Chris Hani
Ranch Hand

Joined: Jun 03, 2004
Posts: 42
I have been a new project for a month.

I have seen a little server program that is designed to multi thread but using timer to repeat jobs. The server basically looking for some records that a flag has been set as 1 and creat one thread per record. Inside the thread, it will modify the flag setting as 0, fetch another record from another table, create a brand new record and so on.


Rough structure of this program is

In my understanding class A is scheduled to be executed every 5000 milisecond. First and foremost, am I right?

I think it is NOT a good design.
I have seen that the main program rescheduled and start to execute before a thread finished. The problem is the most recent rescheduled task interfere the work from thread. The rescheduled task try to fetch same record that is already under work for a thread.

So I modified like this

At first remove class A
Design class B as following

My brand new server didn't accept from the tech leader since the previous one is already running for 3 years.

I think the issue here is I use infinity loop for main program and current server is using Timer for it.

What you guys think? Do you have any valuable link for this?

[ December 12, 2006: Message edited by: Chris Hani ]



[HENRY: Added Code Tags]
[ December 12, 2006: Message edited by: Henry Wong ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I think your extends relationship is just confusing things. Let's work on the Timer approach a bit more.

First write a TimerTask and schedule it for execution.

That will execute your task every 5 seconds as you want. It will run on the Timer's thread so you don't have to start one yourself.

So what should your task do? If your database update is very fast or if there are no other tasks scheduled you can just do all the work right there in the run() of your TimerTask. If the Timer is managing a bunch of other tasks you want to get the database update off the Timer thread and into its own thread. So one of these two things should happen:

Does that make sense?

BTW: If you start a new thread and your database update runs longer than your repeat time (5 seconds) you can run into database concurrency problems. Maybe set the repeat time higher or get rid of the new thread idea.
[ December 13, 2006: Message edited by: Stan James ]

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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18123
    
  39

In my understanding class A is scheduled to be executed every 5000 milisecond. First and foremost, am I right?

I think it is NOT a good design.
I have seen that the main program rescheduled and start to execute before a thread finished. The problem is the most recent rescheduled task interfere the work from thread. The rescheduled task try to fetch same record that is already under work for a thread.


The period specified by the schedule() method is the period between executions. So whether the task takes a second, or a minute, or an hour, the next run of the task will, at earliest, take place 5 seconds after the previous runs finishes. No overlap. No interference between runs.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Chris Hani
Ranch Hand

Joined: Jun 03, 2004
Posts: 42
James,

Your second code is exactly same as the current server does.
It is working fine because the timer is set longer that the database update thread's execution time. I think I understand that design.

What I did was I completely change the design.
I didn't use the Timer at all. Instead I use infinite loop.

My question is
1. Above two what is the popular( or more likely correct in the point of design) approach to desing multi thread server program in the real world?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread and Timer
 
Similar Threads
thread started a process, now what?
Threads doubt
Ending a thread from jsp
Timer Rescheduling problem
How to stop a Timer after it's run more than once?