Thread类和Runnable 接口
- 获取和设置Thread对象信息的方法。
- getId():该方法返回Thread对象的标识符。该标识符是在钱程创建时分配的一个正 整数。在线程的整个生命周期中是唯一且无法改变的。
- getName()/setName():这两种方法允许你获取或设置Thread对象的名称。这个名 称是一个String对象,也可以在Thread类的构造函数中建立。
- getPriority()/setPriority():你可以使用这两种方法来获取或设置Thread对象的优先 级。
- isDaemon()/setDaemon():这两种方法允许你获取或建立Thread对象的守护条件。
- getState():该方法返回Thread对象的状态。
- sleep(long ms):该方法将线程的执行暂停ms时间。
- join():暂停线程的执行,直到调用该方法的线程执行结束为止。可以使用该方法等待另一个 Thread对象结束。
- setUncaughtExceptionHandler():当线程执行出现未校验异常时,该方法用于建立未校验异 常的控制器。
- currentThread():Thread类的静态方法,返回实际执行该代码的Thread对象。
中断
- interrupt():中断目标线程,给目标线程发送一个中断信号,线程被打上中断标记。
- interrupted():判断目标线程是否被中断,但是将清除线程的中断标记。(静态方法)
- isinterrupted():判断目标线程是否被中断,不会清除中断标记
thread.isInterrupted()与Thread.interrupted()的区别
因为 thread.interrupted()相当于给线程发送了一个唤醒的信号,所以如果线程此时恰好处于 WAITING或者TIMEDWAITING状态,就会抛出一个InterruptedException,并且线程被唤醒。而如果 线程此时并没有被阻塞,则线程什么都不会做。**但在后续,线程可以判断自己是否收到过其他线程发来 的中断信号,然后做一些对应的处理(aqs 和 condition中用到这个特性了)_**
这两个方法都是线程用来判断自己是否收到过中断信号的,前者是实例方法,后者是静态方法。二 者的区别在于,前者只是读取中断状态,不修改状态;后者不仅读取中断状态,还会重置中断标志位。
public static void main(String[] args) throws InterruptedException{Thread thread = new Thread(new Runnable() {@Overridepublic void run() {while (true) {}}});thread.start();// 设置中断位thread.interrupt();System.out.println("isInterrupted: " + thread.isInterrupted());// 虽然调用者是thread 但实际上 static修饰的 获取的是主线程的中断状态System.out.println("isInterrupted: " + thread.interrupted());System.out.println("isInterrupted: " + Thread.interrupted());System.out.println("isInterrupted: " + thread.isInterrupted());thread.join();System.out.println("main is over");}

public static void main(String[] args) throws InterruptedException{Thread thread = new Thread(new Runnable() {@Overridepublic void run() {while (!Thread.currentThread().interrupted()) {}System.out.println("thread isInterrupted:" + Thread.currentThread().isInterrupted());}});thread.start();// 设置中断位thread.interrupt();thread.join();System.out.println("main is over");}
优雅的关闭线程
设置标志位
public class CloseThreadElegant {public static void main(String[] args) throws InterruptedException {MyThread myThread = new MyThread();myThread.start();Thread.sleep(5000);myThread.stopRunning();myThread.join();}}class MyThread extends Thread {private boolean running = true;@Overridepublic void run() {while (running) {System.out.println("线程正在运行。。。");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}public void stopRunning() {this.running = false;}}
但上面的代码有一个问题:如果MyThread t在while循环中阻塞在某个地方,例如里面调用了
object.wait()函数,那它可能永远没有机会再执行 while( ! stopped)代码,也就一直无法退出循环。
此时,就要用到InterruptedException()与interrupt()函数。
