这时对你程序的修改- public class StartThreadDemo extends Thread{
- public void run(){
- for(int i=0;i<2;i++){
- printTName();
- }
- }
- public void printTName(){
- String name=Thread.currentThread().getName();
- System.out.println("当前线程的名字 " + name);
- }
- public static void main(String[] args){
- StartThreadDemo t = new StartThreadDemo();
- t.setName("test thread");
- System.out.println("调用start()方法之前,t.isAlive() = " + t.isAlive());
- System.out.println("........................");
- t.start();
- System.out.println("调用start()方法之后,t.isAlive() = " + t.isAlive());
- System.out.println("........................");
- for(int i=0;i<2;i++){
- t.printTName();
- try{
- Thread.sleep(200);
- System.out.println("线程休眠时, t.isAlive() = " + t.isAlive());
- System.out.println("........................");
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- }
复制代码
这时运行结果:
调用start()方法之前,t.isAlive() = false
........................
调用start()方法之后,t.isAlive() = true
........................
当前线程的名字 main
当前线程的名字 test thread
当前线程的名字 test thread
线程休眠时, t.isAlive() = false
........................
当前线程的名字 main
线程休眠时, t.isAlive() = false
........................ |