Java ScheduledThreadPoolExecutor只运行Swingworker一次
ScheduledThreadPoolExecutor(实现ScheduledExecutorService)在使用ScheduleAtFixedRate方法时似乎只运行SwingWorker类一次。原始代码有点长,所以我制作了一个新代码,产生了与下面相同的结果Java ScheduledThreadPoolExecutor只运行Swingworker一次,java,swing,swingworker,scheduledexecutorservice,Java,Swing,Swingworker,Scheduledexecutorservice,ScheduledThreadPoolExecutor(实现ScheduledExecutorService)在使用ScheduleAtFixedRate方法时似乎只运行SwingWorker类一次。原始代码有点长,所以我制作了一个新代码,产生了与下面相同的结果 import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; import javax.swing.Swin
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
public class ScheduledThreadPoolExecutorTest extends SwingWorker<Void, Void>{
@Override
protected Void doInBackground() {
System.out.println("Yay!");
return null;
}
@Override
protected void done() {
try {
get();
} catch(Exception e) {
e.printStackTrace();
}
System.out.println("Woohoo!");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
executor.scheduleAtFixedRate(new ScheduledThreadPoolExecutorTest(), 0, 30, TimeUnit.MILLISECONDS);
}
});
}
}
为什么ScheduledThreadPoolExecutor只运行SwingWorker一次?我该怎么做才能使SwingWorker按照代码中的指示每30毫秒运行一次?而SwingWorker确实按照
doInBackground()
方法中的部分实现了Runnable
接口:
请注意,此方法只执行一次
因此,虽然它的内部run()
方法可能会重复运行,doInBackground()
只会运行一次。不仅如此,SwingWorker中的run()
方法被标记为final
,因此您不能覆盖它多次调用doInBackground
更好的解决方案不是使用SwingWorker,而是使用一个更简单的Runnable派生类。SwingWorker扩展了Runnable,但它使用FutureTask来运行计算 从javadoc:
A cancellable asynchronous computation. This class provides a base
implementation of {@link Future}, with methods to start and cancel
a computation, query to see if the computation is complete, and
retrieve the result of the computation. The result can only be
retrieved when the computation has completed; the {@code get}
methods will block if the computation has not yet completed. Once
the computation has completed, the computation cannot be restarted
or cancelled (unless the computation is invoked using
{@link #runAndReset}).
也就是说,FutureTask只运行一次,如果您再次尝试运行它,它将返回
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
public void run(){
如果(状态)=新||
!不安全。比较AndSwapObject(此、runnerOffset、,
null,Thread.currentThread())
返回;
试一试{
可调用c=可调用;
如果(c!=null&&state==NEW){
V结果;
布尔ran;
试一试{
结果=c.call();
ran=真;
}捕获(可丢弃的ex){
结果=空;
ran=假;
setException(ex);
}
如果(运行)
设置(结果);
}
}最后{
//在将状态设置为之前,运行程序必须为非null
//防止对run()的并发调用
runner=null;
//将运行程序置零后必须重新读取状态以防止
//泄漏中断
int s=状态;
如果(s>=中断)
HandlePossibleConcellationInterrupt(多个);
}
}
SodoInBackground()
只对每个类调用一次,而不是对该类的每个实例调用一次?另外,如果您使用的是Swing对象,并且需要调用repaint()
(当然这里没有显示,但在原始代码中显示),因此绝对需要使用SwingWorker,该怎么办?@ICanCYou:每个实例调用一次--不过您只创建了一个实例。@ICanCYou:如果需要,您还可以创建多个SwingWorker,或者可以使用SwingUtilies将Swing调用排队到事件线程中。请注意,事件线程不需要调用repaint()
。@满是EEL的气垫船执行器是否可以使用scheduleAtFixedRate()
方法每隔30毫秒计划一个SwingWorker的新实例?@ICanCYou:您可以在执行器服务调用的可运行程序中创建SwingWorker,但这似乎是多余的。为什么不根据需要简单地将Swing代码排队呢。
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}