Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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 - Fatal编程技术网

Java 停止可运行对象是否等同于停止线程?

Java 停止可运行对象是否等同于停止线程?,java,multithreading,Java,Multithreading,鉴于此代码: public class Game implements Runnable { private volatile boolean stop; public Game() { ... } public void run() { while(!stop) { // play game } } public void stopGame() { stop = true; } } public class Mai

鉴于此代码:

public class Game implements Runnable {
  private volatile boolean stop;

  public Game() {
    ...
  }

  public void run() {
    while(!stop) {
      // play game
    }
  }

  public void stopGame() {
    stop = true;
  }
}

public class Main {

  public static void main(String[] args){
    Game g = new Game(); // Game class implements Runnable
    Thread t = new Thread(g);
    t.start();
    // do the two method calls below effectively do the same thing?
    t.interrupt();
    g.stopGame(); 
  }

}
stopGame()杀死线程的效果是否与使用中断或java.lang.thread方法杀死线程一样有效?(我不太熟悉如何使用线程方法杀死线程。)

另外,如果stopGame()是杀死线程的最佳方法,那么如果他们只能访问如下线程实例,是否可以调用它:

public static void Main(String[] args){
  List<Thread> threads = new ArrayList<Thread>();
  threads.add(new Thread(new Game()));
  // can you access the Game instance given the Thread instance 
  // or do you need to hold onto the reference of the Game instance?
  threads.get(0).stopGame(); // for example, this won't work.
}
publicstaticvoidmain(字符串[]args){
List threads=new ArrayList();
添加(新线程(新游戏());
//在给定线程实例的情况下,您可以访问游戏实例吗
//还是需要保留游戏实例的引用?
threads.get(0).stopGame();//例如,这不起作用。
}
“终止”线程是一种异常情况。

interrupt()
这:

可以是:

  public void run() {            
      while (!Thread.currentThread().isInterrupted()) {
         // play game
      }                
  }
但就我个人而言,我发现停止线程是一种激进的方式,因为任何中断线程的阻塞调用都会被唤醒。
因此,当您处于正常退出状态时,您可能被迫处理此过早退出,这可能会造成不一致。
使用
boolean
标志可以让线程代码负责执行其认为应该执行的作业,而不强制存在其阻塞调用。

这并不意味着永远不应该处理
中断异常
,但我认为对于正常情况(从用户请求中退出),按照需要将代码变得更复杂是没有意义的。

stopGame()是否像使用 中断,或者使用java.lang.Thread方法终止线程

结束线程的合适方式是允许它终止其执行:即方法的
run()
完成其执行。

在这里使用
布尔
条件和
while
可以:

public void run() {
    while(!stop) {
      // play game
    }
  }
另外,如果stopGame()是杀死线程的最佳方法,那么 如果他们只有访问线程实例的权限,就没有任何方法可以调用它 如下图所示:

public static void Main(String[] args){
  List<Thread> threads = new ArrayList<Thread>();
  threads.add(new Thread(new Game()));
  // can you access the Game instance given the Thread instance 
  // or do you need to hold onto the reference of the Game instance?
  threads.get(0).stopGame(); // for example, this won't work.
}
您可以这样做,例如:

  Game game = new Game(); 
  game.start();
  ...
  game.stopGame();
“杀死”线程是一种异常情况。

interrupt()
这:

可以是:

  public void run() {            
      while (!Thread.currentThread().isInterrupted()) {
         // play game
      }                
  }
但就我个人而言,我发现停止线程是一种激进的方式,因为任何中断线程的阻塞调用都会被唤醒。
因此,当您处于正常退出状态时,您可能被迫处理此过早退出,这可能会造成不一致。
使用
boolean
标志可以让线程代码负责执行其认为应该执行的作业,而不强制存在其阻塞调用。

这并不意味着永远不应该处理
中断异常
,但我认为对于正常情况(从用户请求中退出),按照需要将代码变得更复杂是没有意义的。

stopGame()是否像使用 中断,或者使用java.lang.Thread方法终止线程

结束线程的合适方式是允许它终止其执行:即方法的
run()
完成其执行。

在这里使用
布尔
条件和
while
可以:

public void run() {
    while(!stop) {
      // play game
    }
  }
另外,如果stopGame()是杀死线程的最佳方法,那么 如果他们只有访问线程实例的权限,就没有任何方法可以调用它 如下图所示:

public static void Main(String[] args){
  List<Thread> threads = new ArrayList<Thread>();
  threads.add(new Thread(new Game()));
  // can you access the Game instance given the Thread instance 
  // or do you need to hold onto the reference of the Game instance?
  threads.get(0).stopGame(); // for example, this won't work.
}
您可以这样做,例如:

  Game game = new Game(); 
  game.start();
  ...
  game.stopGame();
stopGame()停止线程的效果是否与使用中断或java.lang.thread方法终止线程一样有效?(我不太熟悉如何使用线程方法杀死线程。)

停止线程的最佳方法是从run()方法返回

您的stopGame()调用会在循环的下一个过程中导致这种情况。但如果该循环包括阻塞调用,则下一次传递可能会在延迟后发生,或者永远不会发生

不推荐使用终止线程的方法,因为它们本质上是不安全的

Thread.interrupt()
不能保证停止任意线程。它会唤醒一些阻塞呼叫。Runnable在捕获InterruptedException时停止是常见的,但不是通用的。从:

中断是对线程的一种指示,它应该停止正在做的事情并做其他事情。由程序员决定线程如何响应中断,但线程终止是很常见的

一种方法是设置用户定义的标志并调用Thread.interrupt()。这将中断任何阻塞调用,即使Runnable调用的代码捕获了InterruptedException或清除了中断状态标志,也可以工作

另外,如果stopGame()是杀死线程的最佳方法,那么如果他们只能访问如下线程实例,是否可以调用它:

public static void Main(String[] args){
  List<Thread> threads = new ArrayList<Thread>();
  threads.add(new Thread(new Game()));
  // can you access the Game instance given the Thread instance 
  // or do you need to hold onto the reference of the Game instance?
  threads.get(0).stopGame(); // for example, this won't work.
}
您可以对线程进行子类化以保存游戏,并在子类中提供停止游戏的方法。但是,单独跟踪可运行实例通常更容易

stopGame()停止线程的效果是否与使用中断或java.lang.thread方法终止线程一样有效?(我不太熟悉如何使用线程方法杀死线程。)

停止线程的最佳方法是从run()方法返回

您的stopGame()调用会在循环的下一个过程中导致这种情况。但如果该循环包括阻塞调用,则下一次传递可能会在延迟后发生,或者永远不会发生

不推荐使用终止线程的方法,因为它们本身就不安全。