public class ThreadName extends Thread{
public void run()
{
System.out.println("线程:"+this.getName());//打印线程名字
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadName thread1=new ThreadName();//创建线程
ThreadName thread2=new ThreadName();
ThreadName thread3=new ThreadName();
thread1.setName("1号");//设置线程名字
thread2.setName("2号");
thread3.setName("3号");
thread1.start();//执行线程
thread2.start();
thread3.start();
}
}
public void run()
{
System.out.println("线程:"+this.getName());//打印线程名字
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ThreadName thread1=new ThreadName();//创建线程
ThreadName thread2=new ThreadName();
ThreadName thread3=new ThreadName();
thread1.setName("1号");//设置线程名字
thread2.setName("2号");
thread3.setName("3号");
thread1.start();//执行线程
thread2.start();
thread3.start();
}
}
这篇博客通过一个简单的Java程序展示了如何创建并启动多个线程,并为每个线程设置并打印出不同的名字。在`ThreadName`类中,`run()`方法用于打印线程名。在`main`方法中,创建了三个线程实例,分别为它们设置了'1号'、'2号'和'3号'的名字,并依次启动。

725

被折叠的 条评论
为什么被折叠?



