Java 多线程学习笔记3

来源:互联网 发布:淘宝店刚开始卖什么好 编辑:程序博客网 时间:2024/06/11 15:01

今天简单记录一下 多线程里面的几个方法

1 currentThread currentThread 方法可以返回当前代码段正在被哪个线程调用信息

package smaug.cloud.provider.thread.t2;/** * Created by naonao on 17/12/9. */public class MyThread implements Runnable {    public MyThread() {        System.out.println("当前线程名字" + Thread.currentThread().getName() + "  ");    }    @Override    public void run() {        System.out.println("当前线程名字" + Thread.currentThread().getName());    }}
package smaug.cloud.provider.thread.t2;/** * Created by naonao on 17/12/9. */public class MyTest {    public static void main(String[] args) {        MyThread myThread = new MyThread();        Thread t = new Thread(myThread, "A");        t.start();    }}

输出

当前线程名字main  当前线程名字A

但是如果代码稍微改一下 就像下面这样

public class MyTest {    public static void main(String[] args) {        MyThread myThread = new MyThread();        Thread t = new Thread(myThread, "A");        //t.start();        t.run();    }}输出是啥nie当前线程名字main  当前线程名字main

再次验证了run 方法只是个普通的方法,如果要创建线程必须用start

在写一个稍微复杂的例子

package smaug.cloud.provider.thread.t2;/** * Created by naonao on 17/12/9. */public class MyThread2 extends Thread {    public MyThread2() {        System.out.println("MyThread  begin --------");        System.out.println("Thread.currentThread().getName() " + Thread.currentThread().getName());        System.out.println("this.getName() " + this.getName());        System.out.println("MyThread  end --------");    }    @Override    public void run() {        System.out.println("run  begin --------");        System.out.println("Thread.currentThread().getName() " + Thread.currentThread().getName());        System.out.println("this.getName() " + this.getName());        System.out.println("run  end --------");    }}

输出

MyThread  begin --------Thread.currentThread().getName() mainthis.getName() Thread-0MyThread  end --------run  begin --------Thread.currentThread().getName() babthis.getName() babrun  end --------

2.isAlive 判断线程是否存活也就是是否还在运行

package smaug.cloud.provider.thread.t3;/** * Created by naonao on 17/12/9. */public class MyThread extends Thread {    @Override    public void run() {        System.out.println(Thread.currentThread().getName() + " isAlive " +this.isAlive());    }}
package smaug.cloud.provider.thread.t3;/** * Created by naonao on 17/12/9. */public class MyTest {    public static void main(String[] args) {        MyThread myThread = new MyThread();        System.out.println("myThread is alive " + myThread.isAlive());        myThread.start();        System.out.println("myThread is alive " + myThread.isAlive());    }}

得到的输出

myThread is alive falseThread-0 isAlive truemyThread is alive false

3 sleep sleep方法是让当前线程休眠一段时间

package smaug.cloud.provider.thread.t4;/** * Created by naonao on 17/12/9. */public class MyThread implements Runnable {    @Override    public void run() {        try {            System.out.println("begin " + System.currentTimeMillis());            Thread.sleep(2000);            System.out.println("end   " + System.currentTimeMillis());        } catch (InterruptedException e) {        }    }}
package smaug.cloud.provider.thread.t4;/** * Created by naonao on 17/12/9. */public class MyTest {    public static void main(String[] args) {        MyThread myThread = new MyThread();        Thread t = new Thread(myThread);        t.start();    }}

从输出时间来看,它睡着了两秒钟

begin 1512820680346end   1512820682351
原创粉丝点击