(1)线程的开始与停止
一、线程的启动
1、深入理解 run() 和 start()
Thread 类是 Java 里对线程概念的抽象,可以这样理解:我们通过 new Thread() 其实只是new出一个Thread 的实例,还没有操作系统中真正的线程挂起钩来。
只有执行了 start() 方法后,才实现了真正意义上的启动线程。
start() 方法让一个线程进入就绪队列等待分配 cpu,分到cpu后才调用实现的run() 方法, start() 方法不能重复调用,如果重复调用会抛出异常。
而 run 方法是业务逻辑实现的地方,本质上和任意一个类的任意一个成员方法并没有任何区别,可以重复执行,也可以被单独调用。
2、启动线程的方式
① XXX extends Thread,然后 XXX.start
② XXX implements Runnable,然后交给 Thread 运行
下面来演示一下:
/**
* ① XXX extends Thread,然后 XXX.start
*/
public class Test {
private static class TestThread extends Thread{
public TestThread(String name) {
super(name);
}
@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println("线程名名称:"+name);
}
}
public static void main(String[] args) {
TestThread testThread = new TestThread("testThread");
testThread.start();
}
}
/**
* ② XXX implements Runnable,然后交给 Thread 运行
*/
public class TestRunnable implements Runnable {
@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println("线程名名称:"+name);
}
public static void main(String[] args) {
Thread thread = new Thread(new TestRunnable());
thread.start();
}
}
3、Thread和Runnable的区别
Thread才是Java里对线程的唯一抽象,Runnable只是对任务(业务逻辑)的抽象。
Thread可以接受任意一个Runnable的实例并执行。
二、线程的中止
1、interrupt()、isInterrupted()
关于线程中止,我们这里介绍的是**interrupt()、isInterrupted()、Thread.interrupt()**三个方法。
interrupt()方法主要是中断当前线程并修改线程的中断标识位改为true,默认是false,但并不是马上执行中断线程。
isInterrupted()方法可以查看当前线程是否中断。
Thread.interrupt()是Thread类的静态方法,它会将线程的中断标识位改为true之后再改回false。
下面来使用一下interrupt()和isInterrupted()方法:
public class InterruptTest {
private static class TestThread extends Thread{
public TestThread(String name) {
super(name);
}
@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+" interrupt flag :"+ isInterrupted());
while (true){
System.out.println(name+" in while ");
System.out.println(name+" in while flag: "+isInterrupted());
}
}
}
public static void main(String[] args) throws InterruptedException {
TestThread interruptTestThread = new TestThread("interruptTestThread");
interruptTestThread.start();
Thread.sleep(20);
interruptTestThread.interrupt();//中断线程,设置线程的标识位
}
}
这里可以看出线程的中断标识位由false改为true,但是线程并没有中断,还在继续的循环打印。
因为在jdk中线程是协作式的,而不是抢占式的,如果需要强制停止线程我们可以使用stop()方法。
下面我们来配合isInterrupted()来使用:
public class InterruptTest {
private static class TestThread extends Thread{
public TestThread(String name) {
super(name);
}
@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name+" interrupt flag :"+ isInterrupted());
while (!isInterrupted()){
System.out.println(name+" in while ");
System.out.println(name+" in while flag: "+isInterrupted());
}
System.out.println(name+" interrupt endFlag :"+ isInterrupted());
}
}
public static void main(String[] args) throws InterruptedException {
TestThread interruptTestThread = new TestThread("interruptTestThread");
interruptTestThread.start();
Thread.sleep(20);
interruptTestThread.interrupt();//中断线程,设置线程的标识位
}
}
在我们加入了 while (!isInterrupted()) 判断之后线程才停止了循环执行后面的代码。
0条评论