Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.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可运行的混乱_Java - Fatal编程技术网

Java可运行的混乱

Java可运行的混乱,java,Java,正如我已经提到的,我对编程界还是相当陌生的。 我目前面临runable的问题。当我检查boolean shutdown的状态时,代码一直在运行,而不是预期每30秒运行一次。 到目前为止,我的代码是: import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class TestC

正如我已经提到的,我对编程界还是相当陌生的。 我目前面临runable的问题。当我检查boolean shutdown的状态时,代码一直在运行,而不是预期每30秒运行一次。 到目前为止,我的代码是:

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestClass {
static boolean shutdown;


public static void main(String[] args) {


    notify(30);
}

private static void notify(int num) {

    Runnable runner = new Runnable() {
        public void run() {
            while(!shutdown){
            System.out.println("running");
        }
    }
    };

    ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
    exec.scheduleAtFixedRate(runner, 0, num, TimeUnit.SECONDS);
}

}

Edit:我需要以某种方式停止该方法的运行,这就是我使用布尔关闭的原因。

ScheduledExecutorService将在指定的时间段后将
可运行的
实例从
线程池
传递到可用的
线程中

while循环会导致线程永远紧密旋转。我敢打赌,运行此功能时,您的计算机显示出100%的核心使用率

ScheduledExecutorService.scheduleAtFixedRate
返回一个
ScheduledFuture
对象。您需要对其调用
cancel()
,以停止任务的执行

您需要删除while循环并取消
ScheduledFuture

我在记事本中输入了这个-它可能无法编译,但你应该明白了。

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class TestClass {
static ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
static boolean shutdown = false;


public static void main(String[] args) throws Exception {
     new ShutdownChecker(30);
     Thread.sleep(90_000);
     shutdown = true;
}

private static class ShutdownChecker implements Runnable
{
    private final ScheduledFuture<?> future;

    public ShutdownChecker(int time)
    {
        future = TestClass.exec.scheduleAtFixedRate(this, 0, num, TimeUnit.SECONDS);
    }

    public void run()
    {
        if(TestClass.shutdown)
        {
            future.cancel();
            System.out.println("not running");
            return;
        }

        System.out.println("running");
    }
}

}
导入java.util.concurrent.Executors;
导入java.util.concurrent.ScheduledExecutorService;
导入java.util.concurrent.TimeUnit;
公共类TestClass{
静态ScheduledExecutorService exec=执行者。newScheduledThreadPool(1);
静态布尔关闭=假;
公共静态void main(字符串[]args)引发异常{
新的关机检查器(30);
睡眠(90_000);
关机=真;
}
私有静态类ShutdownChecker实现Runnable
{
私人最终计划未来;
公共关闭检查器(int时间)
{
future=TestClass.exec.scheduleAtFixedRate(this,0,num,TimeUnit.SECONDS);
}
公开募捐
{
if(TestClass.shutdown)
{
future.cancel();
System.out.println(“未运行”);
返回;
}
System.out.println(“运行”);
}
}
}

删除while循环。@实际上我确实需要while循环,因为我想以某种方式停止它的执行。所以我想用一个布尔值来控制它是否应该运行。executor服务将每隔30秒调用
run()
,当到达方法末尾时,执行将自动停止。这不是你想要做的吗?@beatngu13好吧,这个方法实际上似乎是永恒的,因为每30秒就会有一个新的线程重生。我需要在运行时以某种方式停止它。执行器不会每30秒产生一个新线程。您初始化了一个大小为1的池,这意味着它有一个将被重用的线程。在第一次调用
run()
之后,由于while循环,该线程将永远运行。即使您设置了
shutdown=true
,它也可能不会停止,因为变量不是
易失性的
。您能给我提供一个代码片段吗?我知道,但我试图实现的是停止它。您还需要一个线程来监视布尔值的状态。如果布尔值为true,请关闭ScheduledExecutorService。执行器不会在每个周期后生成新线程。把它读出来。