温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java 线程的生命周期有哪些

发布时间:2021-06-15 15:09:17 来源:亿速云 阅读:111 作者:Leah 栏目:编程语言

本篇文章为大家展示了Java 线程的生命周期有哪些,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

一 代码

/**
* @Title: ThreadStatus.java
* @Description: TODO(演示线程的生命状态)
*/
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.TimeUnit;
public class ThreadStatus
{
  private static Lock lock = new ReentrantLock();
  public static void main(String[] args)
  {
    new Thread(new TimeWaiting(), "TimeWaitingThread").start();
    new Thread(new Waiting(), "WaitingThread").start();
    // 使用两个Blocked线程,一个获取锁,一个被阻塞
    new Thread(new Blocked(), "BThread-1").start();
    new Thread(new Blocked(), "BThread-2").start();
    new Thread(new Sync(), "SyncThread-1").start();
    new Thread(new Sync(), "SyncThread-2").start();
  }
  //该线程不断地进入随眠
  static class TimeWaiting implements Runnable
  {
    public void run() {
      while (true)
      {
        try {
          TimeUnit.SECONDS.sleep(5);
          System.out.println("I am TimeWaiting Thread: "+ Thread.currentThread().getName());
        } catch (InterruptedException e) { }
      }
    }
  }
  //该线程在Waiting.class实例上等待
  static class Waiting implements Runnable
  {
    public void run( )
    {
      while (true)
      {
        synchronized (Waiting.class)
        {
          try {
            System.out.println("I am Waiting Thread: "+ Thread.currentThread().getName());
            Waiting.class.wait( );
          } catch (InterruptedException e) {
            e.printStackTrace( );
          }
        }
      }
    }
  }
  //该线程在Blocked.class实例上加锁后,不会释放该锁
  static class Blocked implements Runnable
  {
    public void run( )
    {
      synchronized (Blocked.class)
      {
        while (true)
        {
          try {
            System.out.println("I am Blocked Thread: "+ Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(5);
          } catch (InterruptedException e) {}
        }
      }
    }
  }
  //该线程用于同步锁
  static class Sync implements Runnable
  {
    public void run( ) {
      lock.lock( );
      try {
        System.out.println("I am Sync Thread: "+ Thread.currentThread().getName());
        TimeUnit.SECONDS.sleep(5);
      } catch (InterruptedException e) { }
      finally {
        lock.unlock();
      }
    }
  }
}

二 运行

I am Waiting Thread: WaitingThread
I am Blocked Thread: BThread-1
I am Sync Thread: SyncThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am Sync Thread: SyncThread-2
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1
I am TimeWaiting Thread: TimeWaitingThread
I am Blocked Thread: BThread-1

上述内容就是Java 线程的生命周期有哪些,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI