I was trying to implement a real-time executing application in which a button click event will assign a task to a Thread , which will call a midi Method to play some music. The music has to be started immediately when button is clicked with a small delay. The midi codes are implemented in the run method of Runnable class. But to reach the 1st statement of run method after the button click event itself is taking more than 2 milli second. I tried to use Executor Service, since it can have collection of newly created threads and it reduce the time delay caused by thread. I am new to ExecutorService application. what i observed was at the first time when the button is clicked its taking more than 2 milli sec but when it is restarted the delay is reduced. but at some stage it is increasing to more than 4 milli sec also.. How this delay can be maintained to less than one milli sec everytime. Is there any way to do it using ExecutorService or any other Service providers... Any helps will be appreciating. Here is my code, how i used ExecutorService, If it is not in the proper way to use it please help me correct it.
private static TestExecutorSerivice obj=new TestExecutorSerivice(3);;
private void handlePlayButtonAction(ActionEvent event) throws InterruptedException {
Handler.click=System.nanoTime();
obj.run();
}
TestExecutorService.java
public class TestExecutorSerivice implements Runnable {
private ExecutorService pool;
public TestExecutorSerivice( int poolSize)
{
pool = Executors.newFixedThreadPool(poolSize);
}
public void run() {
try {
pool.execute(new Handler());
} catch (Exception ex) {
pool.shutdown();
}
}
}
class Handler implements Runnable {
public static long click;
public void run() {
System.out.println("time taken "+(((double)System.nanoTime()-click)/1000000));
}
}
output:
time taken 2.107673
time taken 1.336642
time taken 0.185161
time taken 0.130059
time taken 1.007243
time taken 4.486807
time taken 0.092783