线程创建
创建线程主要有5种方法:
- 继承Thread类,重写run()方法
- 实现Runnable接口,重写run()方法
- 使用匿名内部类,继承Thread类
- 使用匿名内部类,实现Runnable接口
- Lambda表达式
方法1——继承Thread类,重写run()方法
class MyThread1 extends Thread{
@Override
public void run() {
System.out.println("继承Thread类,重写run()方法");
}
}
public class Test {
public static void main(String[] args) {
Thread t1 = new MyThread1();
t1.start();
}
}
方法2——实现Runnable接口,重写run()方法
class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("实现Runnable接口,重写run()方法");
}
}
public class Test {
public static void main(String[] args) {
Runnable runnable = new MyThread2();
Thread t2 = new Thread(runnable);
t2.start();
}
}
方法3——使用匿名内部类,继承Thread类
public class Test {
public static void main(String[] args) {
Thread t3 = new Thread(){
@Override
public void run() {
System.out.println("使用匿名内部类,继承Thread类");
}
};
t3.start();
}
}
方法4——使用匿名内部类,实现Runnable接口
public class Test {
public static void main(String[] args) {
Thread t4 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("使用匿名内部类,实现Runnable接口");
}
});
t4.start();
}
}
方法5——Lambda表达式
public class Test {
public static void main(String[] args) {
Thread t5 = new Thread(()->{
System.out.println("Lambda表达式");
});
t5.start();
}
}
线程中断
线程中断常见的有以下两种方式:
1.通过共享的标记来通知(使用 自定义标志位 来控制线程是否要停止)
2.调用interrupt()方法来通知
(使用 Thread自带的标志位 来进行判定(可以唤醒上面这个sleep这样方法的))
注意:不是让线程立刻停止,而是通知线程,你应该要停止了,是否真的停止,取决于代码写法
方法1——自定义标记flag
public static volatile boolean flag = true;
public static void main2(String[] args) throws InterruptedException {
Thread t1 = new Thread(() -> {
while (flag) {
System.out.println("线程t1正在运行");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
Thread.sleep(3000);
//在主线程里可以随时通过flag变量的取值,来操作t线程是否结束
flag = false;
}
自定义变量这种方式,不能及时响应
尤其是在sleep休眠时间比较长的时候
方法2——调用interrupt方法内的自带标记位
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(() ->{
while (!Thread.currentThread().isInterrupted()){//默认为false,取反一下表示未被终止
//isInterrupt() 为 true 表示被终止,为 false 表示未被终止(此处用了逻辑取反)
System.out.println("hello thread");
try {
Thread.sleep(1000);//如果线程在sleep中休眠,此时调用interrupt会把t线程唤醒,从sleep中提前返回
} catch (InterruptedException e) {//interrupt会触发sleep内部的异常,导致sleep提前返回
e.printStackTrace();
//break;//此时就可以终止线程
//还可以在终止前执行一系列代码后终止,取决于需求
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
});
t.start();
Thread.sleep(3000);//让线程执行3秒
t.interrupt();//终止线程——终止t线程(设置boolean变量)
}
Thread 内部包含了一个 boolean 类型的变量作为线程是否被中断的标记
其实相比于方法1就是把boolean操作封装到Thread的方法里了(片面)
加break的原因:
interrupt会做两件事:
1.把线程内部的标志位(bollean)设置成true(执行到中断异常后还会继续执行)
2.如果线程在进行sleep,就会触发异常,把sleep唤醒
但是sleep在唤醒时,还会做一件事,把刚才设置的这个标志位,再设置回false。(清 空了标志位)
这就导致,当sleep的异常被catch完之后,循环还要继续执行 所以调用interrput,只是通知终止,不是线程一定要乖乖终止!!
线程等待
线程是一个随机调度的过程,等待线程做的事,就是在控制两个线程的结束顺序
本身执行完start后,t线程和main线程就并发执行,分头行动:
main继续往下执行,t也会继续往下执行,一直阻塞到t线程执行结束,main线程才会从join中恢复过来,才能继续往下执行(t线程肯定比main先结束)
阻塞也是一个常见术语——block
public class ThreadDemo3 {
public static void main(String[] args) {
Thread t = new Thread(()->{
for (int i = 0; i < 3; i++) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
//Thread.sleep(5000);
//如果执行join的时候,t已经结束了,join就不会阻塞,会立即返回
System.out.println("join 之前");
//此处的join就是让当前的main线程来等待t线程执行结束(等待t的run执行完)
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("join 之后");
}
}
线程休眠
public static void sleep(long millis) throws InterruptedException休眠当前线程 millis 毫秒public static void sleep(long millis, int nanos) throws InterruptedException可以更高精度的休眠
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
System.out.println(System.currentTimeMillis());
Thread.sleep(3 * 1000);
System.out.println(System.currentTimeMillis());
}
}
获取线程实例
public static Thread currentThread(); 返回当前线程对象的引用
public class ThreadDemo {
public static void main(String[] args) {
Thread thread = Thread.currentThread();
System.out.println(thread.getName());
}
}