[Java] java控制线程运行 →→→→→进入此内容的聊天室

来自 , 2020-04-03, 写在 Java, 查看 100 次.
URL http://www.code666.cn/view/8b406655
  1. public class PrintHelloThread extends Thread
  2. {
  3.  public  static void  SetPrintHelloThreadFlag(boolean flag)
  4.  {
  5.   isPrint = flag;
  6.  }
  7.  private static boolean isPrint = true;
  8.  public void run()
  9.  {
  10.   try
  11.   {
  12.    while(isPrint)
  13.    {
  14.     Thread.sleep(1);
  15.     System.out.println("Hello");
  16.    }
  17.   }
  18.   catch(Exception ex)
  19.   {}
  20.  }
  21.  public  static void main(String[] args)
  22.  {
  23.   PrintHelloThread printThread = new PrintHelloThread();
  24.   CaculateRunnable r = new CaculateRunnable();
  25.   Thread t = new Thread(r);
  26.   printThread.start();
  27.   t.start();
  28.   
  29.  }
  30. }
  31. class CaculateRunnable implements Runnable
  32. {
  33.  public void run()
  34.  {
  35.   int sum =0;
  36.   for(int i = 0;i<1000000;i++)
  37.   {
  38.    sum +=i;
  39.   }
  40.   System.out.println("sum = "+sum);
  41.   PrintHelloThread.SetPrintHelloThreadFlag(false);
  42.   
  43.  }
  44. }//源代码片段来自云代码http://yuncode.net
  45.                        

回复 "java控制线程运行"

这儿你可以回复上面这条便签

captcha