Thread源码知识:
3、currentThread()方法
返回当前正在被那个线程调用
static class MyThread extends Thread {
public MyThread() {
System.out.println("MyThread constructor begin");
System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());
System.out.println("this.getName()=" + this.getName());
System.out.println("MyThread constructor end");
System.out.println();
System.out.println();
}
@Override
public void run() {
super.run();
System.out.println("MyThread run begin");
System.out.println("Thread.currentThread().getName()=" + Thread.currentThread().getName());
System.out.println("this.getName()=" + this.getName());
System.out.println("MyThread run end");
}
}
测试方法1
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.setName("A");
myThread.start();
}
结果:
MyThread constructor begin
Thread.currentThread().getName()=main //R1
this.getName()=Thread-0 //R2
MyThread constructor end
MyThread run begin
Thread.currentThread().getName()=A //R3
this.getName()=A //R4
MyThread run end
结果分析
R1:此线程由主线程new创建,构造函数也就由主线程来执行
R2:每个线程创建的时候,都会默认的初始化一个线程名字,这部分在setName之前执行
R3:A线程正在执行
R4:setName覆盖了myThread的默认线程名字(Thread-0)
测试方法2:
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.setName("B");
Thread proxyThread = new Thread(myThread);
proxyThread.start();
}
结果:
MyThread constructor begin
Thread.currentThread().getName()=main //R1
this.getName()=Thread-0 //R2
MyThread constructor end
MyThread run begin
Thread.currentThread().getName()=Thread-1 //R3
this.getName()=B //R4
MyThread run end
R1:此线程由主线程new创建,构造函数也就由主线程来执行
R2:每个线程创建的时候,都会默认的初始化一个线程名字,这部分在setName之前执行
R3:proxyThread 默认的线程名称,执行了myThread 的run方法
R4:
4、isAlive()方法
判断当前线程是否处于活动状态,活动状态就是线程启动尚未终止
9、yield()方法
放弃当前CPU资源,让给其他任务去占用CPU时间,但放弃的时间不确定,有可能刚刚放弃就马上获得CPU的时间片
public class Test {
static class MyThread extends Thread {
@Override
public void run() {
long beginTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
yield();
new String();
new String();
new String();
new String();
}
long endTime = System.currentTimeMillis();
System.out.println("use time:" + (endTime - beginTime));
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
没注释掉yield方法结果为:use time:48
注释掉yield方法结果为:use time:15
10、线程的优先级
A、线程的优先级范围为1~10,最大的优先级为10,最小的优先级为1,线程默认的优先是5,线程的三个常量
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;
B、优先级的继承性,A线程创建了B线程,则B线程的优先级与A相同
C、线程的规则性:高有优先级的线程总是大部分先执行完,并不代表高优先级的线程先全部执行完,因为CPU尽量会将执行资源让给优先级高的线程
D、线程的随机性:线程抢占机制,导致即使高优先级的线程也不一定先执行完成
11、守护线程
当进程中不存在非守护线程,则守护线程将自动销毁。典型的垃圾回收线程就是守护线程。
public class Test {
static class MyThread extends Thread {
private long counter = 1;
@Override
public void run() {
try {
while (true) {
if (isInterrupted()) {
throw new InterruptedException("被中断了!!!");
}
Thread.sleep(1000);
System.out.println("run counter=" + (counter++));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
myThread.setDaemon(true);
myThread.start();
Thread.sleep(5000);
System.out.println("main end");
}
}
运行结果