Java ScheduledExecutorService-更新内部数据
我有ScheduledExecutorService,我正在尝试更新内部数据,但没有结果Java ScheduledExecutorService-更新内部数据,java,android,Java,Android,我有ScheduledExecutorService,我正在尝试更新内部数据,但没有结果 public void myMethod(final String myString) { myExecutor.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println(myString); } }, 0,
public void myMethod(final String myString) {
myExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(myString);
}
}, 0, 10000, TimeUnit.MILLISECONDS);
}
现在我想更改应用程序中其他位置的字符串
myMethod(myString);
我是否有多个线程,有时有旧数据,有时有新数据?如何修复它?一种可能是关闭
Executor服务。这将停止您提交的任务
myExecutor.shutdown();
myExecutor.shutdownNow();
如果您想再次安排,您需要创建一个新的ExecutorService
tough,因为一旦关闭它就不能再使用。然后,您可以使用myString
的新值重新安排任务
private void scheduleTask(ExecutorService service, String myString) {
service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(myString);
}
}, 0, 10000, TimeUnit.MILLISECONDS);
}
一种可能是关闭Executor服务
。这将停止您提交的任务
myExecutor.shutdown();
myExecutor.shutdownNow();
如果您想再次安排,您需要创建一个新的ExecutorService
tough,因为一旦关闭它就不能再使用。然后,您可以使用myString
的新值重新安排任务
private void scheduleTask(ExecutorService service, String myString) {
service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
System.out.println(myString);
}
}, 0, 10000, TimeUnit.MILLISECONDS);
}
你可以试试这个:
public class Test {
private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(1); // single instance
private static final Lock LOCK = new ReentrantLock(); // for locking
private static final AtomicBoolean RUNNING = new AtomicBoolean(false);
private static String globalString;
public void myMethod(String myString) throws InterruptedException {
LOCK.lockInterruptibly();
try {
globalString = myString;
if(!RUNNING.get()) {// this will make sure only one runnable runs
EXECUTOR.scheduleAtFixedRate(() -> {
System.out.println(globalString);
}, 0, 10000, TimeUnit.MILLISECONDS);
RUNNING.set(true);
}
} finally {
LOCK.unlock();
}
}
}
起初不会有Runnable
运行,因此当第一次调用myMethod
时,running.get()
将返回false
,因此执行器将调度Runnable
。它还将RUNNING
的值翻转为true
。之后,对于所有调用,不会创建新的Runnable
,但是myMethod
将更新run
方法打印的globalString
LOCK
确保一次只有一个线程可以执行Runnable
创建和其他事情的逻辑
确保在作业完成后关闭执行器。您可以尝试以下操作:
public class Test {
private static final ScheduledExecutorService EXECUTOR = Executors.newScheduledThreadPool(1); // single instance
private static final Lock LOCK = new ReentrantLock(); // for locking
private static final AtomicBoolean RUNNING = new AtomicBoolean(false);
private static String globalString;
public void myMethod(String myString) throws InterruptedException {
LOCK.lockInterruptibly();
try {
globalString = myString;
if(!RUNNING.get()) {// this will make sure only one runnable runs
EXECUTOR.scheduleAtFixedRate(() -> {
System.out.println(globalString);
}, 0, 10000, TimeUnit.MILLISECONDS);
RUNNING.set(true);
}
} finally {
LOCK.unlock();
}
}
}
起初不会有Runnable
运行,因此当第一次调用myMethod
时,running.get()
将返回false
,因此执行器将调度Runnable
。它还将RUNNING
的值翻转为true
。之后,对于所有调用,不会创建新的Runnable
,但是myMethod
将更新run
方法打印的globalString
LOCK
确保一次只有一个线程可以执行Runnable
创建和其他事情的逻辑
确保在作业完成后关闭执行器