Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

blocking pattern

Thread view: 
richnjones@gmail.com - 30 Jan 2007 18:53 GMT
Hi there,

Ive written some code that works but Im not too happy with. Can anyone
give me some advice on how to make it better.

Problem:

At time = X call method A()
methodA() returns straight away
Wait for time = Y
call methodB()

Solution:

So what Ive done is:

while (notAtTimeX()){
  sleep(10ms)
}
methodA()
while (notAtTimeX()){
  sleep(10ms)
}
methodB()

Because methodA() doesnt block I have to these horrible while loops
that query the clock and sleep until it gets to the appropriate time.

I was thinking maybe using wait() and notify() might be a better
pattern. I hope this is clear. Ive simplified the problem  a bit but
hopefully it will still be relevant for my problem

What do you think?

Thanks

R
Dag Sunde - 30 Jan 2007 21:11 GMT
> Hi there,
>
[quoted text clipped - 27 lines]
> pattern. I hope this is clear. Ive simplified the problem  a bit but
> hopefully it will still be relevant for my problem

It might be overkill, but take a look at "Quartz":
http://www.opensymphony.com/quartz/

Signature

Dag.

Eric Sosman - 30 Jan 2007 22:07 GMT
richnjones@gmail.com wrote On 01/30/07 13:53,:
> Hi there,
>
[quoted text clipped - 29 lines]
>
> What do you think?

   I think you should consider java.util.Timer.  (If after
considering it you reject it, explain your reasons in your
follow-up.)

Signature

Eric.Sosman@sun.com

Daniel Pitts - 30 Jan 2007 23:52 GMT
On Jan 30, 10:53 am, richnjo...@gmail.com wrote:
> Hi there,
>
[quoted text clipped - 33 lines]
>
> R

Lets think about this a little bit...

I'm guessing methodA kicks off some Thread and returns immediately,
but you can to call methodB sometime after that Thread finishes?
If you could have methodA return the Thread object, you can call
Thread.join().

You should also look into java.util.Timer to call methodA at a
specific time. Or, at the very least, adjust your first while loops to
sleep for (timeX - currentTime) instead of 10ms.
richnjones@gmail.com - 31 Jan 2007 21:32 GMT
> On Jan 30, 10:53 am, richnjo...@gmail.com wrote:
>
[quoted text clipped - 48 lines]
>
> - Show quoted text -

Thanks for the replies so far. At the moment I am using timers. I just
wasnt sure it was the best way of doing it. The multiple thread thing
I think we make matters more complicated. It is probably not apparent
because of the simplicity of my example.
Are timers the best way ahead? I need to query the computer time to
know when to stop. The while loops with sleeps in just seems to ugly.
Am I missing the use of timers here? I will try and put a better
example of my code up tomorrow.

Thanks
Alex Hunsley - 31 Jan 2007 22:16 GMT
>> On Jan 30, 10:53 am, richnjo...@gmail.com wrote:
>>
[quoted text clipped - 43 lines]
> Am I missing the use of timers here? I will try and put a better
> example of my code up tomorrow.

Java's inbuilt Timer and TimerTask classes handle things in a sensible
way - i.e. if you set up multiple TimerTasks, there is only one
background thread dealing with launching your tasks (in the Timer), not
many.

Not sure if this answers your query exactly, but Timer/TimerTask are
pretty handy...

> Thanks
Alex Hunsley - 31 Jan 2007 22:54 GMT
>>> On Jan 30, 10:53 am, richnjo...@gmail.com wrote:
>>>
[quoted text clipped - 48 lines]
> background thread dealing with launching your tasks (in the Timer), not
> many.

Forgot to say - this applies as long as you make one instance only of
Timer, and attach all TimerTasks to that instance. Otherwise you could
indeed have lots of task management threads. (So my suggestion for most
scenarious is for your app to provide one point of access to a Timer
object. If you see what I mean.)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.