Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.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_Multithreading_Threadpool_Executorservice_Threadpoolexecutor - Fatal编程技术网

暂停用户输入java上的线程池

暂停用户输入java上的线程池,java,multithreading,threadpool,executorservice,threadpoolexecutor,Java,Multithreading,Threadpool,Executorservice,Threadpoolexecutor,我有一个线程池,它在我的主类中运行一个while循环 executor = Executors.newFixedThreadPool(numThreads); for (int i = 0; i < numThreads; i++) { Runnable crawl = new crawlThread(this); executor.execute(crawl); } executor.shutdown(); try {

我有一个线程池,它在我的主类中运行一个while循环

   executor = Executors.newFixedThreadPool(numThreads);
   for (int i = 0; i < numThreads; i++) {
        Runnable crawl = new crawlThread(this);
        executor.execute(crawl);
    }
    executor.shutdown();

    try {
        while (!executor.isTerminated()) {
                Thread.sleep(1000);
                this.liveStatus();

                if (System.in.available() != 0) {

                    System.out.println("What would you like to do? (0 = quit, 1 = pause, 2 = resume)");

                    Scanner runinput = new Scanner(System.in);
                    Integer answer = runinput.nextInt();
                    if (answer == 0)
                    {
                        System.out.println("Quitting...");
                        break;
                    } else if (answer == 1)
                    {
                        this.forcePause = true;
                        System.out.println("Pausing...");
                    } else if (answer == 2)
                    {
                        this.forcePause = false;
                        System.out.println("Resuming...");
                    }
                    runinput.close();
                }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
executor=Executors.newFixedThreadPool(numThreads);
for(int i=0;i
虽然我不知道如何在收到用户输入时暂停我的Runnable。我曾尝试从thread/runnable类文件中检查此代码的forcePause状态,以及它是否设置为pause以跳过其执行指令(尽管它不工作)。有没有合适的方法根据用户输入暂停和恢复线程


谢谢

您的代码很好,您应该将其进一步抽象出来,以便捕获异常

示例:

class MyThread extends Thread {

    private volatile boolean running = true; // Run unless told to pause

    ...

    @Override
    public void run()
    {
        for(int i=0 ; ; i++)
        {

            // This is a crude implementation of pausing the thread
            while (!running)
                // Work

            area.setText(i+"");
    }

    public void pauseThread() throws InterruptedException
    {
        running = false;
    }

    public void resumeThread()
    {
        running = true;
    }

}