java线程如何关闭

Java线程如何关闭 。 关闭的方法:interrupt() .

java线程如何关闭



需要这些哦
电脑
Eclipse
方式/
1打开eclipse软件 , 如图所示:
java线程如何关闭



2点击菜单栏New-->Java Project,如图所示:
java线程如何关闭



3新建java项目当作功之后 , 新建一个线程类 ,
如图所示:
java线程如何关闭



4在新建线程类中键入如下代码:
package com.tang.java.demo;


public class ThreadDemo {


public static void main(String[] args) {
new NewThread(); // 建立一个新线程
     try {
        for(int i = 5; i > 0; i--) {
          System.out.println("本家儿线程: " + i);
          //线程遏制100毫秒
          Thread.sleep(100);
          //遏制线程方式
          Thread.interrupted();
         
          
          
        }
     } catch (InterruptedException e) {
        System.out.println("本家儿线程中止.");
     }
     System.out.println("本家儿线程退出.");
     
     


}


}
//建立一个新的线程
class NewThread implements Runnable {
Thread t;
【java线程如何关闭】NewThread() {
   // 建立第二个新线程
   t = new Thread(this, "Demo Thread");
   System.out.println("子线程 thread: " + t);
   t.start(); // 起头线程
}


// 第二个线程进口
public void run() {
   try {
      for(int i = 5; i > 0; i--) {
         System.out.println("子线程 Thread: " + i);
         // 暂停线程
         Thread.sleep(50);
      }
  } catch (InterruptedException e) {
      System.out.println("子线程 interrupted.");
  }
  System.out.println("子线程退出 thread.");
}
}
java线程如何关闭



5然后点击运行 , Run-->Run as-->Java Application
java线程如何关闭



注重事项sleep()方式是线程休眠
interrupted()方式线程间断

以上内容就是java线程如何关闭的内容啦 , 希望对你有所帮助哦!

    猜你喜欢