如何在其他函数中停止线程

How to stop Thread in other function

我有一个线程游戏。我在函数 StartGame() 中新建了这个线程,我想在函数 GameOver() 中停止这个线程。怎么做到的?

首先,代码可以帮助回答问题。但是,你可以做的是(如果我什至明白你在问什么):

boolean running = false;
public void StartGame()
if (thread !== null)
   {
    thread= new Thread(this) <-------(if your class implements Runnable)
    running = true
    thread.start();
   }
 public void run()
 {
 while(running){
 //Code you want to execute  
 EndGame()
 }
 public void EndGame()
 {
 if (thread == null)
   {
   return;
   }
 else
  running = false;
  //anything else you want to do (exit, any other methods, etc.)
 }