线程学习 获取当前线程信息,区分开各个线程
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);
}
} 2 楼 hanmiao 2012-10-17 楼主的文章为什么不设置个代码高亮显示呢,排版看上去特别乱。