文章目录
Thread常用方法
简单介绍几个常用的方法:
currentThread()
currentThread() 方法可返回代码段正在被哪个线程调用的信息。
System.out.println("Thread name: " + Thread.currentThread().getName());
借助下面的例子加深理解:
public class CountOperate extends Thread {
public CountOperate() {
System.out.println("CountOperate -- begin");
System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
System.out.println("this.getName() = " + this.getName());
System.out.println("CountOperate -- end");
}
@Override
public void run() {
System.out.println("run -- begin");
System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
System.out.println("this.getName() = " + this.getName());
System.out.println("run -- end");
}
}
public class Run {
public static void main(String[] args) {
CountOperate countOperate = new CountOperate();
Thread thread = new Thread(countOperate);
thread.setName("A");
thread.start();
}
输出:
CountOperate -- begin
Thread.currentThread().getName() = main
this.getName() = Thread-0
CountOperate -- end
run -- begin
Thread.currentThread().getName() = A
this.getName() = Thread-0
run -- end
isAlive()
方法isAlive() 的功能是判断当前的线程是否处于活动状态。
活动状态就是线程已经启动且尚未终止。线程处于正在运行或准备开始运行的状态,就认为线程是“存活”的。
class MyThread2 extends Thread {
@Override
public void run() {
super.run();
System.out.println("MyThread2.run");
}
}
public static void main(String[] args) throws InterruptedException {
MyThread2 thread2 = new MyThread2();
System.out.println("begin == " + thread2.isAlive());
thread2.start();
Thread.sleep(200);
System.out.println("end == " + thread2.isAlive());
}
输出:
begin == false
MyThread2.run
end == false
如果将线程对象以构造参数的方式传递给Thread对象进行start() 启动时,运行的结果和前面示例是有差异的。造成这样的差异的原因还是来自于Thread.currentThread() 和this的差异。
public class CountOperate extends Thread {
public CountOperate() {
System.out.println("CountOperate -- begin");
System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive());
System.out.println("this.getName() = " + this.getName());
System.out.println("this.isAlive() = " + this.isAlive());
System.out.println("CountOperate -- end");
}
@Override
public void run() {
System.out.println("run -- begin");
System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
System.out.println("Thread.currentThread().isAlive() = " + Thread.currentThread().isAlive());
System.out.println("this.getName() = " + this.getName());
System.out.println("this.isAlive() = " + this.isAlive());
System.out.println("run -- end");
}
}
public static void main(String[] args) {
CountOperate countOperate = new CountOperate();
Thread thread = new Thread(countOperate);
System.out.println("test begin = " + Thread.currentThread().isAlive());
thread.setName("A");
thread.start();
System.out.println("test end = " + Thread.currentThread().isAlive());
}
输出:
CountOperate -- begin
Thread.currentThread().getName() = main
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
CountOperate -- end
test begin = true
test end = true
run -- begin
Thread.currentThread().getName() = A
Thread.currentThread().isAlive() = true
this.getName() = Thread-0
this.isAlive() = false
run -- end
注意,这里的this,实际指的是countOperate 对象,虽然它是一个线程类,但是并没有被启动,只是它的run() 方法被调用了而已。
sleep()
方法sleep() 的作用是在指定的毫秒数内让当前“正在执行的线程”休眠(暂停执行)。这个“正在执行的线程”是指this.currentThread() 返回的线程。
public class MyThread3 extends Thread {
@Override
public void run() {
try {
System.out.println("run threadName=" + currentThread().getName() + " begin");
Thread.sleep(2000);
System.out.println("run threadName=" + currentThread().getName() + " end");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThread3 thread3 = new MyThread3();
System.out.println("begin = " + System.currentTimeMillis());
thread3.run();
System.out.println("end = " + System.currentTimeMillis());
}
输出:
begin = 1606311845332
run threadName=main begin
run threadName=main end
end = 1606311847339
从时间上,就可看出有2000毫秒的时间差。
如果将上面的thread3.run(); 换成 thread3.start(); 就会有异步的效果,如下:
public class MyThread3 extends Thread {
@Override
public void run() {
try {
System.out.println("run threadName=" + currentThread().getName() + " begin " + System.currentTimeMillis());
Thread.sleep(2000);
System.out.println("run threadName=" + currentThread().getName() + " end " + System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
MyThread3 thread3 = new MyThread3();
System.out.println("begin = " + System.currentTimeMillis());
thread3.start();
System.out.println("end = " + System.currentTimeMillis());
}
输出:
begin = 1606312266324
end = 1606312266328
run threadName=Thread-0 begin 1606312266329
run threadName=Thread-0 end 1606312268338
由于main线程与MyThread3线程是异步执行的,所以首先打印的信息为begin和end。而MyThread3线程是随后运行的,在最后两行打印run begin和run end相关的信息。
getId()
getId() 方法的作用是取得线程的唯一标识。
Thread 源码中是通过下面的代码得到的:
/* For autonumbering anonymous threads. */
private static int threadInitNumber;
private static synchronized int nextThreadNum() {
return threadInitNumber++;
}
/* For generating thread ID */
private static long threadSeqNumber;
private static synchronized long nextThreadID() {
return ++threadSeqNumber;
}
暂停线程
暂停线程意味着此线程还可以恢复运行。
在Java多线程中,可以使用suspend() 方法暂停线程,使用resume() 方法恢复线程的执行。目前,它们都是过期作废的方法,不推荐使用。
-
suspend与resume方法的缺点——独占
在使用suspend与resume方法时,如果使用不当,极易造成公共的同步对象的独占,使得其他线程无法访问公共同步对象。 -
suspend与resume方法的缺点——不同步
在使用suspend与resume方法时也容易出现因为线程的暂停而导致数据不同步的情况。
yield()
yield() 方法的作用是放弃当前的CPU资源,将它让给其他的任务去占用CPU执行时间。但放弃的时间不确定,有可能刚刚放弃,马上又获得CPU时间片。
setPriority()
在操作系统中,线程可以划分优先级,优先级较高的线程得到的CPU资源较多,也就是CPU优先执行优先级较高的线程对象中的任务。设置线程优先级有助于帮“线程规划器”确定在下一次选择哪一个线程来优先执行。
设置线程的优先级使用setPriority() 方法。
如下所示为JDK源码:
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
setPriority0(priority = newPriority);
}
}
在Java中,线程的优先级分为1~10这10个等级,如果小于1或大于10,则JDK抛出异常throw new IllegalArgumentException()。
JDK中使用3个常量来预置定义优先级的值,代码如下:
优先级的默认值为5。
/**
* The minimum priority that a thread can have.
*/
public final static int MIN_PRIORITY = 1;
/**
* The default priority that is assigned to a thread.
*/
public final static int NORM_PRIORITY = 5;
/**
* The maximum priority that a thread can have.
*/
public final static int MAX_PRIORITY = 10;
线程优先级的继承特性
在Java中,线程的优先级具有继承性,比如A线程启动B线程,则B线程的优先级与A是一样的。
在Thread的JDK源码中,有相应的代码:
private void init(ThreadGroup g, Runnable target, String name,
long stackSize, AccessControlContext acc,
boolean inheritThreadLocals) {
...
Thread parent = currentThread();
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();
...
}
优先级具有规则性
当线程优先级的等级差距很大时,谁先执行完和代码的调用顺序无关。
CPU尽量将执行资源让给优先级比较高的线程。
优先级具有随机性
线程的优先级还具有“随机性”,也就是优先级较高的线程不一定每一次都先执行完。