如何在不使用循环器和处理程序的情况下在特定线程中安排任务?

时间:2021-04-01 16:58:18

Timer creates its own thread and ScheduledThreadPoolExecutor uses a pool. Is their a way to specify the thread where the task will be executed directly without having to marshal any code? And if this is a bad idea, please explain why (beside the thread being busy).

Timer创建自己的线程,ScheduledThreadPoolExecutor使用池。他们是一种指定直接执行任务的线程而无需编组任何代码的方法吗?如果这是一个坏主意,请解释原因(在线程忙碌的旁边)。

I have no problem with the looper-handler approach, I'm just curious.

我对looper-handler方法没有任何问题,我只是好奇。

1 个解决方案

#1


0  

You can create a ScheduledThreadPoolExecutor with one single thread using Executors.newSingleThreadScheduledExecutor().

您可以使用Executors.newSingleThreadScheduledExecutor()创建一个带有一个线程的ScheduledThreadPoolExecutor。

Optionally, you can pass a ThreadFactory as parameter if you want to have more control about this single thread. The thread factory's newThread(Runnable) method is called every time the executor wants to have a new Thread instance that should run the given Runnable (which is not identical to the Runnable you pass to the executor's execute(...), submit(...) or schedule(...) methods).

(可选)如果要对此单个线程拥有更多控制权,可以将ThreadFactory作为参数传递。每次执行程序想要拥有一个应该运行给定Runnable的新Thread实例时,调用线程工厂的newThread(Runnable)方法(与传递给执行程序的execute(...)的Runnable不同,提交(。 ..)或安排(...)方法)。

Note that you are not able to reuse an existing thread, as there is no way to 'inject' code into an already running thread in general, as it is possible in Qt. There, every thread has its own event queue and timing facility, so you can freely decide which (already existing) thread should process your timed task (see Timers in Qt).

请注意,您无法重用现有线程,因为通常无法将代码“注入”已经运行的线程,因为在Qt中是可能的。在那里,每个线程都有自己的事件队列和计时工具,因此您可以*决定哪个(现有的)线程应该处理您的定时任务(请参阅Qt中的计时器)。

There is no such feature in Java out of the box.

开箱即用的Java中没有这样的功能。

#1


0  

You can create a ScheduledThreadPoolExecutor with one single thread using Executors.newSingleThreadScheduledExecutor().

您可以使用Executors.newSingleThreadScheduledExecutor()创建一个带有一个线程的ScheduledThreadPoolExecutor。

Optionally, you can pass a ThreadFactory as parameter if you want to have more control about this single thread. The thread factory's newThread(Runnable) method is called every time the executor wants to have a new Thread instance that should run the given Runnable (which is not identical to the Runnable you pass to the executor's execute(...), submit(...) or schedule(...) methods).

(可选)如果要对此单个线程拥有更多控制权,可以将ThreadFactory作为参数传递。每次执行程序想要拥有一个应该运行给定Runnable的新Thread实例时,调用线程工厂的newThread(Runnable)方法(与传递给执行程序的execute(...)的Runnable不同,提交(。 ..)或安排(...)方法)。

Note that you are not able to reuse an existing thread, as there is no way to 'inject' code into an already running thread in general, as it is possible in Qt. There, every thread has its own event queue and timing facility, so you can freely decide which (already existing) thread should process your timed task (see Timers in Qt).

请注意,您无法重用现有线程,因为通常无法将代码“注入”已经运行的线程,因为在Qt中是可能的。在那里,每个线程都有自己的事件队列和计时工具,因此您可以*决定哪个(现有的)线程应该处理您的定时任务(请参阅Qt中的计时器)。

There is no such feature in Java out of the box.

开箱即用的Java中没有这样的功能。