Java之线程篇二

[复制链接]
发表于 2026-2-2 06:09:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

目次

Thread的常见构造方法
Thread的常见属性
代码示例1
代码示例2
示例代码3
代码示例4
代码示例5
小结
线程制止
代码示例1
代码示例2
代码示例3
代码示例4
小结
线程等候
获取当火线程的引用


Thread的常见构造方法


举例
  1. Thread t1 = new Thread();
  2. Thread t2 = new Thread(new MyRunnable());
  3. Thread t3 = new Thread("这是我的名字");
  4. Thread t4 = new Thread(new MyRunnable(), "这是我的名字");
复制代码
代码示例
  1. public class Demo06 {
  2.     public static void main(String[] args) {
  3.         Thread t=new Thread(()->{
  4.             while(true){
  5.                 System.out.println("hello thread");
  6.                 try {
  7.                     Thread.sleep(1000);
  8.                 } catch (InterruptedException e) {
  9.                     throw new RuntimeException(e);
  10.                 }
  11.             }
  12.         },"这是新线程");
  13.         t.start();
  14.     }
  15. }
复制代码
通过jconsole.exe观察

Thread的常见属性

属性获取方法
IDgetId()
名称getName()
状态getState()
优先级getPriority()
是否配景进程isDaemon()
是否存活isAlive()
是否被制止isInterrupted()









   ID 是线程的唯一标识,差别线程不会重复;
名称是各种调试工具用到;
状态体现线程当前所处的一个环境;
优先级高的线程理论上来说更轻易被调治到;
关于配景线程,记取一点:JVM会在一个进程的全部非配景线程竣过后,才会竣事运行;
是否存活,即简单的明白,为 run 方法是否运行竣事了
  代码示例1

  1. public class Demo06 {
  2.     public static void main(String[] args) {
  3.         Thread t=new Thread(()->{
  4.             while(true){
  5.                 System.out.println("hello thread");
  6.                 try {
  7.                     Thread.sleep(1000);
  8.                 } catch (InterruptedException e) {
  9.                     throw new RuntimeException(e);
  10.                 }
  11.             }
  12.         },"这是新线程");
  13.         System.out.println(t.getId());
  14.         System.out.println(t.getName());
  15.         System.out.println(t.getState());
  16.         System.out.println(t.getPriority());
  17.         System.out.println(t.isDaemon());
  18.         System.out.println(t.isAlive());
  19.         System.out.println(t.isInterrupted());
  20.         t.start();
  21.     }
  22. }
复制代码
运行结果

代码示例2

  1. public class Demo07 {
  2.     public static void main(String[] args) throws InterruptedException {
  3.         Thread t=new Thread(()->{
  4.             System.out.println("线程开始");
  5.             try {
  6.                 Thread.sleep(2000);
  7.             } catch (InterruptedException e) {
  8.                 throw new RuntimeException(e);
  9.             }
  10.             System.out.println("线程结束");
  11.         });
  12.         System.out.println(t.isAlive());
  13.         t.start();
  14.         System.out.println(t.isAlive());
  15.         Thread.sleep(3000);
  16.         System.out.println(t.isAlive());
  17.     }
  18. }
复制代码
运行结果

示例代码3

  1. public class Demo08 {
  2.     private static boolean isQuit=false;
  3.     public static void main(String[] args) throws InterruptedException {
  4.         Thread t=new Thread(()->{
  5.             while(!isQuit){
  6.                 System.out.println("线程工作中");
  7.                 try {
  8.                     Thread.sleep(1000);
  9.                 } catch (InterruptedException e) {
  10.                     throw new RuntimeException(e);
  11.                 }
  12.             }
  13.             System.out.println("线程工作完毕!");
  14.         });
  15.         t.start();
  16.         Thread.sleep(5000);
  17.         isQuit=true;
  18.         System.out.println("设置 isQuit 为 true");
  19.     }
  20. }
复制代码
运行结果

代码示例4

  1. public class Demo09 {
  2.     public static void main(String[] args) throws InterruptedException {
  3.         Thread t=new Thread(()->{
  4.             //Thread类内部有一个标志位,可以用来判定当前的循环是否要结束
  5.             while(!Thread.currentThread().isInterrupted()){
  6.                 System.out.println("线程工作中");
  7.                 try {
  8.                     Thread.sleep(1000);
  9.                 } catch (InterruptedException e) {
  10.                     //throw new RuntimeException(e);
  11.                     //1.假装没听见,循环继续正常执行
  12.                     e.printStackTrace();
  13.                     //2.加上一个bread,表示让线程立即结束
  14.                     //break;
  15.                     //3.做一些其它工作,完成之后再结束
  16.                     break;
  17.                 }
  18.             }
  19.         });
  20.         t.start();
  21.         Thread.sleep(5000);
  22.         System.out.println("让 t 线程终止");
  23.         t.interrupt();
  24.     }
  25. }
复制代码
运行结果

