package unit8; public class Mainthread { public static void main(String[] args) { Thread t = new Thread().currentThread(); System.out.println("当前的线程是:"+t); t.setName("MyJavaThread"); System.out.println("当前的线程是:"+t); try{ for(int i=0;i<3;i++){ System.out.println(i); Thread.sleep(1500); } }catch(InterruptedException e){ System.out.println("主线程被中断"); } System.out.println("主线程结束"); } }
执行结果:
当前的线程是:Thread[main,5,main] 当前的线程是:Thread[MyJavaThread,5,main] 0 1 2 主线程结束