• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

wait until variable has reached specific value

 
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 80869
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80869
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are certain circumstances where you might start a second thread and wait until it has finished.
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80869
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiple threads in the same JVM or separate JVMs?
 
Bob Matthews
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiple threads in the same JVM
 
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28425
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 688
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
reply
    Bookmark Topic Watch Topic
  • New Topic