Reputation: 3
As a beginner, I'm trying to code something like that. For sure, the wrong way, because it doesn't work at all. What I'm trying to do is : use the results i can get when I do something inside a if loop and use these results to calculate conditions the next time I enter the if... To be more understanable, I want to do the same thing if cond1 && cond2 are TRUE but not to early even if the situation occurs (delay of 1000) and not at a value under the previous one...
I don't know how to code this, for the first time I enter the if and for the others... Can you help me please??? Thanks a lot.
boolean cond_1 = a < 10;
boolean cond_2 = b < 15;
boolean cond_delay = (time_now - time_i_have_done_something) > 1000;
boolean cond_value = value_now > value_i_have_done_something;
if (cond_A1 == true && cond_A2 == true && cond_delay == true) {
do something;
long time_i_have_done_something = do_something.gettime();
double value_i_have_done_something = do_something.getvalue();
}
Example of the logic I want to implement :
if (cond_A1 == true) { // firt occurence at 7 h 30 min 00 sec 000 ms
time = gettime();
value = getvalue();
IOrder order = engine.submitOrder("EURUSD", instrument, orderCommand, 1);
}
if (cond_A1 == true) { // second occurence at 7 h 30 min 00 sec 190 ms
// do nothing because time between occurence#1 and occurence#2 < 1000 ms
}
if (cond_A1 == true) { // firt occurence at 7 h 30 min 01 sec 050 ms
// execute this because time between occurence#1 and occurence#3 > 1000 ms
time = gettime();
value = getvalue();
IOrder order = engine.submitOrder("EURUSD", instrument, orderCommand, 1);
}
Upvotes: 0
Views: 201
Reputation: 3166
This will loop, and before the action doSomething()
is called, if will check that a condition is true, and that 1 second has passed since the action was last taken. It will also wait for the first action to be taken at a 1 second delay because the first call is not treated as a special case. This can be fixed by declaring start like this:
long start = System.nanoTime() - timeBetween;
I put in a repeat variable with the value of 5. That can of course be removed.
I chose to not sleep()
the thread, but just yield()
for the processor to be able to take on other tasks while also being able to just busy-loop the program:
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
class StackOverflowTest {
public static void main(String [] args){
StackOverflowTest test = new StackOverflowTest();
test.notTooFast();
}
public void notTooFast(){
boolean condition1 = true;
long timeBetween = 1_000_000_000; // 1 second.
long start = System.nanoTime();
int repeat = 5; // run the loop only 5 times.
while (repeat > 0) { // or for an endless loop, use: while (true)
// has timeBetween passed since last time?
if (condition1 && (System.nanoTime() - start > timeBetween)) {
start = System.nanoTime(); // reset the time
doSomething(repeat--); // don't forget to decrease the counter.
} else {
Thread.currentThread().yield(); // hint to yield this threads current use of a processor.
}
}
}
// just print something for the sake of the example:
public void doSomething(int countdown){
System.out.println(LocalDateTime
.now()
.truncatedTo(ChronoUnit.MILLIS)
+ ": Doing something "
+ countdown);
}
}
It prints:
2020-07-15T13:30:34.409: Doing something 5
2020-07-15T13:30:35.350: Doing something 4
2020-07-15T13:30:36.350: Doing something 3
2020-07-15T13:30:37.349: Doing something 2
2020-07-15T13:30:38.350: Doing something 1
I imagine, the line
doSomething(repeat--);
should probably be replaced with something like this:
repeat--;
IOrder order = engine.submitOrder("EURUSD", instrument, orderCommand, 1);
Instead of Thread.currentThread().yield();
, you can also use
try {
Thread.currentThread().sleep(1000); // 1 second.
} catch (InterruptedException ex) {
// handle whichever way
Thread.currentThread().interrupt(); // reset interrupt flag
}
which will effective put the thread to sleep for 1 second.
Upvotes: 2