代码示例5

  1. public class Demo10 {
  2.     public static void main(String[] args) throws InterruptedException {
  3.         Thread t=new Thread(()->{
  4.             for (int i = 0; i < 5; i++) {
  5.                 System.out.println("t 线程工作中");
  6.                 try {
  7.                     Thread.sleep(1000);
  8.                 } catch (InterruptedException e) {
  9.                     throw new RuntimeException(e);
  10.                 }
  11.             }
  12.         });
  13.         t.start();
  14.         
  15.         System.out.println("join 等待开始");
  16.         t.join();
  17.         System.out.println("join 等待结束");
  18.     }
  19. }
复制代码
小结

   上面的代码是让主线程来等候 t 线程实行竣事,一旦调用 join,主线程就会触发壅闭,此时 t 线程就可以乘隙完成后续的工作,不绝到壅闭到 t 实行完毕,join才会排除壅闭。
  
  即:哪个线程调用 join(),哪个线程就会被壅闭。
  代码示例6
  1. public class Demo11 {
  2.     public static void main(String[] args) throws InterruptedException {
  3.         Thread t=new Thread(()->{
  4.             while(true){
  5.                 try {
  6.                     Thread.sleep(1000);
  7.                 } catch (InterruptedException e) {
  8.                     throw new RuntimeException(e);
  9.                 }
  10.             }
  11.         });
  12.         System.out.println(t.getState());
  13.         t.start();
  14.         for (int i = 0; i < 5; i++) {
  15.             System.out.println(t.getState());
  16.             Thread.sleep(1000);
  17.         }
  18.         System.out.println(t.getState());
  19.     }
  20. }
复制代码
 运行结果

线程制止

常见的两种方式:
   1.通过共享的标记来举行沟通;
  2.调用 interrupt() 方法来关照。
  

代码示例1

  1. public class Demo11 {
  2.     private static class MyRunnable implements Runnable{
  3.         public volatile boolean isQuit=false;
  4.         @Override
  5.         public void run() {
  6.             while(!isQuit) {
  7.                 System.out.println("开始");
  8.                 try {
  9.                     Thread.sleep(1000);
  10.                 } catch (InterruptedException e) {
  11.                     throw new RuntimeException(e);
  12.                 }
  13.             }
  14.             System.out.println("结束");
  15.         }
  16.     }
  17.     public static void main(String[] args) throws InterruptedException {
  18.         MyRunnable target=new MyRunnable();
  19.         Thread t=new Thread(target);
  20.         t.start();
  21.         Thread.sleep(5000);
  22.         target.isQuit=true;
  23.     }
  24. }
复制代码
运行结果

代码示例2

  1. public class Demo11 {
  2.     private static class MyRunnable implements Runnable{
  3.         @Override
  4.         public void run() {
  5.             while(!Thread.interrupted()) {
  6.                 System.out.println("开始");
  7.                 try {
  8.                     Thread.sleep(1000);
  9.                 } catch (InterruptedException e) {
  10.                     throw new RuntimeException(e);
  11.                 }
  12.             }
  13.             System.out.println("结束");
  14.         }
  15.     }
  16.     public static void main(String[] args) throws InterruptedException {
  17.         MyRunnable target=new MyRunnable();
  18.         Thread t=new Thread(target);
  19.         t.start();
  20.         Thread.sleep(5000);
  21.         t.interrupt();
  22.     }
  23. }
复制代码
运行结果

代码示例3

  1. public class Demo11 {
  2.     private static class MyRunnable implements Runnable{
  3.         @Override
  4.         public void run() {
  5.             while(true){
  6.                 System.out.println(Thread.currentThread().isInterrupted());
  7.             }
  8.         }
  9.     }
  10.     public static void main(String[] args) throws InterruptedException {
  11.         MyRunnable target=new MyRunnable();
  12.         Thread t=new Thread(target);
  13.         t.start();
  14.         Thread.sleep(2000);
  15.         t.interrupt();
  16.     }
  17. }
复制代码
 运行结果

代码示例4

  1. public class Demo11 {
  2.     private static class MyRunnable implements Runnable{
  3.         @Override
  4.         public void run() {
  5.             while(true){
  6.                 System.out.println(Thread.interrupted());
  7.             }
  8.         }
  9.     }
  10.     public static void main(String[] args) throws InterruptedException {
  11.         MyRunnable target=new MyRunnable();
  12.         Thread t=new Thread(target);
  13.         t.start();
  14.         Thread.sleep(2000);
  15.         t.interrupt();
  16.     }
  17. }
复制代码
运行结果

小结

   假如线程由于调用wait/join/sleep等方法而壅闭挂起,则以InterruptedException非常的情势关照,扫除制止标记,当出现InterruptedException的时间,要不要竣事线程取决于catch中代码的写法,可以选择忽略这个非常,也可以选择跳出循环竣事线程;
  
  1.Thread.interrupted()判断当火线程的制止标记被设置,扫除制止标记;
  2.Thread.currentThread().isInterrupted()判断指定线程的制止标记被设置,不清楚制止标记;
  这两种方法关照收到的更及时,及时线程正在sleep也可以立刻收到。
  线程等候


获取当火线程的引用

方法属性
public static Thread currentThread()返回当火线程对象的引用




  1.     public class ThreadDemo {
  2.         public static void main(String[] args) {
  3.             Thread thread = Thread.currentThread();
  4.             System.out.println(thread.getName());
  5.         }
  6.     }
复制代码

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表