多线程基础知识第二篇:线程常用方法及各种状态

本篇主要介绍一下Thread类的方法以及探究一下线程的状态。

首先,Thread类常用的静态方法:

1.Thread.activeCount(),得到存活的线程数,返回值是int类型;

2.Thread.holdsLock(Object obj),当前线程是否获得了指定的对象同步锁,返回值是boolean类型;

3.Thread.currentThread(),得到当前线程,返回值是个Thread对象,一般后面再接着调用getName()方法,得到当前线程的名字;

4.Thread.sleep(long millis),让当前线程睡眠指定时间,以毫秒为单位

5.Thread.yield(),让当前释放掉调度器的调度,使得调度器重新调度,一般来说只有优先级比当前线程高或者至少跟当前线程优先级相同的线程才可能得到调度器的调用,否则还是该线程被调度器调用。

Thread类常用的非静态方法:

1.thread1.get/setName(),thread1.get/setPriority(),thread1.isAlive() 是否存活,thread1.isDaemon() 是否是后台线程,thread1.getState() 得到线程的状态,getTheadGroup() 得到线程组。

2.thread1.join():当前线程阻塞,等待thread1线程执行后再执行。

3.wait() 使当前线程等待,notify() 唤醒在此同步监视器上等待的单个线程,notifyAll() 唤醒在此同步监视器上等待的所有线程:这三个方法不是Thread类定义的,而是继承Object类得到的,这三个方法只能由同步监视器对象调用synchronized同步方法,this就是同步监视器对象。synchronized同步代码块,synchronized后面括号里的对象就是同步监视器对象

线程的状态:

在Thread类中有一个内部枚举State,里面有6个变量,用于描述线程的状态:

[java]  view plain  copy
  1. public enum State {  
  2.     /** 
  3.      * Thread state for a thread which has not yet started. 
  4.      */  
  5.     NEW,  
  6.   
  7.     /** 
  8.      * Thread state for a runnable thread. A thread in the runnable state is 
  9.      * executing in the Java virtual machine but it may be waiting for other 
  10.      * resources from the operating system such as processor. 
  11.      */  
  12.     RUNNABLE,  
  13.   
  14.     /** 
  15.      * Thread state for a thread blocked waiting for a monitor lock. A thread in 
  16.      * the blocked state is waiting for a monitor lock to enter a synchronized 
  17.      * block/method or reenter a synchronized block/method after calling 
  18.      * {@link Object#wait() Object.wait}. 
  19.      */  
  20.     BLOCKED,  
  21.   
  22.     /** 
  23.      * Thread state for a waiting thread. A thread is in the waiting state due 
  24.      * to calling one of the following methods: 
  25.      * <ul> 
  26.      * <li>{@link Object#wait() Object.wait} with no timeout</li> 
  27.      * <li>{@link #join() Thread.join} with no timeout</li> 
  28.      * <li>{@link LockSupport#park() LockSupport.park}</li> 
  29.      * </ul> 
  30.      * 
  31.      * <p> 
  32.      * A thread in the waiting state is waiting for another thread to perform a 
  33.      * particular action. 
  34.      * 
  35.      * For example, a thread that has called <tt>Object.wait()</tt> on an object 
  36.      * is waiting for another thread to call <tt>Object.notify()</tt> or 
  37.      * <tt>Object.notifyAll()</tt> on that object. A thread that has called 
  38.      * <tt>Thread.join()</tt> is waiting for a specified thread to terminate. 
  39.      */  
  40.     WAITING,  
  41.   
  42.     /** 
  43.      * Thread state for a waiting thread with a specified waiting time. A thread 
  44.      * is in the timed waiting state due to calling one of the following methods 
  45.      * with a specified positive waiting time: 
  46.      * <ul> 
  47.      * <li>{@link #sleep Thread.sleep}</li> 
  48.      * <li>{@link Object#wait(long) Object.wait} with timeout</li> 
  49.      * <li>{@link #join(long) Thread.join} with timeout</li> 
  50.      * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> 
  51.      * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> 
  52.      * </ul> 
  53.      */  
  54.     TIMED_WAITING,  
  55.   
  56.     /** 
  57.      * Thread state for a terminated thread. The thread has completed execution. 
  58.      */  
  59.     TERMINATED;  
  60. }  
由上面的源码,分析可知:

线程有6种状态,NEW、RUNNABLE 、BLOCKED、WAITING、TIMED_WAITING、TERMINATED。

猜你喜欢

转载自blog.csdn.net/qq_15037231/article/details/80678802