public class HelloRunnable implements Runnable
{
public void run()
{
//打印当前线程的信息
printCurrentThreadInfo();
System.out.println("Hello from a thread!");
}
public static void main(String args[])
{
//打印当前线程的信息
printCurrentThreadInfo();
//启动线程
(new Thread(new HelloRunnable())).start();
}
/**
* 打印当前线程的信息
*/
public static void printCurrentThreadInfo()
{
//获取当前线程
Thread thread = Thread.currentThread();
//获取线程ID和线程名字
long id = thread.getId();
String name = thread.getName();
//打印线程将要启动的信息
System.out.println("启动线程!" + "ID:" + id + " " + "Name:" + name);
}
}
JAVA 线程学习 获取当前线程信息,区分开各个线程
最新推荐文章于 2022-03-26 10:04:28 发布