Java并发编程实例--2.获取和设置线程信息
常用线程属性
ID: 每个线程的唯一标识;
Name: 线程名称;
Priority: 线程优先级,从1-10,数字越大优先级越高;不推荐改变线程优先级;
Status: 线程状态,包含6种状态:new, runnable, blocked, waiting, time waiting, terminated
本例中,我们会开发一个程序以设置线程名称和优先级并展示一些线程信息。
Calculator.java
package com.dylan.thread.ch1.c02;
/**
* This class prints the multiplication table of a number
* @author xusucheng
* @create 2018-04-21
**/
public class Calculator implements Runnable{
private int number;
public Calculator(int number) {
this.number=number;
}
@Override
public void run() {
for (int i=1; i<=10; i++){
System.out.printf("%s: %d * %d = %d\n",Thread.
currentThread().getName(),number,i,i*number);
}
}
}
Main.java
package com.dylan.thread.ch1.c02;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.Thread.State;
/**
* @author xusucheng
* @create 2018-04-21
**/
public class Main {
/**
* Main method of the example
* @param args
*/
public static void main(String[] args) {
// Thread priority infomation
System.out.printf("Minimum Priority: %s\n",Thread.MIN_PRIORITY);
System.out.printf("Normal Priority: %s\n",Thread.NORM_PRIORITY);
System.out.printf("Maximun Priority: %s\n",Thread.MAX_PRIORITY);
Thread threads[];
Thread.State status[];
// Launch 10 threads to do the operation, 5 with the max
// priority, 5 with the min
threads=new Thread[10];
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);
}
// Wait for the finalization of the threads. Meanwhile,
// write the status of those threads in a file
try (FileWriter file = new FileWriter("D:\\output\\threadLog.txt");PrintWriter pw = new PrintWriter(file);){
for (int i=0; i<10; i++){
pw.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(pw, threads[i],status[i]);
status[i]=threads[i].getState();
}
}
finish=true;
for (int i=0; i<10; i++){
finish=finish &&(threads[i].getState()== State.TERMINATED);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* This method writes the state of a thread in a file
* @param pw : PrintWriter to write the data
* @param thread : Thread whose information will be written
* @param state : Old state of the thread
*/
private static void writeThreadInfo(PrintWriter pw, 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");
}
}
日志文件内容:
Main : Status of Thread 0 : NEW
Main : Status of Thread 1 : NEW
Main : Status of Thread 2 : NEW
Main : Status of Thread 3 : NEW
Main : Status of Thread 4 : NEW
Main : Status of Thread 5 : NEW
Main : Status of Thread 6 : NEW
Main : Status of Thread 7 : NEW
Main : Status of Thread 8 : NEW
Main : Status of Thread 9 : NEW
Main : Id 11 - Thread 0
Main : Priority: 10
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 12 - Thread 1
Main : Priority: 1
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 13 - Thread 2
Main : Priority: 10
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 14 - Thread 3
Main : Priority: 1
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 15 - Thread 4
Main : Priority: 10
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 16 - Thread 5
Main : Priority: 1
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 17 - Thread 6
Main : Priority: 10
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 18 - Thread 7
Main : Priority: 1
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 19 - Thread 8
Main : Priority: 10
Main : Old State: NEW
Main : New State: BLOCKED
Main : ************************************
Main : Id 20 - Thread 9
Main : Priority: 1
Main : Old State: NEW
Main : New State: RUNNABLE
Main : ************************************
Main : Id 12 - Thread 1
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 14 - Thread 3
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 15 - Thread 4
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 16 - Thread 5
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 17 - Thread 6
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 18 - Thread 7
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 19 - Thread 8
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 20 - Thread 9
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 12 - Thread 1
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: BLOCKED
Main : ************************************
Main : Id 13 - Thread 2
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 15 - Thread 4
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 13 - Thread 2
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 12 - Thread 1
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 14 - Thread 3
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 12 - Thread 1
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: BLOCKED
Main : ************************************
Main : Id 14 - Thread 3
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 20 - Thread 9
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 20 - Thread 9
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 18 - Thread 7
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 17 - Thread 6
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: TERMINATED
Main : ************************************
Main : Id 18 - Thread 7
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 16 - Thread 5
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 11 - Thread 0
Main : Priority: 10
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 11 - Thread 0
Main : Priority: 10
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 16 - Thread 5
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************
Main : Id 12 - Thread 1
Main : Priority: 1
Main : Old State: BLOCKED
Main : New State: RUNNABLE
Main : ************************************
Main : Id 12 - Thread 1
Main : Priority: 1
Main : Old State: RUNNABLE
Main : New State: TERMINATED
Main : ************************************