Multithreading 如何停止其他函数中的线程

Multithreading 如何停止其他函数中的线程,multithreading,function,Multithreading,Function,我有一个关于线的游戏。我在函数StartGame()中新建了此线程,并希望在函数GameOver()中停止此线程。如何做到这一点?首先,代码有助于回答这个问题。然而,你能做的是(如果我能理解你的要求): boolean running=false; 公共无效StartName() 如果(线程!==null) { thread=new thread(这个)你能提供更多的信息吗,比如你使用的是什么语言和什么系统。没有这些细节,这个问题还不够清楚。还有,你说的“在其他函数中”是什么意思?你的意思是从另

我有一个关于线的游戏。我在函数StartGame()中新建了此线程,并希望在函数GameOver()中停止此线程。如何做到这一点?

首先,代码有助于回答这个问题。然而,你能做的是(如果我能理解你的要求):

boolean running=false;
公共无效StartName()
如果(线程!==null)
{

thread=new thread(这个)你能提供更多的信息吗,比如你使用的是什么语言和什么系统。没有这些细节,这个问题还不够清楚。还有,你说的“在其他函数中”是什么意思?你的意思是从另一条线索?如果我帮助你,如果你能接受我的回答,我将不胜感激,谢谢。如果不能,问我你的担忧,我会回答。@Thoailenh
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.)
 }