线程控制基本方法
方 法 功 能
isAlive() 判断线程是否还“活”着,即当前 run 线程是否还未终止。
getPriority() 获得线程的优先级数值
setPriority() 设置线程的优先级数值
Thread.sleep() 将当前线程睡眠指定毫秒数
join() 调用某线程的该方法,将当前线程与该线程“合并”,即等待该线程结束,
再恢复当前线程的运行。
yield() 让出 CPU,当前线程进入就绪队列等待调度。
wait() 当前线程进入对象的 wait pool。
notify()/notifyAll() 唤醒对象的 wait pool 中的一个/所有等待线程。
run()和 start()
这两个方法应该都比较熟悉,把需要并行处理的代码放在
run()方法中,start()方法启动线
程将自动调用
run()方法,这是由 Java 的内存机制规定的。并且 run()方法必须是 public 访问
权限,返回值类型为
void。
isAlive 方法实例:
package com.Gavin.createthread;
public class TestIsAlive {
public static void main(String[] args) {
Thread6 t6 = new Thread6("t6");
t6.start();
for(int i = 0; i < 50; i++) {
// System.out.println(Thread.currentThread().getName()); //主线程
System.out.println("t6's name:" + t6.getName());
}
}
}
class Thread6 extends Thread {
public Thread6(String string) {
super(string);
}
public void run() {
System.out.println("thread is alive:" + Thread.currentThread().isAlive());