如果在线程中的InterruptedException的catch块内使用,则返回“return”返回的位置?

时间:2017-03-17 14:56:22

标签: java multithreading

对你来说这可能是一个非常简单的问题,但如果你愿意帮我澄清我的疑问,我将不胜感激。

假设我创建了一个使用sleep()方法的线程,并且从我的main方法中断了它。

这是我的代码片段

class myThread implements Runnable{
    volatile boolean checking=true;
    int counter =1;
    public void run()
    {
         while(checking)
         {
              System.out.println(Thread.currentThread().getName() + " - count - "+counter++);
              try{
                  Thread.sleep(1000);
              }catch(InterruptedException e){
                  System.out.println("Interrupted");
                  Thread.currentThread().interrupt();
                  return;  //line of confusion
              }

         }
    }
    public void stopCounting()
    {
        checking = false;
    }
    public boolean getCheker()
    {
        return checking ;
    }

}
//main class 
public class Demo{
    public static void main(string args[])
    {
        myThread th = new myThread();
        Thread t1 = new Thread(th);
        t1.start();
        for(int i=0;i<50000;i++)
        {
            //do nothing
            System.out.println("Do nothing...");
        }
        t1.interrupt();
        System.out.println("Press enter to stop the counter....");

        Scanner sc = new Scanner(System.in);
        sc.nextLine();
        th.stopCounting();

        //this line is executed when I am not using return statement
        // in thread
        System.out.println("value of while loop cheker is: "+th.getCounter());
    }
}

在上述情况下,“返回”的回归在哪里?

案例1:我检查了是否在catch块中保留了“return”关键字,最后执行的行再次来自main方法,即“按Enter键停止计数器... 。“

案例2:如果省略return语句,那么main中最后执行的行是“System.out.println(”counter的值是“+ th.getCheker());”

我的问题是,为什么在第一种情况下也行“System.out.println(”计数器的值是“+ th.getCheker());”没有执行?

我认为在调用return时,控件应该从线程的“run()”方法返回到启动线程的main中的位置。因此,应该立即执行当时未在main中执行的语句。但似乎我使用return语句应用程序结束。主要还在回归。之后没有任何声明被执行。你能解释一下吗?我缺少什么?

3 个答案:

答案 0 :(得分:1)

你的&#34;混乱线&#34;将导致线程的Stylesheet compilation failed: 3 errors reported XPST0017 XPath syntax error at char 0 on line 15 in {ImageInfo:new(.)}: Cannot find a matching 1-argument function named {java:ImageInfo}new(). For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS XPST0017 XPath syntax error at char 0 on line 17 in {ImageInfo:getWidth($image)}: Cannot find a matching 1-argument function named {java:ImageInfo}getWidth(). For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS XPST0017 XPath syntax error at char 0 on line 18 in {ImageInfo:getHeight($image)}: Cannot find a matching 1-argument function named {java:ImageInfo}getHeight(). For diagnostics on calls to Java methods, use the -TJ command line option or set the Configuration property FeatureKeys.TRACE_EXTERNAL_FUNCTIONS 方法返回。这将导致线程死亡,因为当run()返回时,线程总是死亡。

&#34;案例1:&#34; vs.&#34;案例2:&#34;

你的&#34;混乱线&#34;包含在循环中。如果您取出run()语句,则return方法不会在此时返回:它将返回run()循环的顶部。

  

我认为在调用return时控件应该从&#34; run()&#34;线程到线程起始位置的主要位置的方法。

这不是线程的工作方式。对于新线程来说,回过头来没有任何意义。它是main()线程的工作,用于执行启动新线程之后的语句。新线程的while(checking)方法完成后,除了死亡之外没有什么可做的。

答案 1 :(得分:0)

它将结束函数,当函数返回void时,它不会返回任何内容

答案 2 :(得分:0)

return;将打破while循环。

您可以使用break;checking = false,但效果相同。

  

最后执行的行再次来自主方法,即“按Enter键停止计数器....”

也许那是因为它等着你实际按 Enter

  

如果省略return语句,那么main中最后执行的行是“System.out.println(”counter的值是“+ th.getCheker());”

这行代码与您的问题不符......

  

应该从线程的“run()”方法返回到主线的起始位置

不,它是一个单独的线程,它在后台并行执行。您恰好打印到相同的输出流,因此很难看到。

Thread在自己的线程中使用自己的调用堆栈执行。当您从return run()时,它会结束,因此会停止它,并且它不会在您的代码中“返回到其他地方”。

如果要删除扫描仪,则应运行两个打印语句。

相关问题