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

需要这些哦
电脑
Eclipse
方式/
1打开eclipse软件 , 如图所示:

2点击菜单栏New-->Java Project,如图所示:

3新建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.");
}
}

5然后点击运行 , Run-->Run as-->Java Application

注重事项sleep()方式是线程休眠
interrupted()方式线程间断
以上内容就是java线程如何关闭的内容啦 , 希望对你有所帮助哦!
猜你喜欢
- 腾讯视频如何退出登录
- 腾讯视频如何切换账号
- 如何掌握生活小常识
- 香雪兰怎么养才矮(香雪兰旭如何养护才矮化)
- C#如何连接数据库
- 如何在一个PDF文件中插入另一个PDF文件?
- 如何清洗洗衣机?
- 高窦猫罐头怎么样 如何能为猫罐头保鲜
- 如何抑制母猫发情 主人应对母猫发情措施
- 猫的爪子有什么作用 猫爪如何修剪和护理
