Java多线程Sleep详解

1.Sleep->线程暂停,暂停的是currentThread;
        如果是Thread初始化MyThread类,多个线程共享一个MyThread实例,那么run函数运行时,currentThread就是Thread1、Thread2....
2.Main本身是一个线程,语句顺序执行,与其他线程独立
       如下所示代码,初始化一个MyThread类,由Thread类初始化3个线程,分别为thread_a/thread_b/thread_c 分别进入同一个MyThread实例的run方法;run内含有sleep(5000)语句,main含有sleep(2000)语句;

public class MyThread extends Thread{
    public MyThread(String name){
        this.setName(name);
        System.out.println("构造方法 currentThread: "+currentThread().getName());
        System.out.println("构造方法 currentThread:"+currentThread().isAlive());
        System.out.println("构造方法 this:"+this.getName());
        System.out.println("构造方法 this"+this.isAlive());
    }

    @Override
    public void run() {
        System.out.println("进入Run方法:"+currentThread().getName()+"调用:进入时间"+System.currentTimeMillis());
        super.run();
        System.out.println("Run方法 currentThread: "+currentThread().getName());
        System.out.println("Run方法 currentThread.isAlive:"+currentThread().isAlive()+currentThread().getName());
        try {
            System.out.println("************"+Thread.currentThread().getName()+"暂停5s");
            sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Run方法 this:"+this.getName()+"由谁调用:"+currentThread().getName());
        System.out.println("Run方法 this.isAlive:"+this.isAlive()+"由谁调用:"+currentThread().getName());

    }
}
public class Run {
    public static void main(String[] args) {
        System.out.println(Thread.currentThread().getName()+" Main线程");
        MyThread myThread = new MyThread("MyThreadA");
        Thread thread_a =new Thread(myThread,"threadA");
        Thread thread_b =new Thread(myThread,"threadB");
        Thread thread_c =new Thread(myThread,"threadC");
        System.out.println(Thread.currentThread().getName()+"开始*******");
        System.out.println("a_start");
        thread_a.start();
        System.out.println("b_start");
        thread_b.start();
        System.out.println("c_start");
        thread_c.start();
        try {
            System.out.println("************"+Thread.currentThread().getName()+"暂停5s");
            sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"结束!在"+System.currentTimeMillis());

如果main没有sleep语句,在执行第一个run语句之前就会输出main结束!,因为main线程是顺序执行的,执行thread_a.start()之后,thread_a独立执行,不影响main线程继续执行thread_b开启thread_b线程,因而main函数很快执行完,如果main加上sleep(2000)则2s时才会输出main结束!再过3s也就是5s后,thread_a三个线程继续执行sleep后面的语句;



main Main线程
构造方法 currentThread: main
构造方法 currentThread:true
构造方法 this:MyThreadA
构造方法 thisfalse
main开始*******
a_start
b_start
c_start
************main暂停5s
进入Run方法:threadA调用:进入时间1504773621446
进入Run方法:threadB调用:进入时间1504773621446
Run方法 currentThread: threadB
Run方法 currentThread.isAlive:truethreadB
************threadB暂停5s
进入Run方法:threadC调用:进入时间1504773621446
Run方法 currentThread: threadC
Run方法 currentThread.isAlive:truethreadC
************threadC暂停5s
Run方法 currentThread: threadA
Run方法 currentThread.isAlive:truethreadA
************threadA暂停5s
main结束!在1504773623446
Run方法 this:MyThreadA由谁调用:threadC
Run方法 this:MyThreadA由谁调用:threadB
Run方法 this:MyThreadA由谁调用:threadA
Run方法 this.isAlive:false由谁调用:threadC
Run方法 this.isAlive:false由谁调用:threadA
Run方法 this.isAlive:false由谁调用:threadB


运行结果:sychronized方法

main Main线程
构造方法 currentThread: main
构造方法 currentThread:true
构造方法 this:MyThreadA
构造方法 thisfalse
main开始*******
a_start
b_start
c_start
************main暂停5s
进入Run方法:threadA调用:进入时间1504773534417
Run方法 currentThread: threadA
Run方法 currentThread.isAlive:truethreadA
************threadA暂停5s
main结束!在1504773536421
Run方法 this:MyThreadA由谁调用:threadA
Run方法 this.isAlive:false由谁调用:threadA
进入Run方法:threadC调用:进入时间1504773539417
Run方法 currentThread: threadC
Run方法 currentThread.isAlive:truethreadC
************threadC暂停5s
Run方法 this:MyThreadA由谁调用:threadC
Run方法 this.isAlive:false由谁调用:threadC
进入Run方法:threadB调用:进入时间1504773544432
Run方法 currentThread: threadB
Run方法 currentThread.isAlive:truethreadB
************threadB暂停5s


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值