给线程取名字
判断线程是否存活线程不一定马上停止
setName()
getName()
isAlive()后台还有gc 异常线程
package study;
/**
* Thread.currentThread 当前线程
* steName()
* getName()
* isAlive()
* @author http://blog.csdn.net/thewaiting/
*
*/
public class InfoDome{
public static void main(String[] args) throws InterruptedException {
ThreadDome t1 = new ThreadDome();
Thread proxy = new Thread(t1,"线程1");
proxy.setName("xiancheng");
System.out.println(proxy.getName());
System.out.println(Thread.currentThread().getName());
proxy.start();
System.out.println("启动后的状态"+proxy.isAlive());
Thread.sleep(5);
t1.stop();
Thread.sleep(100);
System.out.println("停止后的状态"+proxy.isAlive());
}
}
线程体
package study;
/**
*
* @author http://blog.csdn.net/thewaiting/
*
*/
public class ThreadDome implements Runnable {
private boolean flag = true;
private int num = 0;
@Override
public void run() {
while (flag) {
System.out.println(Thread.currentThread().getName()+num++);
}
}
public void stop() {
this.flag = false;
}
}
运行结果截取
xiancheng146
xiancheng147
停止后的状态false
优先级问题
优先级 :概率 不是绝对的优先级
setPriority()设置优先级
getPriority()获得优先级
MAX_PRIORITY 10
NORM_PRIORITY 5
MIN_PRIORITY 1
package study;
/**
* 优先级 :概率 不是绝对的优先级
* MAX_PRIORITY 10
* NORM_PRIORITY 5
* MIN_PRIORITY 1
* @author http://blog.csdn.net/thewaiting/
*
*/
public class InfoDome{
public static void main(String[] args) throws InterruptedException {
ThreadDome t1 = new ThreadDome();
Thread proxy = new Thread(t1,"线程1 ");
ThreadDome t2 = new ThreadDome();
Thread proxy2 = new Thread(t2,"线程2 ");
proxy.setPriority(Thread.MIN_PRIORITY);
proxy2.setPriority(Thread.MAX_PRIORITY);
proxy.start();
proxy2.start();
Thread.sleep(3);
t1.stop();
t2.stop();
}
}