Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

wait until variable has reached specific value

 
Bob Matthews
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I wish to do the following:-
At a certain point in my code I want to check a variable
If the variable is not a certain value, I wish to wait and recheck
i.e. do not proceed until the variable has reached a certain value

Is it possible to code this in java ?

I do not want to envelope the remain code in an IF statement

It is similar to a GOTO statement in other languages where you create a loop

Thanks

Bob M
 
Campbell Ritchie
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Depending how crucial the value is, you can use a loop, or an assert or even a straightforward throw:-If you want to ensure the Exception is handled, make it checked. You do that by choice of what to make its superclass, i.e. what kind of Exception you write after extends:-
public class EinsteinException extends XYZException ...
 
Campbell Ritchie
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are certain circumstances where you might start a second thread and wait until it has finished.
 
Bob Matthews
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value I am checking is the number of incoming messages
I am running simultaneous programs which communicate with each other and I need to wait until I have a message from each and every one of them

Bob M
 
Campbell Ritchie
Marshal
Posts: 52636
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple threads in the same JVM or separate JVMs?
 
Bob Matthews
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Multiple threads in the same JVM
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using an ExecutorService which you gave a list of tasks, then it has built-in methods to find out whether all of the tasks have completed and to wait for them all to complete. If you didn't use an ExecutorService, or some similar tool, then you really should use one, rather than trying to write your own version.

At least, it sounds like that's your actual problem; the recasting of it as trying to see the changing values of a variable is confusing and so maybe that's not your actual problem.
 
Bob Matthews
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul

you said................
<<If you're using an ExecutorService which you gave a list of tasks, then it has built-in methods to find out whether all of the tasks have completed and to wait for them all to complete. If you didn't use an ExecutorService, or some similar tool, then you really should use one, rather than trying to write your own version.

At least, it sounds like that's your actual problem; the recasting of it as trying to see the changing values of a variable is confusing and so maybe that's not your actual problem.>>

I do not know what an ExecutorService is

I am running simultaneous strategies on the Dukascopy Forex platform
At a certain point in the code I need to wait until I have received a message (or broadcast) from each of the other strategies before I continue with the code
The broadcast method is part of the Dukascopy API

Bob M

 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So... you really have a question about that API, don't you? There might be people around here who know something about it, so I'll move this thread to a more suitable forum. In the meantime you might consider posting some code which illustrates your problem -- people might be able to guess at possibilities then.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a job for CountDownLatch. You initialize it with a specific numeric value, then let it (a)wait until all workers have decreased the value by 1.
 
Bob Matthews
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Many thanks for your suggestion - it has led me to CyclicBarrier which is exactly what I need

Many thanks

Bob M
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
Do not threaten THIS beaver! Not even with this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!