获取和设置线程信息
线程中包含一些属性
ID:线程ID
Name:
Prioity: 优先级,能够设置1到10 ,优先级由低到高;
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为数字);
如果使用setPriority()设置的优先级参数不合法,将会抛出IllegalArgumentException异常,正常值在1-10之间