blocking pattern

R

richnjones

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
 
D

Dag Sunde

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

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

Eric Sosman

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?

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

Daniel Pitts

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

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.
 
R

richnjones

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.- Hide quoted text -

- 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
 
A

Alex Hunsley

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.

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...
 
A

Alex Hunsley

Alex said:
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.

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top