目录
文章目录
一、线程应该怎么编程?
二、线程的创建
三、线程的启动-start()
3.1Thread类中run和start的区别
四、线程的中断
五、线程的等待-join()
六、线程休眠
七、获取线程实例
一、线程应该怎么编程?
线程应该怎么去进行编程?Java中有一个Thread类用来专门对线程编程的类。
Thread 类是 JVM 用来管理线程的一个类,换句话说,每个线程都有一个唯一的 Thread 对象与之关联。
Thread类的对象就是用来描述一个线程执行流的,JVM会将这些Thread对象组织起来,用于线程调度,线程管理。
方法1:继承Thread类
public class ThreadDemo {public static void main(String[] args) {Thread t = new MyThread();t.start();while (true) {System.out.println("hello main");}} }class MyThread extends Thread{@Overridepublic void run() {while (true) {System.out.println("hello world");}} }
上述代码涉及俩个线程:
1.main方法所对应的线程(一个进程里面至少得有一个线程)也可以成为主线程
2.通过t.start()创建的新的线程
方法二:实现Runnable接口
public class MyRunnable implements Runnable{@Overridepublic void run() {while (true) {System.out.println("hello,thread");}}public static void main(String[] args) {MyRunnable runnable = new MyRunnable();Thread t = new Thread(runnable);t.start();while (true) {System.out.println("hello main");}} }
第一种写法是使用Thread的run描述线程入口
第二章写法是使用Runnable interface 来描述线程入口
没有本质区别
方法三: 继承Thread,使用匿名内部类
public class ThreadDemo1 {public static void main(String[] args) {Thread t = new Thread(){@Overridepublic void run(){while (true){System.out.println("hello t"+ Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}}};t.start();} }
方法四:实现Runnable,使用匿名内部类,内部类(定义在类里面的类)
public class MyRunnable2 {public static void main(String[] args) {Thread t = new Thread(new Runnable() {@Overridepublic void run() {System.out.println("hello t");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();} }
{ 放到哪里就是针对哪个类创建的匿名内部类
方法五:使用Lambda表达式,最简单直观的方法
lambda表达式的基本写法:()->{ }
public class MyThreadDemo3 {public static void main(String[] args) {Thread t = new Thread(() -> {while (true){System.out.println("hello t"+Thread.currentThread().getName());try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();} }
之前我们已经看到了如何通过覆写 run 方法创建一个线程对象,但线程对象被创建出来并不意味着线程就开始运行了。
覆写 run 方法是提供给线程要做的事情的指令清单
线程对象可以认为是把 李四、王五叫过来了
而调用 start() 方法,就是喊一声:”行动起来!“,线程才真正独立去执行了。
调用 start 方法, 才真的在操作系统的底层创建出一个线程.
run方法的作用是描述线程具体要执行的任务;start方法的作用是真正的去申请系统线程
run方法是一个类中的普通方法,主动调用和调用普通方法一样,会顺序执行一次;
start调用方法后, start方法内部会调用Java 本地方法(封装了对系统底层的调用)真正的启动线程,并执行run方法中的代码,run 方法执行完成后线程进入销毁阶段。
中断一个线程,就是让一个线程停下来,线程的终止,本质上来说,让一个线程终止,就是让该线程的入口方法执行完毕。
目前常见的有以下两种方式:
1. 通过共享的标记来进行沟通
2. 调用 interrupt() 方法来通知
方法 | 说明 |
public void interrupt() | 中断对象关联的线程,如果线程正在阻塞,则以异常方式通知, 否则设置标志位 |
public static boolean interrupted() | 判断当前线程的中断标志位是否设置,调用后清除标志位 |
public boolean isInterrupted() | 判断对象关联的线程的标志位是否设置,调用后不清除标志位 |
1、要线程中断,首先要给线程中设定一个结束标志位
这个死循环,导致入口方法无法执行完毕,不能结束线程,那么我们把循环条件用一个变量来控制:
此时要记住,在lambda表达式中只能捕获的变量是final或者”实际final“
下面不是static修饰的外面的局部变量:
当前是使用自己创建的变量来控制循环,Thread类内置了一个标志位,让我们更方便的实现上述效果:
public class ThreadDemo1 {public static void main(String[] args) {Thread t = new Thread(() -> {while (!Thread.currentThread().isInterrupted()){System.out.println("hello t");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}}});t.start();try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}t.interrupt();} }
为什么会出现异常后还会正常继续执行??
这是因为interrupt方法的作用:
1.设置表示位为true
2.如果该线程正在阻塞中(比如正在执行sleep),此时就是把阻塞状态唤醒,通过抛出异常的方式让sleep立即结束
注意!!!
当sleep被唤醒的时候,sleep会自动的把interrupted标志位给清空(true -> false)这样下次循环就可以正常执行了
如果想让线程结束那么加上一个break
public class ThreadDemo1 {public static void main(String[] args) {Thread t = new Thread(() -> {while (!Thread.currentThread().isInterrupted()){System.out.println("hello t");try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();break;}}});t.start();try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}t.interrupt();}
}
线程之间是并发执行的,操作系统对于线程的调度是无序的,无法判断俩个线程谁先执行谁先结束,那么就要通过join方法来使线程来等待
public class ThreadDemo2 {public static void main(String[] args) throws InterruptedException {Thread t = new Thread(() -> {System.out.println("hello t");});t.start();t.join();System.out.println("hello main");}
}
意思是在main线程中,调用join方法,让main线程等待t先结束,再往下执行
也是我们比较熟悉一组方法,有一点要记得,因为线程的调度是不可控的,所以,这个方法只能保证实际休眠时间是大于等于参数设置的休眠时间的
方法 | 说明 |
public static void sleep(long millis) throws InterruptedException | 休眠当前线程 millis 毫秒 |
public static void sleep(long millis, int nanos) throws InterruptedException | 可以更高精度的休眠 |
public class ThreadDemo6 {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 ThreadDemo6 {public static void main(String[] args) {Thread thread = Thread.currentThread();System.out.println(thread.getName());}
}