Thread 类是 JVM 用来管理线程的一个类,换句话说,每个线程都有一个唯一的 Thread 对象与之关联。用我们上面的例子来看,每个执行流,也需要有一个对象来描述,类似下图所示,而 Thread 类的对象就是用来描述一个线程执行流的,JVM 会将这些 Thread 对象组织起来,用于线程调度,线程管理.
1.Thread 的常见构造方法
Thread t1 = new Thread();
Thread t2 = new Thread(new MyRunnable());
Thread t3 = new Thread("这是我的名字");
Thread t4 = new Thread(new MyRunnable(), "这是我的名字");
package thread;
public class Test9 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}, "mythread");
thread.start();
while (true){
System.out.println("hello main");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
2.Thread 的几个常见属性
- ID 是线程的唯一标识,不同线程不会重复
- 名称是各种调试工具用到
- 状态表示线程当前所处的一个情况,下面我们会进一步说明
- 优先级高的线程理论上来说更容易被调度到
- 关于后台线程,需要记住一点:JVM会在一个进程的所有非后台线程结束后,才会结束运行。
- 是否存活,即简单的理解,为 run 方法是否运行结束了
- 线程的中断问题,下面我们进一步说明
3.控制线程的几个操作
1.创建线程
之前我们已经看到了如何通过覆写 run 方法创建一个线程对象,但线程对象被创建出来并不意味着线程就开始运行了。
- 覆写 run 方法是提供给线程要做的事情的指令清单
- 线程对象可以认为是把 李四、王五叫过来了
- 而调用 start() 方法,就是喊一声:”行动起来!“,线程才真正独立去执行了
调用 start 方法, 才真的在操作系统的底层创建出一个线程.
2.中断线程(让线程结束)
方法1: 使用自定义的变量来中断线程
package thread;
public class Test10 {
//通过变量控制线程结束
private static boolean isQuit = false;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (!isQuit) {
System.out.println("hello thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
Thread.sleep(5000);
isQuit = true;
}
}
这里的isQuit这个变量相当于在thread线程中读取,在main线程中修改
如果按照上述代码来写,其实存在一定的缺陷的。
方法2:使用 Thread.interrupted() 或者 Thread.currentThread().isInterrupted() 代替自定
义标志位.
Thread 内部包含了一个 boolean 类型的变量作为线程是否被中断的标记
package thread1;
public class Test1 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread() {
@Override
public void run() {
while (!this.isInterrupted()) {
System.out.println("hello thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
// break;
}
}
}
};
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
线程的代码处理interrupt方法也是,有三种不同的处理方式
给我买瓶酱油:
1.好的,立刻就去
2.好的,我打完这把就去
3.装作没听见,继续玩
3.等待一个线程-join()
有时,我们需要等待一个线程完成它的工作后,才能进行自己的下一步工作。
package thread1;
public class Test2 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
for (int i = 0; i < 5; i++) {
System.out.println("hello thread");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
System.out.println("thread 线程未结束!");
try {
thread.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("thread 线程结束!");
}
}
4.获取当前线程引用
package thread1;
public class Test3 {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}, "threadA");
thread.start();
Thread.sleep(5000);
thread.interrupt();
}
}
哪个线程调用了这个静态方法,就能够返回哪个线程的实例引用!这种获取方式,可以在任何代码中使用~
5.休眠当前线程
也是我们比较熟悉一组方法,有一点要记得,因为线程的调度是不可控的,所以,这个方法只能保证实际休眠时间是大于等于参数设置的休眠时间的