Java多线程总结(2) — 线程生命周期中常用方法

本文回顾了线程的五种状态,并详细介绍了Java中线程的sleep()、join()、interrupt()、yield()、wait()、notify()和notifyAll()等生命周期中的关键方法,探讨了它们的使用场景和效果,以及中断机制和线程通信的基本原理。
摘要由CSDN通过智能技术生成

1. 线程的状态回顾

  线程从创建到最终的消亡,要经历若干个状态。一般来说,线程包括以下这几个状态:(原文更好理解,就不翻译了)

  1. New When we create a new Thread object using new operator, thread state is New Thread. At this point, thread is not alive and it’s a state internal to Java programming.
  2. Runnable When we call start() function on Thread object, it’s state is changed to Runnable and the control is given to Thread scheduler to finish it’s execution. Whether to run this thread instantly or keep it in runnable thread pool before running it depends on the OS implementation of thread scheduler.
  3. Running When thread is executing, it’s state is changed to Running. Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to Running and CPU starts executing this thread. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion of run() method or waiting for some resources like IO,Network,etc.
  4. Blocked/Waiting A thread can be waiting for other thread to finish using thread join or it can be waiting for some resources to available, for example producer consumer problem (生产者-消费者问题)or waiter notifier implementation or IO resources, then it’s state is changed to Waiting. Once the thread wait state is over, it’s state is changed to Runnable and it’s moved back to runnable thread pool.
  5. Dead Once the thread finished executing, it’s state is changed to Dead and it’s considered to be not alive.

这里写图片描述

2. 线程生命周期中常用方法总结

2.1 sleep(long millis)

  运行的线程调用sleep方法进入阻塞状态(不会释放任何对象锁),睡眠完之后继续进入就绪状态,等待线程调度。

public void sleepMethod() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                long start = System.currentTimeMillis();
                Thread.sleep(2000);
                long end = System.currentTimeMillis();
                System.out.println(end -start);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

  输出结果可能稍微大于2000ms。Thread.sleep() 和线程调度模块交互,将当前线程设置为阻塞状态,等待指定的时间。一旦等待时间结束,线程的状态切换为可执行状态,等待CPU的时间片轮转。所以等待的确切时间取决于操作系统的线程调度机制和定时器的精度。
  注意:

  1. 线程sleep期间,并没有失去此线程所获得的监视器(monitors)和锁(locks)。
  2. 线程sleep期间,任何其他的线程抖可以中断此线程,一旦中断,则抛出InterruptedException.

2.2 join()

  join()可以用来暂停当前线程的执行,让调用join()的线程强制运行完成或执行指定的时间。
public final void join()
public final synchronized void join(long millis)
public final synchronized void join(long millis, int nanos)
  测试案例:

public void joinMethod() {
    Thread thread1 = new Thread(new MyRunnable(), "thread1");
    Thread thread2 = new Thread(new MyRunnable(), "thread2");
    Thread thread3 = new Thread(new MyRunnable(), 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sunny Mornings

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值