简体   繁体   English

如何在单线程中使用多个可运行接口?

[英]How to use Multiple runnable interfaces in single thread?

I am developing a java application which computes various mathematical functions. 我正在开发一个计算各种数学函数的Java应用程序。 Here is the scenario, I have M runnable tasks (each for computing various problems, like one solves quadratic eqns, other solves exponential functions, something like that). 在这种情况下,我有M个可运行的任务 (每个任务用于计算各种问题,例如一个解决二次方程,另一个解决指数函数,诸如此类)。 These M runnables has to be executed for every N mins . 这些M个可运行对象必须每N分钟执行一次 These runnables can be executed sequentially not necessarily in parallel manner. 这些可运行对象可以不一定以并行方式顺序执行。 I am not allowed to create more than one thread . 不允许创建多个线程

I can use ScheduledExecutorService for running the tasks periodically. 我可以使用ScheduledExecutorService定期运行任务。 As per Javadoc, Only one runnable can be used with ScheduledExecutorService. 根据Javadoc,ScheduledExecutorService只能使用一个可运行的对象。 There are methods like invokeAll(...) , which allows us to provide Collection of runnable, but these doesnt provide scheduling option. 有诸如invokeAll(...)之类的方法,该方法允许我们提供Runnable的 Collection,但这些方法不提供调度选项。

On browsing through internet I found, using Thread.sleep() is not a good way to develop an application. 在浏览互联网时,我发现使用Thread.sleep()并不是开发应用程序的好方法

Any suggestions?? 有什么建议么??

You can create ExecutorService that contains only one thread to run your jobs: 您可以创建仅包含一个线程来运行作业的ExecutorService

ExecutorService executorService = Executors.newSingleThreadExecutor();

When you submit several jobs to this service using invokeAll method it will run them sequentially using single Thread instance. 当您使用invokeAll方法向该服务提交多个作业时,它将使用单个Thread实例依次运行它们。

If you want to use ScheduledExecutorService to run your jobs every N minutes you can switch to 如果要每隔N分钟使用ScheduledExecutorService运行作业,则可以切换到

ScheduledExecutorService scheduledExecutorService = 
    Executors.newSingleThreadScheduledExecutor();

that will provide you with additional methods to better control your jobs. 这将为您提供其他方法,以更好地控制您的工作。

As you can see invokeAll method is derived from ExecutorService , so it does not provide you scheduling options. 如您所见, invokeAll方法是从ExecutorService派生的,因此它不提供调度选项。 Still this is just a shortcut and you can schedule multiple Runnable instances using regular loop: 仍然这只是一个快捷方式,您可以使用常规循环安排多个Runnable实例:

for (Runnable job : allJobs) {
    scheduledExecutorService.scheduleAtFixedRate(job, 0L, N, TimeUnit.MINUTES);
}

If you create a multiple runnable class like the following, you can pass an instance of it to your scheduler and it will run all the runnables that you instantiated it with. 如果创建如下所示的多个可运行类,则可以将其实例传递给调度程序,它将运行实例化其的所有可运行类。 The scheduler does the rest, this just sequentially runs the array of runnables. 调度程序会做其余的事情,这只是顺序地运行可运行对象数组。

public class MultipleRunnable implements Runnable
{
    private Runnable[] commands;
    public MultipleRunnable(Runnable[] commands)
    {
        this.commands = commands;
    }

    public void run()
    {
        for(int i = 0 ; i < commands.length ; i++)
        {
             commands[i].run();
        }
    }
}

Something like this to start it should work: 像这样的东西应该可以开始工作:

ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(
new MultipleRunnable(new Runnable[]{run1, run2, ..., runM}),
n, n, TimeUnit.MINUTES);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM