鱼C论坛

 找回密码
 立即注册
查看: 506|回复: 3

return相关的基础问题

[复制链接]
发表于 2023-8-28 09:50:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

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

x
  1. public class ThreadTest09 {
  2.     public static void main(String[] args){
  3.         MyRunnable6 r = new MyRunnable6();
  4.         Thread t = new Thread(r);
  5.         t.setName("分支线程t");
  6.         t.start();

  7.         //5秒后想让它中断
  8.         try {
  9.             Thread.sleep(1000*5);
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.         r.run = false;

  14.     }

  15. }

  16. class MyRunnable6 implements Runnable{

  17.     boolean run = true;

  18.     /*第一种
  19.    @Override
  20.     public void run() {
  21.         if (run){
  22.             for (int i = 0; i < 10; i++) {
  23.                 System.out.println(Thread.currentThread().getName()+"---->"+i);
  24.                 try {
  25.                     Thread.sleep(1000);
  26.                 } catch (InterruptedException e) {
  27.                     e.printStackTrace();
  28.                 }
  29.             }
  30.         }else {
  31.             return;
  32.         }
  33.     }*/

  34.     @Override
  35.     public void run() {
  36.         for (int i = 0; i < 10; i++) {
  37.             if (run){
  38.                 System.out.println(Thread.currentThread().getName()+"---->"+i);
  39.                 try {
  40.                     Thread.sleep(1000);
  41.                 } catch (InterruptedException e) {
  42.                     e.printStackTrace();
  43.                 }
  44.             }else {
  45.                 return;
  46.             }
  47.         }
  48.     }
  49. }
复制代码


为什么不能用第一种中断线程的执行  
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-28 09:51:18 | 显示全部楼层
第一种中断线程的执行的代码是被注释掉的,因此无法执行。在注释中,返回语句被用于中断线程的执行。当run变量为false时,返回语句会提前结束run方法的执行,从而中断线程。但是,在第一种方式中,即使run变量为false,线程仍然会继续执行for循环中的代码,这是因为return语句只会中断当前的run方法,而不会中断整个线程的执行。

在第二种方式中,当run变量为false时,使用return语句中断了整个run方法的执行,从而中断了线程的执行。因此,第二种方式是正确的中断线程的方式。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-28 09:51:48 | 显示全部楼层
第一种中断线程的执行的方法是通过在run()方法中使用return语句来提前结束线程。但是,这种方式并不会真正中断线程的执行,而只是提前将run()方法返回,正常执行后续代码。

在你提供的代码中,使用第一种方式时,当runfalse时,线程的执行会跳出循环并执行return语句,但是线程并没有被真正中断,仍会继续执行其余的代码,直至run()方法结束。

要实现真正的线程中断,可以使用Thread.interrupt()方法来中断线程。调用该方法会设置线程的中断标志位,然后需要在线程的执行逻辑中判断中断标志位,并根据需要做出相应的处理。

以下是修改后的代码示例,使用Thread.interrupt()方法来中断线程的执行:

  1. public class ThreadTest09 {
  2.     public static void main(String[] args) {
  3.         MyRunnable6 r = new MyRunnable6();
  4.         Thread t = new Thread(r);
  5.         t.setName("分支线程t");
  6.         t.start();

  7.         // 5秒后想让它中断
  8.         try {
  9.             Thread.sleep(1000 * 5);
  10.         } catch (InterruptedException e) {
  11.             e.printStackTrace();
  12.         }
  13.         
  14.         t.interrupt(); // 中断线程
  15.         r.run = false;
  16.     }
  17. }

  18. class MyRunnable6 implements Runnable {
  19.     boolean run = true;

  20.     @Override
  21.     public void run() {
  22.         for (int i = 0; i < 10; i++) {
  23.             if (Thread.currentThread().isInterrupted()) {
  24.                 System.out.println("线程已被中断");
  25.                 return; // 根据需要进行处理,可以提前结束线程
  26.             }

  27.             if (run) {
  28.                 System.out.println(Thread.currentThread().getName() + " ----> " + i);
  29.                 try {
  30.                     Thread.sleep(1000);
  31.                 } catch (InterruptedException e) {
  32.                     System.out.println("线程被中断");
  33.                     return; // 根据需要进行处理,可以提前结束线程
  34.                 }
  35.             } else {
  36.                 return; // 根据需要进行处理,可以提前结束线程
  37.             }
  38.         }
  39.     }
  40. }
复制代码


通过以上修改,当调用t.interrupt()方法时,线程将通过判断中断标志位来提前结束执行。

请注意,在run()方法中使用Thread.currentThread().isInterrupted()方法来判断线程的中断状态,并根据需要进行相应的处理。同时,在捕获InterruptedException异常时,也可以通过return语句提前结束线程的执行。

希望能帮助到你!如果你还有任何问题,请随时提问。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-8-28 18:48:06 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 05:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表