/** * ThreadDemo2.java * @author cjc * */ public class ThreadDemo2 { public static void main(String[] args) { Thread th=new Thread(new TestThread()); th.setName("子线程1"); th.start(); for(int i=0;i<10;i++) { System.out.println("Thread "+Thread.currentThread().getName()+" is running..."); System.out.println("Thread 子线程1 "+(th.isAlive()?"is":"is not")+" alive..."); } } }
/** * TestThread.java * @author cjc * */ public class TestThread implements Runnable { public void run() { for(int i=0;i<5;i++) { System.out.println("Thread "+Thread.currentThread().getName()+" is running..."); } } }
java线程操作方法setName,getName,isAlive
时间: 2024-12-07 18:19:10