線程中包含一些屬性java
ID:線程IDide
Name: this
Prioity: 優先級,可以設置1到10 ,優先級由低到高;spa
status:線程狀態,共有六個:new, runnable,blocked, waiting, time waiting, terminated線程
public class Calculator implements Runnable { private int number; public Calculator(int number) { this.number = number; } @Override public void run() { for (int i = 0; i < number; i++) { System.out.printf("%s: %d\n", Thread.currentThread().getName(), i * number); } } public static void main(String[] args) { Thread threads[] = new Thread[10]; Thread.State status[] = new Thread.State[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); } //將線程狀態變化寫入到文件 try (FileWriter fileWriter = new FileWriter("thread.log"); PrintWriter writer = new PrintWriter(fileWriter)) { for (int i = 0; i < 10; i++) { writer.println("Main : Status of Thread " + i + " : " + threads[i].getState()); status[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() != status[i]) { writeThreadInfo(writer, threads[i], status[i]); status[i] = threads[i].getState(); } } finish = true; for (int i = 0; i < 10; i++) { finish = finish && (threads[i].getState() == Thread.State.TERMINATED); } } } catch (Exception e) { e.printStackTrace(); } } private static void writeThreadInfo(PrintWriter pw, Thread thread, Thread.State state) { pw.printf("Main : Id %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()); pw.printf("Main : ************************************\n"); } }
當沒有指定線程名稱的時候,JVM自動爲線程命名Thread-N (N爲數字);code
若是使用setPriority()設置的優先級參數不合法,將會拋出IllegalArgumentException異常,正常值在1-10之間
get