Java线程入门二

参考 Java核心技术系列:Java多线程编程核心技术

1. currentThread方法

public class MyThread extends Thread {

    public MyThread() {
        System.out.println("MyThread.MyThread: "+MyThread.currentThread().getName());
    }

    @Override
    public void run() {
        
        System.out.println("run: "+ MyThread.currentThread().getName());
    }
}

public class Demo01 {

    public static void main(String[] args) {

        MyThread run = new MyThread();

        run.start();
    }
}

输出结果如下:
MyThread.MyThread: main --> run对象创建是main线程
run: Thread-0 --> run方法执行是自己启动的线程

public class CountOperate extends Thread {

    public CountOperate() {
        System.out.println("CountOperate -------- begin");
        System.out.println("name: "+Thread.currentThread().getName());

        System.out.println("this.getName():" + this.getName());

        System.out.println("CountOperate -------- end");
    }

    @Override
    public void run() {

        System.out.println("run -------- begin");
        System.out.println("threadName: "+Thread.currentThread().getName());

        System.out.println("this.getName():" + this.getName());

        System.out.println("run -------- end");
    }
}

public static void main(String[] args) throws InterruptedException {

	CountOperate run = new CountOperate();
	Thread thread = new Thread(run);
	thread.setName("a");
	thread.start();
	/*run.setName("b");
	run.start();*/
}

输出结果如下:
CountOperate -------- begin
name: main
this.getName():Thread-0
CountOperate -------- end
run -------- begin
threadName: a
this.getName():Thread-0
run -------- end

改变main方法代码如下:

public static void main(String[] args) {

	CountOperate run = new CountOperate();
	/*Thread thread = new Thread(run);
	thread.setName("a");
	thread.start();*/
	run.setName("b");
	run.start();
}

输出结果如下:
CountOperate -------- begin
name: main
this.getName():Thread-0
CountOperate -------- end
run -------- begin
threadName: b
this.getName():b
run -------- end

造成两个结果不一样的原因:Thread.currentThread()和this的区别

2. isAlive

public class MyThread extends Thread {
    @Override
    public void run() {

        System.out.println("run: "+ this.isAlive());
    }
}
public static void main(String[] args) {

	MyThread run = new MyThread();
	//MyThread t = new MyThread();
	//Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run还没有执行完毕
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false 
}

输出结果如下:
false --> 线程尚未启动
true --> run线程还没有执行完毕
run: true --> 执行MyThread线程里面run方法
false --> run线程在1s内执行完毕了

修改main方法代码如下:

public static void main(String[] args) {

	//MyThread run = new MyThread();
	MyThread t = new MyThread();
	Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run还没有执行完毕
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false run在1s内执行完毕了
}

输出结果如下:

false
true
run: false 和之前的唯一区别:Thread.currentThread()和this的区别
false

更复杂的示例:

public class CountOperate extends Thread {

    public CountOperate() {
        System.out.println("CountOperate -------- begin");
        System.out.println("thread: " + Thread.currentThread().getName());//thread: main
        System.out.println("thread-isAlive: " + Thread.currentThread().isAlive());//thread-isAlive: true
        System.out.println("this.getName():" + this.getName());//this.getName():Thread-0
        System.out.println("this.isAlive:" + this.isAlive());//this.isAlive:false
        System.out.println("CountOperate -------- end");
    }

    @Override
    public void run() {

        System.out.println("run -------- begin");
        System.out.println("thread: " + Thread.currentThread().getName());//thread: b
        System.out.println("thread-isAlive: " + Thread.currentThread().isAlive());//thread-isAlive: true
        System.out.println("this.getName():" + this.getName());//this.getName():Thread-0
        System.out.println("this.isAlive:" + this.isAlive());//this.isAlive:false
        System.out.println("run -------- end");
    }
}
public static void main(String[] args) {

	//CountOperate run = new CountOperate();
	CountOperate t = new CountOperate();
	Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run还没有执行完毕
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false run在1s内执行完毕了
}

输出结果如下:
CountOperate -------- begin
thread: main
thread-isAlive: true
this.getName():Thread-0
this.isAlive:false
CountOperate -------- end
false
true
run -------- begin
thread: b
thread-isAlive: true
this.getName():Thread-0
this.isAlive:false
run -------- end
false

修改main方法代码如下:

public static void main(String[] args) {

	CountOperate run = new CountOperate();
	//CountOperate t = new CountOperate();
	//Thread run = new Thread(t);
	System.out.println(run.isAlive());//false
	run.setName("b");
	run.start();
	System.out.println(run.isAlive());//true run还没有执行完毕
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println(run.isAlive());//false run在1s内执行完毕了
}

输出结果如下:
CountOperate -------- begin
thread: main
thread-isAlive: true
this.getName():Thread-0
this.isAlive:false
CountOperate -------- end
false
true
run -------- begin
thread: b
thread-isAlive: true
this.getName():b
this.isAlive:true
run -------- end
false

3. sleep() 略去

在指定的毫秒数内让"当前正在执行的线程" 休眠(暂停执行)
当前正在执行的线程:是指 this.currentThread返回的线程

4. getId: 获取线程的唯一标识 略去

猜你喜欢

转载自blog.csdn.net/guo20082200/article/details/83239287
今日推荐