如何在其他功能中停止Thread

时间:2015-01-01 10:47:32

标签: multithreading function

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

1 个答案:

答案 0 :(得分:0)

首先,代码可以帮助回答这个问题。但是,你可以做的就是(如果我甚至理解你的要求):

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.)
 }