Java 实例 - 查看线程是否存活

Java 教程 · 2019-02-11 22:58:48

以下实例演示了如何通过继承 Thread 类并使用 isAlive() 方法来检测一个线程是否存活:

TwoThreadAlive.java 文件

public class TwoThreadAlive extends Thread { public void run() { for (int i = 0; i < 10; i++) { printMsg(); } } public void printMsg() { Thread t = Thread.currentThread(); String name = t.getName(); System.out.println("name=" + name); } public static void main(String[] args) { TwoThreadAlive tt = new TwoThreadAlive(); tt.setName("Thread"); System.out.println("before start(), tt.isAlive()=" + tt.isAlive()); tt.start(); System.out.println("just after start(), tt.isAlive()=" + tt.isAlive()); for (int i = 0; i < 10; i++) { tt.printMsg(); } System.out.println("The end of main(), tt.isAlive()=" + tt.isAlive()); } }

以上代码运行输出结果为:

before start(), tt.isAlive()=false
just after start(), tt.isAlive()=true
name=main
name=Thread
name=main
name=main
name=main
name=main
name=main
name=main
name=main
name=main
name=main
name=Thread
name=Thread
name=Thread
name=Thread
name=Thread
The end of main(), tt.isAlive()=true
name=Thread
name=Thread
name=Thread
name=Thread

点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html

查看所有标签

STL源码剖析

STL源码剖析

侯捷 / 华中科技大学出版社 / 2002-6 / 68.00元

学习编程的人都知道,阅读、剖析名家代码乃是提高水平的捷径。源码之前,了无秘密。大师们的缜密思维、经验结晶、技术思路、独到风格,都原原本本体现在源码之中。 这本书所呈现的源码,使读者看到vector的实现、list的实现、heap的实现、deque的实现、Red Black tree的实现、hash table的实现、set/map的实现;看到各种算法(排序、查找、排列组合、数据移动与复制技术......一起来看看 《STL源码剖析》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具