Recently I wrote code that had to limit request throughput. I used ScheduleExecutorService.scheduleAtFixedRate and I believed that it should do the work (It did!) but I wrote some test to check time of scheduled task and i was amazed. First few tasks weren't scheduled as javadoc explain with n*period. Can anyone explain me what am I missing?
If it work that way then why it is not mentioned in javadoc? And then question is how exactly scheduler work?
I would like to avoid looking into sources:)
Example:
import java.time.Duration;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ExecutorTest {
Executor executor;
ScheduledExecutorService schedulingExecutor;
BlockingQueue<LocalTime> times;
public static void main(String[] args) throws InterruptedException {
new ExecutorTest().start();
}
public ExecutorTest() {
schedulingExecutor = Executors.newScheduledThreadPool(1);
executor = Executors.newCachedThreadPool();
times = new LinkedBlockingQueue<>();
}
public void start() throws InterruptedException {
schedulingExecutor.scheduleAtFixedRate(this::executeTask, 0, 50, TimeUnit.MILLISECONDS);
LocalTime nextEvaluatedTime = times.take();
LocalTime time = nextEvaluatedTime;
while (true) {
System.out.println(String.format(String.join(" ", "recorded time: %d", "calculated proper time: %d", "diff: %d"),
time.toNanoOfDay(),
nextEvaluatedTime.toNanoOfDay(),
Duration.between(nextEvaluatedTime, time).toNanos()));
nextEvaluatedTime = time.plus(50, ChronoUnit.MILLIS);
time = times.take();
}
}
private void executeTask() {
executor.execute(() -> {
times.add(LocalTime.now());
});
}
}
If you run this program you could see that few first time wasn't recorded as expected. Why?
Related
Here is my code:
import java.io.File;
import jaco.mp3.player.MP3Player;
class SimpleAudioPlayer {
public static void main(String[] args) {
File audio_file = new File("Clarx - H.A.Y.mp3");
MP3Player music_player = new MP3Player();
music_player.addToPlayList(audio_file);
music_player.play();
// wait for music_player.play() to finish executing
}
}
I wanted to create an mp3-player and found this Project, what the code snippet does is creating a new MP3Player object, creating a new File, and adding it to the Playlist. After that, it just starts playing the song. But the problem is that it just plays about one or two seconds of the file before the program stops executing. How can I wait until the play() function has stopped executing?
Answer:
Thanks to giraycoskun for this!
import java.io.File;
import jaco.mp3.player.MP3Player;
import java.util.concurrent.*;
class SimpleAudioPlayer {
public static void main(String[] args) {
File audio_file = new File("Clarx - H.A.Y.mp3");
MP3Player music_player = new MP3Player();
music_player.addToPlayList(audio_file);
ExecutorService threadpool = Executors.newCachedThreadPool();
Future<Long> futureTask;
futureTask = (Future<Long>) threadpool.submit(music_player::play);
// Simple variable to check hpw often the folowing loop gets executed
int n = 0;
while (!futureTask.isDone()) {
System.out.println("Executing" + n);
n++;
}
}
}
I had to do some minor changes to the answer he submitted, but it works great, thank you very much!
I haven't tried the code on my computer however these can help:
https://docs.oracle.com/javase/8/docs/api/?java/util/concurrent/package-summary.html
https://www.baeldung.com/java-asynchronous-programming
import java.io.File;
import jaco.mp3.player.MP3Player;
import java.util.concurrent;
class SimpleAudioPlayer {
public static void main(String[] args) {
File audio_file = new File("Clarx - H.A.Y.mp3");
MP3Player music_player = new MP3Player();
music_player.addToPlayList(audio_file);
ExecutorService threadpool = Executors.newCachedThreadPool();
Future<Long> futureTask = threadpool.submit(() -> music_player.play());
while (!futureTask.isDone()) {
// wait for music_player.play() to finish executing
System.out.println("FutureTask is not finished yet...");
}
}
}
I need to wait for a condition or timeout. I came up with the following approach, but there are too many things happening. How can i compress this.
import io.reactivex.Observable;
import java.util.concurrent.TimeUnit;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.ThreadLocalRandom;
public class Test{
public static void main(String[] args)throws InterruptedException{
AtomicBoolean toggle = new
java.util.concurrent.atomic.AtomicBoolean(true);
Observable.interval(0,50, TimeUnit.MILLISECONDS)
.takeWhile(l->l<(20000/50))
.takeWhile(l-> toggle.get())
.observeOn(Schedulers.io())
.map(l->{ return (l>ThreadLocalRandom.current()
.nextInt(5, 20 + 1))?true:false;})
// The above map will call a remote function to check for some condition
.observeOn(Schedulers.computation())
.filter(exist->exist)
//.takeWhile(exist->!exist)
.map(l->{toggle.set(false);return l;})
.map(l->{System.out.println("Called at "+l);return l;})
.blockingSubscribe();
}
}
Here is code. You can use firstElement to get the first item.
import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class Q44234633 {
public static void main(String[] args) throws InterruptedException {
Observable.interval(50, TimeUnit.MILLISECONDS)
.takeWhile(l -> l < 400)
.observeOn(Schedulers.io())
.filter(l -> isConditionTrue(l))
.observeOn(Schedulers.computation())
.firstElement()
.doOnSuccess(System.out::println)
.isEmpty()
.filter(empty -> empty)
.doOnSuccess(b -> System.out.println("TimeOut"))
.blockingGet();
}
private static boolean isConditionTrue(long time) {
return time > ThreadLocalRandom.current().nextInt(5, 20 + 1);
}
}
There are also two tips for you.
You can use doOnNext rather than map if you actually don't map the value.
BooleanValue? true : false can be written BooleanValue directly.
I am creating a ScheduledExecutorService through Dropwizard's LifecycleEnvironment.scheduledExecutorService(). I schedule four tasks on it, which throw an exception after 3 seconds. The problem is that no stack trace is printed for exceptions and hence I cannot trace why it happened. The task which throws exception one is never restarted.
I tried setting a default uncaught exception handler but it didn't help either:
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
#Override
public void uncaughtException(Thread thread, Throwable throwable) {
System.err.println("An exception occurred as below:");
throwable.printStackTrace(System.err);
}
});
Following is the complete code:
This is the main driving class which extends Application:
App.java
import io.dropwizard.Application;
import io.dropwizard.setup.Environment;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class App extends Application<AppConfiguration> {
public void run(AppConfiguration appConfiguration, Environment environment) throws Exception {
final ScheduledExecutorService scheduledExecutorService = environment.lifecycle()
.scheduledExecutorService("throwing-exception-threads").threads(4)
.build();
scheduledExecutorService.scheduleAtFixedRate(new ThreadToDie(), 0,5, TimeUnit.SECONDS);
scheduledExecutorService.scheduleAtFixedRate(new ThreadToDie(), 0,5, TimeUnit.SECONDS);
scheduledExecutorService.scheduleAtFixedRate(new ThreadToDie(), 0,5, TimeUnit.SECONDS);
scheduledExecutorService.scheduleAtFixedRate(new ThreadToDie(), 0,5, TimeUnit.SECONDS);
final HelloWorldResource resource = new HelloWorldResource();
environment.jersey().register(resource);
}
public static void main(String[] args) throws Exception {
new App().run(args);
}
}
Code for task being scheduled at fixed interval is below. It prints counter value which is incremented at each second. When the counter is 3 it throws a NullPointerException.
ThreadToDie.java
public class ThreadToDie implements Runnable {
int i = 0;
#Override
public void run() {
i++;
System.out.printf("Value of i: %d\n", i);
if (i % 3 == 0) {
System.out.printf("Throwing NullPointerException\n");
throw new NullPointerException("This should be printed.");
}
}
}
For the sake of completeness following are the Configuration class and A HelloWorld API class. Though in the question asked what they contain is not relevant.
AppConfiguration.java
import io.dropwizard.Configuration;
public class AppConfiguration extends Configuration {
}
HelloWorldResource.java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.Optional;
#Path("/hello")
#Produces(MediaType.APPLICATION_JSON)
public class HelloWorldResource {
#GET
public String hello(#QueryParam("name") Optional<String> name) {
final String retVal = String.format("Hello %s!", name.orElse("World"));
return retVal;
}
}
See Why is UncaughtExceptionHandler not called by ExecutorService? for an explanation on why the UncaughtExceptionHandler is never triggered - each task you supply is handled by a worker that catches all exceptions.
When you submit a task to an executor you receive back a Future and can access the exception in that way:
ScheduledFuture<?> future = scheduledExecutorService.scheduleAtFixedRate(new ThreadToDie(), 0,5, TimeUnit.SECONDS);
try {
future.get();
} catch (ExecutionException ex) {
ex.getCause().printStackTrace();
}
Future.get will wait until the task has completed or errored.
I want "runnable" to run at 5tps. This is not executing paralelly.
package tt;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class mySpawner {
public int tillDone = 0;
public int tillMax = 0;
public ArrayList arrayList;
private myWorker myworking;
private ScheduledExecutorService service = Executors.newScheduledThreadPool(50);
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
System.out.println(System.nanoTime());
Thread.sleep(7000);
} catch (InterruptedException ex) {
Logger.getLogger(mySpawner.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
public void activate() {
try {
service = Executors.newScheduledThreadPool(50);
service.scheduleAtFixedRate(runnable, 0, 200, TimeUnit.MILLISECONDS);
} catch (Exception e) {//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
public void deactivate() {
service.shutdown();
}
}
Consider this:
Your tasks are sleeping for 7 seconds during their execution
You are scheduling a new task every 200ms
You only have 50 threads in your executor
It should be clear, I hope, that you'll run out of pooled threads in just a few seconds, and you'll lose your parallelism. You need to balance this better, either by reducing the rate or reducing the sleep. Increasing the pool size won't help, you'll still run out of threads.
scheduleAtFixedRate does only spawn a single thread but executes the runnable provided with a fixed rate.
The action runs in less amount of time than the given period: in this case it is respawned with exactly the specified period.
The action runs longer (your case): the action is started again immediately.
If you want to have the desired behaviour you may use the following pattern: Just execute the runnable once:
service.schedule(runnable, 0, TimeUnit.MILLISECONDS);
but inside the runnable's run method add the next invokation by
service.schedule(runnable, 200, TimeUnit.MILLISECONDS);
Nevertheless consider the arithmetic as described in the answer by skaffman.
Update: Howard is right, my first example was wrong.
I verified this works if you change your active() method:
service = Executors.newScheduledThreadPool(50);
new Thread() {
public void run() {
long nextTime = System.currentTimeMillis();
while (true) {
service.submit(runnable);
long waitTime = nextTime - System.currentTimeMillis();
Thread.sleep(Math.max(0, waitTime));
nextTime += 200;
}
}
}.start();
I've been Googling Java timestamps, timers, and anything to do with time and Java.
I just can't seem to get anything to work for me.
I need a timestamp to control a while loop like the pseudo-code below
while(true)
{
while(mytimer.millsecounds < amountOftimeIwantLoopToRunFor)
{
dostuff();
}
mytimer.rest();
}
Any ideas what data type I could use; I have tried Timestamp, but didn't seem to work.
Thanks
CiarĂ¡n
Do something like:
long maxduration = 10000; // 10 seconds.
long endtime = System.currentTimeMillis() + maxduration;
while (System.currentTimeMillis() < endtime) {
// ...
}
An (more advanced) alternative is using java.util.concurrent.ExecutorService. Here's an SSCCE:
package com.stackoverflow.q2303206;
import java.util.Arrays;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String... args) throws Exception {
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.invokeAll(Arrays.asList(new Task()), 10, TimeUnit.SECONDS); // Get 10 seconds time.
executor.shutdown();
}
}
class Task implements Callable<String> {
public String call() throws Exception {
while (true) {
// ...
}
return null;
}
}