package com.jackson.deng.concurrent.two;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.lang.Thread.State;
/**
* 线程管理(二)获取和设置线程信息
*
* ID: 每个线程的独特标识。
* Name: 线程的名称。
* Priority: 线程对象的优先级。优先级别在1-10之间,1是最低级,10是最高级。不建议改变它们的优先级,但是你想的话也是可以的。
* Status: 线程的状态。在Java中,线程只能有这6种中的一种状态: new, runnable, blocked, waiting, time waiting, 或 terminated.
*
* @author jackson
*
*/
public class SetOrGetThreadInfo {
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);
}
}
}
public static void main(String[] args) throws Exception {
//记录状态文件的位置
FileWriter file = new FileWriter("D:/log.txt");
PrintWriter pw = new PrintWriter(file);
SetOrGetThreadInfo thisClass = new SetOrGetThreadInfo();
Thread[] threads = new Thread[10];
// 创建一个大小为10的Thread类的数组,一个大小为10的Thread.State数组来保存将要执行的线程和它们的状态。
State[] status = new State[10];
for (int i = 0; i < 10; i++) {
threads[i] = new Thread(thisClass.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);
pw.println("Main : Status of Thread " + i + " : " + threads[i].getState());
status[i] = threads[i].getState();
}
//开启10个线程
for (int i = 0; i < 10; i++) {
threads[i].start();
}
boolean finish = false;
while (!finish) {
for (int i = 0; i < 10; i++) {
/**
* 1.如果线程状态发生改变,记录信息<br>
* 2.把改变后的线程状态赋给status[i]变量,以便下次发生改变进行对比
*/
if (threads[i].getState() != status[i]) {
//1.
writeThreadInfo(pw, threads[i], status[i]);
//2.
status[i] = threads[i].getState();
}
}
finish = true;
for (int i = 0; i < 10; i++) {
finish = finish && (threads[i].getState() == State.TERMINATED);
}
}
pw.flush();
pw.close();
}
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");
}
}
程序的控制台显示的是线程计算的乘法表,而log.txt文本记录的是不同线程的状态演变。这样子,可以更好的观察线程的演变过程。
Thread 类有能保存使用线程信息的属性。JVM根据线程的优先级来选择将使用CPU的线程,然后再根据每个线程的情况来实现它们的状态。
如果你没有声明一个线程的名字,那么JVM会自动命名它为:Thread-XX,XX是一个数字。线程的ID或者状态是不可修改的。Thread类没有实现setId()和setStatus()方法来允许修改它们。
补充:线程有6中状态:分别是NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING,TERMINATED;
网上说法有很多,3,4,5的都有,但是我查找源代码,发现确实有上面说的6中状态
线程还没有start()之前,处于NEW状态(网上的说法是创建状态).
算了,我就不翻译了,我把源代码贴出来,看的真切点.
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;
}