Delay method invocation - java

I have the following code:
public void onEnter() {
Observable<GObject> obs = context.update(activeG);
obs.subscribe((gObj) -> {
//TODO: add delay of 3 sec.
activeG.publishNewG();
activeG.nextState(GType.RUNNING);
});
}
My question is, How can I put a delay of 3 seconds so
activeG.publishNewG()
is called, then delay of 3 seconds, and then a call to
activeGame.nextState(GameStateType.RUNNING);
'publishNewG' returns void.
Thank you!

If I understand correctly, you want to put a 3 second delay between publishNewG and nextState. You can use doOnNext to inject activity at certain points in the sequence, for example, before and after a 3 second delay:
Observable.just(1)
.doOnNext(e -> System.out.println("Action before"))
.delay(3, TimeUnit.SECONDS)
.doOnNext(e -> System.out.println("Action after"))
.toBlocking().first();

You would typically accomplish this using a scheduler of some sort. Java has ScheduledExecutorService that does this for you.
public class MyClass {
private final ScheduledExecutorService scheduler;
public MyClass() {
scheduler = Executors.newSingleThreadScheduledExecutor();
}
public void onEnter() {
Observable<GObject> obs = context.update(activeG);
obs.subscribe((gObj) -> {
activeG.publishNewG();
// Run in 3 seconds
scheduler.schedule(() -> {
activeG.nextState(GType.RUNNING);
}, 3, TimeUnit.SECONDS);
});
}
}
Edit: From what I can understand of the RxJava docs this is how you would do it with delay:
public void onEnter() {
Observable<GObject> obs = context.update(activeG);
// Observable that delays all events by 3 seconds
Observable<GObject> delayed = obs.delay(3, TimeUnit.SECONDS);
// This will fire immediately when an event happens
obs.subscribe((gObj) -> {
activeG.publishNewG();
});
// This will fire 3 seconds after an event happens
delayed.subscribe((gObj) -> {
activeG.nextState(GType.RUNNING);
});
}

You can easily add delay by using the timer operator.
For example:
// simulated stream of events spread apart by 400ms
Observable<Long> yourObservable = Observable.interval(400, TimeUnit.MILLISECONDS).take(3);
yourObservable.flatMap(data -> {
// add delay of 3 seconds
return Observable.timer(3, TimeUnit.SECONDS).map(i -> data);
}).map(dataAfterDelay -> {
// do whatever you want after 3 seconds
System.out.println("got data " + dataAfterDelay + " after 3 seconds");
return dataAfterDelay + " processed after delay";
}).toBlocking().forEach(System.out::println); // toBlocking here just as example to block main thread

AFAIK you can use Thread.sleep(3000)
If not, something like this should do:
long start = new Date().getTime();
while(new Date().getTime() - start < 3000L){}

Related

ScheduledExecutorService execute tasks late, with low CPU & RAM usage

