下面是Thread.java中相關屬性的源代碼:java
private char name[]; /* * Thread ID */ private long tid; /* * Native method */ private native void setPriority0(int newPriority); /** * The minimum priority that a thread can have. */ public final static int MIN_PRIORITY = 1; /** * The default priority that is assigned to a thread. */ public final static int NORM_PRIORITY = 5; /** * The maximum priority that a thread can have. */ public final static int MAX_PRIORITY = 10; /** * A thread state. A thread can be in one of the following states: * <ul> * <li>{@link #NEW}<br> * A thread that has not yet started is in this state. * </li> * <li>{@link #RUNNABLE}<br> * A thread executing in the Java virtual machine is in this state. * </li> * <li>{@link #BLOCKED}<br> * A thread that is blocked waiting for a monitor lock * is in this state. * </li> * <li>{@link #WAITING}<br> * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. * </li> * <li>{@link #TIMED_WAITING}<br> * A thread that is waiting for another thread to perform an action * for up to a specified waiting time is in this state. * </li> * <li>{@link #TERMINATED}<br> * A thread that has exited is in this state. * </li> * </ul> * * <p> * A thread can be in only one state at a given point in time. * These states are virtual machine states which do not reflect * any operating system thread states. * * @since 1.5 * @see #getState */ public enum State { /** * Thread state for a thread which has not yet started. */ NEW, /** * Thread state for a runnable thread. A thread in the runnable * state is executing in the Java virtual machine but it may * be waiting for other resources from the operating system * such as processor. */ RUNNABLE, /** * Thread state for a thread blocked waiting for a monitor lock. * A thread in the blocked state is waiting for a monitor lock * to enter a synchronized block/method or * reenter a synchronized block/method after calling * {@link Object#wait() Object.wait}. */ BLOCKED, /** * Thread state for a waiting thread. * A thread is in the waiting state due to calling one of the * following methods: * <ul> * <li>{@link Object#wait() Object.wait} with no timeout</li> * <li>{@link #join() Thread.join} with no timeout</li> * <li>{@link LockSupport#park() LockSupport.park}</li> * </ul> * * <p>A thread in the waiting state is waiting for another thread to * perform a particular action. * * For example, a thread that has called <tt>Object.wait()</tt> * on an object is waiting for another thread to call * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on * that object. A thread that has called <tt>Thread.join()</tt> * is waiting for a specified thread to terminate. */ WAITING, /** * Thread state for a waiting thread with a specified waiting time. * A thread is in the timed waiting state due to calling one of * the following methods with a specified positive waiting time: * <ul> * <li>{@link #sleep Thread.sleep}</li> * <li>{@link Object#wait(long) Object.wait} with timeout</li> * <li>{@link #join(long) Thread.join} with timeout</li> * <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li> * <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li> * </ul> */ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }
在上面截取的Thread類源代碼中咱們能夠看到,線程的優先級是經過一個native方法來實現的。而線程的狀態是經過一個枚舉類來定義。數組
經過實現Runnable接口來建立一個Calculator線程類。定義它的run()方法,在這個方法中咱們經過Thread.currentThread().getName()方法來獲取線程名稱並打印線程名和乘法運算表。this
public class Calculator implements Runnable { private int number; public Calculator(int number) { this.number = number; } public void run() { for (int i = 1; i <= 10; i++) { System.out.printf("%s: %d * %d = %d\n", Thread.currentThread().getName(), i, number, i * number); } } }
接下來咱們在main方法中建立10個大小的線程數組,並新建10個線程,爲雙數線程指定最高優先級,爲單數線程執行最低優先級。並執定線程名:「Thread i」。spa
Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(new Calculator(i)); if (i % 2 == 0) { threads[i].setPriority(Thread.MAX_PRIORITY); } else { threads[i].setPriority(Thread.MIN_PRIORITY); } threads[i].setName("Thread " + i); }
接下來,咱們建立一個日誌文件「./data/log.txt」。首先把10個線程的初始狀態打印到日誌文件中,而後啓動10個線程。循環等待全部線程進入結束狀態,線程狀態一旦發生變化會打印線程信息到日誌文件中。線程
FileWriter fw = null; PrintWriter pw = null; try { fw = new FileWriter("./data/log.txt"); pw = new PrintWriter(fw); for (int i = 0; i < 10; i++) { pw.println("Main: Status of the Thread " + i + " is " + threads[i].getState()); states[i] = threads[i].getState(); } for (int i = 0; i < 10; i++) { threads[i].start(); } boolean finish = false; while (!finish) { for (int i = 0; i < 10; i++) { if (threads[i].getState() != states[i]) { writeThreadInfo(pw, threads[i], states[i]); states[i] = threads[i].getState(); } } finish = true; for (int i = 0; i < 10; i++) { finish = finish && threads[i].getState() == Thread.State.TERMINATED; } } } catch (IOException e) { e.printStackTrace(); } finally { if (pw != null) { pw.close(); } if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
打印線程信息,狀態,優先級的方法:日誌
public static void writeThreadInfo(PrintWriter pw, Thread thread, Thread.State state) { pw.printf("Main: %d : %s\n", thread.getId(), thread.getName()); pw.printf("Main: Priority %d\n", thread.getPriority()); pw.printf("Main: old state: %s\n", state); pw.printf("Main: new state: %s\n", thread.getState()); }
全部線程初始狀態:NEWcode
Main: Status of the Thread 0 is NEW Main: Status of the Thread 1 is NEW Main: Status of the Thread 2 is NEW Main: Status of the Thread 3 is NEW Main: Status of the Thread 4 is NEW Main: Status of the Thread 5 is NEW Main: Status of the Thread 6 is NEW Main: Status of the Thread 7 is NEW Main: Status of the Thread 8 is NEW Main: Status of the Thread 9 is NEW
選取一個線程看一下狀態信息:orm
Main: 9 : Thread 0 Main: Priority 10 Main: old state: NEW Main: new state: RUNNABLE Main: 9 : Thread 0 Main: Priority 10 Main: old state: RUNNABLE Main: new state: BLOCKED Main: 9 : Thread 0 Main: Priority 10 Main: old state: BLOCKED Main: new state: RUNNABLE Main: 9 : Thread 0 Main: Priority 10 Main: old state: RUNNABLE Main: new state: TERMINATED