Thread 类的基本用法

左手的ㄟ右手 2024-03-25 14:04 241阅读 0赞

目录

1.线程创建

1.继承 Thread, 重写 run

2.实现 Runnable, 重写 run

3.继承 Thread, 重写 run, 使用匿名内部类

4.实现 Runnable, 重写 run, 使用匿名内部类

5.使用 lambda 表达式

2.线程中断

2.1给线程中设定一个结束标志位

2.2调用 interrupt() 方法来通知

3.线程等待

4.线程休眠

5.获取线程实例


1.线程创建

1.继承 Thread, 重写 run

  1. class MyThread extends Thread{
  2. @Override
  3. public void run() {
  4. while(true){
  5. System.out.println("hello t");
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. }
  13. }
  14. public class test1 {
  15. public static void main(String[] args) {
  16. Thread t=new MyThread();
  17. t.start();
  18. while(true){
  19. System.out.println("hello main");
  20. try {
  21. Thread.sleep(1000);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }

2.实现 Runnable, 重写 run

  1. class MyRunnable implements Runnable{
  2. @Override
  3. public void run() {
  4. while(true){
  5. System.out.println("hello t");
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. }
  13. }
  14. public class test2 {
  15. public static void main(String[] args) {
  16. MyRunnable runnable=new MyRunnable();
  17. Thread t=new Thread(runnable);
  18. t.start();
  19. System.out.println("hello main");
  20. try {
  21. Thread.sleep(1000);
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }

3.继承 Thread, 重写 run, 使用匿名内部类

  1. public class test3 {
  2. public static void main(String[] args) {
  3. Thread t=new Thread(){
  4. @Override
  5. public void run() {
  6. while(true){
  7. System.out.println("hello t");
  8. try {
  9. Thread.sleep(1000);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. }
  15. };
  16. t.start();
  17. while(true){
  18. System.out.println("hello main");
  19. try {
  20. Thread.sleep(1000);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }

4.实现 Runnable, 重写 run, 使用匿名内部类

  1. public class test4 {
  2. public static void main(String[] args) {
  3. Thread t=new Thread(new Runnable() {
  4. @Override
  5. public void run() {
  6. while(true){
  7. System.out.println("hello t");
  8. try {
  9. Thread.sleep(1000);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. }
  13. }
  14. }
  15. });
  16. t.start();
  17. while(true){
  18. System.out.println("hello main");
  19. try {
  20. Thread.sleep(1000);
  21. } catch (InterruptedException e) {
  22. e.printStackTrace();
  23. }
  24. }
  25. }
  26. }

5.使用 lambda 表达式

  1. public class test5 {
  2. public static void main(String[] args) {
  3. Thread t=new Thread(()->{
  4. while(true){
  5. System.out.println("hello t");
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();
  10. }
  11. }
  12. });
  13. t.start();
  14. while(true){
  15. System.out.println("hello main");
  16. try {
  17. Thread.sleep(1000);
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
  23. }

2.线程中断

让一个线程停下来,线程的终止

本质上,线程终止办法就一种,让线程的入口方法执行完毕

2.1给线程中设定一个结束标志位

  1. while(true){
  2. ...
  3. }

死循环,导致入口方法无法执行完毕

解决方法

  1. blic static boolean isQuit=false;
  2. public static void main(String[] args) {
  3. Thread t=new Thread( ()->{
  4. while(!isQuit){
  5. System.out.println("hello t");
  6. try {
  7. Thread.sleep(1000);
  8. } catch (InterruptedException e) {
  9. e.printStackTrace();//打印当前异常位置的调用栈
  10. }
  11. }
  12. System.out.println("t 线程终止");
  13. });
  14. t.start();
  15. //在主线程中,修改isQuit
  16. try {
  17. Thread.sleep(3000);
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. isQuit=true;
  22. }

#

#

" class="reference-link">4762a0a280524e858d23b90314c3f4be.png

2.2调用 interrupt() 方法来通知

interrupt方法的作用:

1.设置标志位为true

2.如果该线程正在阻塞中(比如在执行sleep)

  1. public static void main(String[] args) {
  2. Thread t=new Thread(() ->{
  3. //currentThread是获取到当前线程实例
  4. //currentThread得到的对象是t
  5. //isInterrupted是t对象里自带的一个标志位
  6. while(!Thread.currentThread().isInterrupted()){
  7. System.out.println("hello t");
  8. try {
  9. Thread.sleep(1000);
  10. } catch (InterruptedException e) {
  11. e.printStackTrace();
  12. break;
  13. //如果要结束循环,要在catch中加break
  14. }
  15. }
  16. });
  17. t.start();
  18. try {
  19. Thread.sleep(3000);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. //把t内部的标志位设置为true
  24. t.interrupt();
  25. }

07c7fd4d8c9f416ba1da350d78137d79.png

此时会把阻塞状态唤醒

通过抛出异常的方法让sleep立即结束

当sleep被唤醒时,sleep会自动把isInterrupted标志位给清空(true->false)

观察标志位是否清除:

如果sleep执行时,标志位为false,sleep正常休眠

如果标志位为true,无论sleep刚执行或执行一半:

1.立刻抛出异常

2.清空标志位为false

下次循环到sleep,由于标志位本身是false,就什么都不干

多线程执行顺序:

每个线程都是独立执行,执行到start方法,代码兵分两路,主线程继续执行,新线程进入到run方法执行,主线程执行完interrupt,继续往后执行,不会再到interrupt

interrupt只是通知,不是”命令”,是否结束由代码来灵活控制

3.线程等待

线程之间并发执行,操作系统对线程的调度,无法确定哪个线程先执行结束

用线程等待可以规定结束顺序:如果t1线程中,调用t2.join,是让t1线程等待t2结束

  1. Thread t=new Thread(()->{
  2. System.out.println("t");
  3. });
  4. t.start();
  5. t.join();
  6. //在main中调用t.join,是让main线程等待t先结束,在往下执行
  7. //在t.join执行,如果t线程还没结束,main线程会阻塞等待
  8. System.out.println("main");

1.main线程调用tjoin的时候,如果t还执行

此时main线程阻塞,再到t执行完毕(t的run执行完了),main才从阻塞中解除,才执行

2.main线程调用t.join的时候,如果t结束

此时join不会阻塞,会立即往下执行

都保证t是先结束的

join还有一个有参的版本,填写一个参数,作为超时时间,最大等待时间

如果等待时间到达上限,还没等待,就不等了

4.线程休眠
















方法

说明

public static void sleep(long millis) throws InterruptedException

休眠当前线程 millis

毫秒

public static void sleep(long millis, int nanos) throws

InterruptedException

可以更高精度的休眠

  1. try {
  2. Thread.sleep(1000);
  3. } catch (InterruptedException e) {
  4. e.printStackTrace();
  5. break;
  6. //如果要结束循环,要在catch中加break
  7. }

5.获取线程实例












方法

说明

public static Thread currentThread();

返回当前线程对象的引用

  1. public static void main(String[] args) {
  2. Thread thread = Thread.currentThread();
  3. System.out.println(thread.getName());
  4. }

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读