方法 | 功能 |
---|
isAlive() | 判断线程是否还“活”着,即线程是否还未终止 |
getPriority() | 获得线程的优先级数值 |
setPriority(int num) | 设置线程的优先级数值 |
setName(String name) | 给线程定义名称 |
getName() | 取得线程名称 |
currentThread() | 取得当前正在运行的线程对象 |
public class ThreadInfoTest {
public static void main(String[] args) {
testInfo test01 = new testInfo();
testInfo test02 = new testInfo();
Thread thread01 = new Thread(test01);
thread01.setName("thread01"); // 设置线程名称
Thread thread02 = new Thread(test02);
thread02.setName("thread02");
// Thread中的常量
// MIN_PRIORITY : 1 --> 最低优先级
// NORM_PRIORITY : 5 --> 默认优先级
// MAX_PRIORITY : 10 --> 最高优先级
thread01.setPriority(Thread.MIN_PRIORITY); // 设置线程优先级
thread02.setPriority(Thread.MAX_PRIORITY); // 设置优先级后不代表线程的执行顺序,只表示线程被执行的概率高低
thread01.start();
thread02.start();
try {
Thread.sleep(50);
test01.stop();
test02.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class testInfo implements Runnable {
private boolean flag = true;
@Override
public void run() {
int i = 0;
while (flag) {
System.out.println(Thread.currentThread().getName() + ":" + i); // 获取当前线程名称
i++;
}
}
public void stop() {
this.flag = false;
}
}