• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread and Timer

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Chris Hani
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic