Java线程问题

Yat*_*oel 2 java multithreading

我想通过使用布尔字段来停止线程.我已经实现了一些代码来执行此操作,如下所示:

我的线程类是这样的:

public class ParserThread implements Runnable {
    private volatile boolean stopped = false;

    public void stopTheThread() {
        stopped = true;
    }
    :
    :
}
Run Code Online (Sandbox Code Playgroud)

以下是从函数start()启动10个线程的主线程

public class Main() {
    Thread [] threads;

    public void start() {
        for(int i = 0; i < 10; i++) {
            threads[i] = new Thread(new ParserThread());
        }       
    }

    public void stop() {
        // code to stop all the threads
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想调用ParserThread的stop方法来设置"stopped = true"来停止线程.我想要为所有10个线程完成这件事.

我该如何调用stop方法.我希望它在Main类的stopAllThreads()方法中完成.

Bri*_*new 11

看一下Executor框架.如果您使用ExecutorService,则可以将所有工作提交为Runnables.执行程序框架将跟踪所有这些线程,并且所有托管线程将通过该shutdownNow()方法接收关闭请求(中断).

你的线程必须正确处理中断.有关更多信息,请参阅此文章.

使用Executor框架来管理线程集,收集结果,处理异常等通常更容易.


Ada*_*ski 6

这是使用ExecutorService的另一种替代方法.它比直接操作Threads需要更少的代码,并提供了两种停止工作线程的替代方法.它还允许您潜在地捕获每个工作项的结果(如果您使用Callable实例而不是Runnables).即使您不希望捕获显式返回值,它也允许您轻松地将任何异常编组回主线程.

// Array of Runnables that we wish to process.
ParserThread[] parserThreads = ...

// Create executor service containing as many threads as there are Runnables.
// (Could potentially have less threads and let some work items be processed
// sequentially.)
ExecutorService execService = Executors.newFixedThreadPool(parserThreads.length);

// Submit each work item.  Could potentially store a reference to the result here
// to capture exceptions.
for (ParserThread runnable : parserThreads) {
  execService.submit(runnable);
}
Run Code Online (Sandbox Code Playgroud)

然后关闭所有可以调用的线程:

executorService.shutDown(); // Initiates an orderly shut-down of the service.
Run Code Online (Sandbox Code Playgroud)

... 要么

executorService.shutDownNow(); // Stops all tasks, interrupting waiting tasks.
Run Code Online (Sandbox Code Playgroud)


Gra*_*dpa 5

您必须自己保留ParserThread对象:

public class Main() {
    ParserThread[] parserthreads = new ParserThread[10];

    public void start() {
        for(int i = 0; i < 10; i++) {
            parserthreads[i] = new ParserThread();
            new Thread(parserthreads[i]).start();
        }       
    }

    public void stop() {
        for (int i = 0; i < 10; i++) {
            parserthreads[i].stopTheThread();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要Thread对象本身(例如,join()与它们一起使用),要么单独跟踪它们,要么让ParserThread继承自Thread:

public class ParserThread extends Thread {
    private volatile boolean stopped = false;
    public void stopTheThread() {
        stopped = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

另外,正如其他人所指出的那样,在课堂上无stopTheThread()用地复制interrupt方法Thread,所以你最好的选择是:

public class Main() {
    Thread[] parserthreads = new Thread[10];

    public void start() {
        for(int i = 0; i < 10; i++) {
            parserthreads[i] = new Thread(new ParserThread());
            parserthreads[i].start();
        }       
    }

    public void stop() {
        for (int i = 0; i < 10; i++) {
            parserthreads[i].interrupt();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在ParserThread中,if (Thread.currentThread().isInterrupted())尽可能多地打电话.

  • 虽然这个答案确实解决了你的原始问题,但我认为这是错误的(从某种意义上说,你不需要那些额外的机器,因为Java已经提供了一个合作中断()). (3认同)