如何使用ExecutorService Java减少到达Runnable类的运行方法的时间延迟 [英] How to reduce the time delay to reach run method of Runnable class using ExecutorService Java

查看:280
本文介绍了如何使用ExecutorService Java减少到达Runnable类的运行方法的时间延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个实时执行的应用程序,在该应用程序中,单击按钮事件会将任务分配给Thread,该线程将调用 midi方法来播放一些音乐.单击按钮稍有延迟后,必须立即开始播放音乐. MIDI代码在Runnable类的run方法中实现.但是要在按钮单击事件本身花费 2毫秒以上之后到达run方法的第一条语句. 我尝试使用Executor Service,因为它可以收集新创建的线程,并且可以减少线程引起的时间延迟.我是ExecutorService应用程序的新手.我观察到的是第一次单击该按钮,它花费了超过2毫秒的时间,但是重新启动时,延迟减少了.但是在某些阶段,它还会增加到超过4毫秒..如何将这种延迟保持在每次少于1毫秒的时间.有什么办法可以使用ExecutorService或任何其他服务提供商来完成它吗?任何帮助将不胜感激. 这是我的代码,我是如何使用ExecutorService的,如果使用它的方式不正确,请帮助我更正它.

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

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));
   }
 }

输出:

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

推荐答案

延迟可能是由于OS调度造成的,在这种情况下,可能没有太多事情要做,或者是因为线程池忙于启动线程,或者是因为池已满.

The delays could be due to OS scheduling, in which case there is probably not much to do, or because the thread pool is busy starting the threads or because your pool is fully used.

您可以尝试使用更大的池并预启动所有线程.像这样:

You could try to use a larger pool and prestart all the threads. Something like:

ThreadPoolExecutor tpe = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>());
tpe.prestartAllCoreThreads();
pool = tpe;

例如,池大小为10.

(第一行等效于Executors.newFixedThreadPool)

如果您仍然观察到相同的效果,则可能需要找到其他方法.您的midi库可以提供一种无需使用线程即可异步发送笔记的方法,但是我对此并不熟悉.

If you still observe the same effect, then you will probably need to find a different approach. Your midi library may provide a way to send notes asynchronously without the need to use threads but I'm not familiar with it so I don't know.

另请参阅: https://stackoverflow.com/search?q=%5Bandroid%5D% 20midi%20delay

这篇关于如何使用ExecutorService Java减少到达Runnable类的运行方法的时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