Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ScheduledExecutorService-更新内部数据_Java_Android - Fatal编程技术网

Java 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,

我有ScheduledExecutorService,我正在尝试更新内部数据,但没有结果

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
创建和其他事情的逻辑

确保在作业完成后关闭执行器