我想创建一个后台线程来处理一些任务(Runnable)并按照它们发布的顺序执行它们.
重要说明:不得在Event Dispatcher Thread上执行这些任务.
就像是 :
BackgroundEventThread backgroundEventThread = new BackgroundEventThread();
然后,在以后,代码中的许多地方:
Runnable thingToDo = new Runnable(){...};
backgroundEventThread.executeThis(thingToDo);
//the things to do will be executed in the order in which they are posted.
类BackgroundFventThread应该是非常简单的代码,但我想知道这样的类是否已经存在于我在JDK或某些公共库中未知的某个地方……
编辑:我事先不知道在这个线程上执行的任务数.
我本可以有:
> task0(真的很短)发生在t0
> task1(很长时间来处理……)正在发生
在t0 1s
> task2(短)发生在t0 5s等
> task3(等等……)
我需要在task1之后执行task2(事先我不知道),我希望所有这些任务尽快执行.
完全像在EDT上发布的任务,但不在EDT上.
解决方法:
您可以使用单个线程的“池”进行设置,然后向其执行任务以执行.
标签:java,multithreading,events
来源: https://codeday.me/bug/20190610/1214075.html