I need to create multiple tasks, each of that executes every n seconds. I've decided to use ScheduledExecutorService to schedule task execution. The problem is tasks not executed in time. I thought the reason is not enough processor time, but actual CPU usage is about 4-5 percents.
My schedulers creator:
class SchedulersCreator {
private final ScheduledExecutorService scheduler
= Executors.newScheduledThreadPool(1);
public SchedulersCreator(int tasksAmount, int repeatCount) {
for (int taskId = 0; taskId <= tasksAmount; taskId++) {
// create new task, that executes every 2 seconds
MyTask task = new MyTask(scheduler, repeatCount, 2, taskId);
// execute new task
task.run();
}
}
public static void main(String[] args) {
System.out.println("Program started");
// create & start 10 tasks, each of the executes 10 times with period 2 seconds
SchedulersCreator scheduler = new SchedulersCreator(10, 10);
System.out.println("All tasks created & started");
}
}
My task:
class MyTask implements Runnable {
// number of executions
private int executesTimesLeft;
// execution period
private final int periodSeconds;
// task id
private final int id;
// scheduler
private ScheduledExecutorService scheduler;
// field to measure time between executions
private long lastExecution = 0;
public MyTask(ScheduledExecutorService scheduler, int executes, int periodSeconds, int id) {
this.executesTimesLeft = executes;
this.id = id;
this.periodSeconds = periodSeconds;
this.scheduler = scheduler;
}
private void performAction() {
long before = System.currentTimeMillis();
long time = (before - lastExecution) % 1_000_000;
lastExecution = before;
// Simulates useful calculations
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
long after = System.currentTimeMillis();
if (id % 100_000 == 0) {
long duration = after - before;
System.out.println("Time since prev execution:\t" + time + "\t"
+ "Task " + id + ": "
+ executesTimesLeft + " executions lefts; "
+ "current duration\t" + duration);
}
}
#Override
public void run() {
// perform useful calculation in another thread
new Thread(() -> performAction()).run();
executesTimesLeft--;
if (executesTimesLeft > 0) { // schedule next task execution
scheduler.schedule(this, periodSeconds, SECONDS);
}
}
}
The code at the ideone: https://ideone.com/s3iDif.
I've expected time between executions about 2 seconds, but the actual result is 3-4 seconds.
Program output:
...
Time since prev execution: 3028 Task 0: 2 executions lefts; current duration 1000
Time since prev execution: 4001 Task 0: 1 executions lefts; current duration 1001
Your code doesn't use the scheduler properly.
// perform useful calculation in another thread
new Thread(() -> performAction()).run();
This doesn't actually run the code in a new thread. To do that you need to call start(), not run(). Calling run() makes the code execute in the current thread, no different than if you had just written performAction();.
However, you shouldn't be explicitly creating a new thread at all. You can and should do the work right in MyTask.run().
Tasks don't need to know about the scheduler or their frequency. Change this code:
MyTask task = new MyTask(scheduler, repeatCount, 2, taskId);
// execute new task
task.run();
to:
MyTask task = new MyTask(repeatCount, taskId);
Future<?> future = scheduler.scheduleAtFixedRate(task, 0, 2, SECONDS);
You want the task to repeat, so use the scheduler method that does so. That'll allow the scheduler to adjust the time in between tasks based on how long they take to run.
Move all of performAction() into MyTask.run(). When you want the task to stop repeating, use the future to cancel it.

RxJava Delay for Hot Observables

I saw this question here.
It's about achieving delay for each emitted item. This is how to achieve it based on the accepted answer:
Observable.zip(Observable.range(1, 5)
.groupBy(n -> n % 5)
.flatMap(g -> g.toList()),
Observable.interval(50, TimeUnit.MILLISECONDS),
(obs, timer) -> obs)
.doOnNext(item -> {
System.out.println(System.currentTimeMillis() - timeNow);
System.out.println(item);
System.out.println(" ");
}).toList().toBlocking().first();
In the question, the asker specifically asked for a fixed set of observables (Observable.range(1,5)), unfortunately this is not what I want to achieve.
I also saw this comment.
That comment is what I want to achieve. So my source observable emits items at a slower (and sometimes faster) rate than the interval. Also the observable's emits are never ending.
===
So basically I want hot observables to have minimum delay.
For example, if I want 400ms minimum delay and I have this kind of observable emittance:
X1-100ms delay-X2-200ms delay-X3-600ms delay-X4-20000ms delay-X5-...
I want it to yield:
X1-400ms delay-X2-400ms delay-X3-600ms delay-X4-20000ms delay-X5-...
Anybody have any idea to achieve that?
Your requirement is so strange...
I can resolve it but not elegant. Here is my code:
class Three<A, B, C> {
A a;
B b;
C c;
// Getter, Setter, Constructor
}
public static void main(String[] args) throws Exception {
BehaviorSubject<Integer> s = BehaviorSubject.create();
// Three = (The value, upstream comes mills, downstream emits mills)
s.map(i -> new Three<>(i, System.currentTimeMillis(), System.currentTimeMillis()))
.scan((a, b) -> {
b.setC(a.getC() + Math.max(400L, b.getB() - a.getB()));
return b;
})
.concatMap(i -> Observable.just(i.getA()).delay(Math.max(0, i.getC() - System.currentTimeMillis()),
TimeUnit.MILLISECONDS))
.subscribe(i -> System.out.println(i + "\t" + System.currentTimeMillis()));
s.onNext(0);
Thread.sleep(100);
s.onNext(1);
Thread.sleep(200);
s.onNext(2);
Thread.sleep(600);
s.onNext(3);
Thread.sleep(2000);
s.onNext(4);
Thread.sleep(200);
s.onNext(5);
Thread.sleep(800);
s.onNext(6);
Thread.sleep(1000);
}
and output
0 1510128693984
1 1510128694366 // 400ms
2 1510128694766 // 400ms
3 1510128695366 // 600ms
4 1510128697366 // 2000ms
5 1510128697766 // 400ms
6 1510128698567 // 800ms

RxJava: Why retryWhen/repeatWhen doesn't work?

I've been stuck with this for a day. Inspired in Dan Lew great post, I tried to make a simple testcase for repeatWhen() and retryWhen():
public class ObsTest {
private static final Logger LOG = LoggerFactory.getLogger(ObsTest.class);
#Test
public void test1() throws InterruptedException {
Observable<Integer> obs = rx.Observable.<Integer> create(observer -> {
LOG.info("onSubscribe");
Integer data = RandomUtils.nextInt(0, 1000);
if (data % 2 != 0) {
observer.onError(new RuntimeException("Odd number " + data));
} else {
observer.onNext(data);
}
observer.onCompleted();
}, BackpressureMode.BUFFER);
obs.repeatWhen(completed -> completed.delay(1, TimeUnit.MILLISECONDS))
.retryWhen(error -> error.delay(1, TimeUnit.MILLISECONDS))
.subscribe(i -> LOG.info("value={}", i), e -> LOG.info("Exception = {}", e.getMessage()));
}
My idea is this should run forever, emitting even numbers as "correct" results, and odd numbers as "errors".
Instead, this runs for one or two loops and then stops. And that is when the delay is 1 millisecond, for longer periods of time (ie. 1 second), it runs a single time, emitting just a single odd or even number.
I'm sure I'm doing something wrong, but I can't find what it is.
When you call delay which uses Schedulers.computation() by default you are introducing asynchrony. Once activity starts occurring in a background thread your test will finish and presumably your process is exited. You need to use a blockingSubscribe or put a longish Thread.sleep at the end.
As Dave Moten mentioned, delay uses Schedulers.computation() by default, but you can pass scheduler of your choice instead - for tests purposes you may use TestScheduler and "take control over time". Code below shows how can it be used - as you can see this subscription won't terminal for another 30 days, what is basically forever ;)
public class ObsTest {
#Test
public void test1() {
Observable<Integer> obs = rx.Observable.create(observer -> {
Integer data = RandomUtils.nextInt(0, 1000);
if (data % 2 != 0) {
observer.onError(new RuntimeException("Odd number " + data));
} else {
observer.onNext(data);
}
observer.onCompleted();
}, Emitter.BackpressureMode.BUFFER);
TestScheduler scheduler = Schedulers.<Integer>test();
AssertableSubscriber subscriber = obs.repeatWhen(completed -> completed.delay(1, TimeUnit.MILLISECONDS, scheduler))
.retryWhen(error -> error.delay(1, TimeUnit.MILLISECONDS, scheduler))
.subscribeOn(scheduler)
.test();
subscriber.assertNoValues();
scheduler.advanceTimeBy(30, TimeUnit.SECONDS);
subscriber.assertNoTerminalEvent();
scheduler.advanceTimeBy(30, TimeUnit.DAYS);
subscriber.assertNoTerminalEvent();
}
}

Java 8: How can I convert a for loop to run in parallel?

for (int i=0; i<100000; i++) {
// REST API request.
restTemplate.exchange(url, HttpMethod.GET, request, String.class);
}
I have a situation where I have to request a resource for 100k users and it takes 70 minutes to finish. I tried to clean up my code as much as possible and I was able to reduce it only by 4 minutes).
Since each request is independent of each other, I would love to send requests in parallel (may be in 10s, 100s, or even 1000s of chunks which every finishes quickly). I'm hoping that I can reduce the time to 10 minutes or something close. How do I calculate which chunk size would get the job done quickly?
I have found the following way but I can't tell if the program processes all the 20 at a time; or 5 at a time; or 10 at a time.
IntStream.range(0,20).parallel().forEach(i->{
... do something here
});
I appericiate your help. I am open to any suggestions or critics!!
UPDATE: I was able to use IntStream and the task finished in 28 minutes. But I am not sure this is the best I could go for.
I used the following code in Java 8 and it did the work. I was able to reduce the batch job to run from 28 minutes to 3:39 minutes.
IntStream.range(0, 100000).parallel().forEach(i->{
restTemplate.exchange(url, HttpMethod.GET, request, String.class);
}
});
The standard call to parallel() will create a thread for each core your machine has available minus one core, using a Common Fork Join Pool.
If you want to specify the parallelism on your own, you will have different possibilities:
Change the parallelism of the common pool: System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "20")
Use an own pool:
Example:
int allRequestsCount = 20;
int parallelism = 4; // Vary on your own
ForkJoinPool forkJoinPool = new ForkJoinPool(parallelism);
IntStream.range(0, parallelism).forEach(i -> forkJoinPool.submit(() -> {
int chunkSize = allRequestsCount / parallelism;
IntStream.range(i * chunkSize, i * chunkSize + chunkSize)
.forEach(num -> {
// Simulate long running operation
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + ": " + num);
});
}));
This implementation is just examplary to give you an idea.
For your situation you can work with fork/join framework or make executor service pool of threads.
ExecutorService service = null;
try {
service = Executors.newFixedThreadPool(8);
service.submit(() -> {
//do your task
});
} catch (Exception e) {
} finally {
if (service != null) {
service.shutdown();
}
}
service.awaitTermination(1, TimeUnit.MINUTES);
if(service.isTerminated())
System.out.println("All threads have been finished");
else
System.out.println("At least one thread running");
And using fork/join framework
class RequestHandler extends RecursiveAction {
int start;
int end;
public RequestHandler(int start, int end) {
this.start = start;
this.end = end;
}
#Override
protected void compute() {
if (end - start <= 10) {
//REST Request
} else {
int middle = start + (end - start) / 2;
invokeAll(new RequestHandler(start, middle), new RequestHandler(middle, end));
}
}
}
Public class MainClass{
public void main(String[] args){
ForkJoinTask<?> task = new RequestHandler(0, 100000);
ForkJoinPool pool = new ForkJoinPool();
pool.invoke(task);
}
}
I've written a short article about that. It contains simple tool that allows you to control pool size:
https://gt-dev.blogspot.com/2016/07/java-8-threads-parallel-stream-how-to.html

Adding delay between Observable Items RxJava [duplicate]

This question already has answers here:
RxJava delay for each item of list emitted
(17 answers)
Closed 6 years ago.
I have an observable I've created from a list of objects. For each object in the list I make a network request but I'd like to put a delay between each item in the list as to space out the requests a bit. Here's a snippet of my code.
return Observable.from(documentGroupModels).flatMap(new Func1<DocumentGroupModel, Observable<Boolean>>() {
#Override
public Observable<Boolean> call(DocumentGroupModel documentGroupModel) {
return refreshDocumentWithUri(documentGroupModel.getUri(), documentGroupModel.sectionGroupId,
includeExceptions, false);
}
});
Using delay or buffer doesn't quite work for this scenario as far as I can tell.
You can use a combination of Zip and interval operator if your delay is static, so you can emit an item of your zip every time configure on your interval.
Check the example
#Test
public void delaySteps() {
long start = System.currentTimeMillis();
Subscription subscription =
Observable.zip(Observable.from(Arrays.asList(1, 2, 3)), Observable.interval(200, TimeUnit.MILLISECONDS),
(i, t) -> i)
.subscribe(n -> System.out.println("time:" + (System.currentTimeMillis() - start)));
new TestSubscriber((Observer) subscription).awaitTerminalEvent(3000, TimeUnit.MILLISECONDS);
}
Also you can create an Observable with your list and use concatMap, then you can use delay for every item emitted. Maybe this solution is more elegant and no so Hacky
#Test
public void delayObservableList() {
Observable.from(Arrays.asList(1, 2, 3, 4, 5))
.concatMap(s -> Observable.just(s).delay(100, TimeUnit.MILLISECONDS))
.subscribe(n -> System.out.println(n + " emitted"),
e -> {
},
() -> System.out.println("All emitted"));
new TestSubscriber().awaitTerminalEvent(1000, TimeUnit.MILLISECONDS);
}
You can see another examples of delay here https://github.com/politrons/reactive/blob/master/src/test/java/rx/observables/utils/ObservableDelay.java
Use the `delay' operator e.g.
return Observable.from(documentGroupModels).flatMap(new Func1<DocumentGroupModel, Observable<Boolean>>() {
#Override
public Observable<Boolean> call(DocumentGroupModel documentGroupModel) {
return refreshDocumentWithUri(documentGroupModel.getUri(), documentGroupModel.sectionGroupId,
includeExceptions, false).delay(2000, TimeUnit.MILLISECONDS);
}
});

Categories