代码之家  ›  专栏  ›  技术社区  ›  Themelis

在循环中调用url.openstream()时停止线程执行

  •  1
  • Themelis  · 技术社区  · 7 年前

    编辑 :我知道thread.interrupt()的作用。

    • while (!Thread.currentThread().isInterrupted()) 当我中断线程时不退出。
    • 我还尝试从url.openstream()捕获异常;当 线程被中断(绝望,可能是一种阻塞方法, 如果没有成功,则退出循环。

    应用程序创建一个 Thread 那个 连续读取 一 URL . 3秒钟后 螺纹 被中断,但不幸的是继续执行。

    如何 停止线程 从执行?

    代码(Meav.java,MyRunnabel.java):

    public class Main {
    
        public static void main(String[] args) {
            MyRunnable runnable = new MyRunnable("http://ninjaflex.com/");
            Thread thread = new Thread(runnable);
            thread.start();
            sleep(3000);
            thread.interrupt();
            System.out.println("Thread.interrupt() invoked.");
        }
    
        private static void sleep(long timeMilli) {
            try {
                Thread.sleep(timeMilli);
            } catch (Exception e) {
    
            }
        }
    }
    
    public class MyRunnable implements Runnable {
    
        private String website;
    
        MyRunnable(String website) {
            this.website = website;
        }
    
        @Override
        public void run() {
            URL url = createUrl();
            if (url != null) {
                while (!Thread.currentThread().isInterrupted()) {
                    sleepOneSec();
                    readFromUrl(url);
                    System.out.println("Read from " + website);
                }
                System.out.println("Script: Interrupted, exiting.");
            }
        }
    
        private URL createUrl() {
            URL url = null;
            try {
                url = new URL(website);
            } catch (MalformedURLException e) {
                System.out.println("Wrong URL?");
            }
            return url;
        }
    
        private void sleepOneSec() {
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                System.out.println("Error sleeping");
            }
        }
    
        private void readFromUrl(URL url) {
            InputStream in = null;
            try {
                in = url.openStream();
            } catch (Exception e) {
                System.out.println("Exception while url.openStream().");
                e.printStackTrace();
            } finally {
                closeInputStream(in);
            }
        }
    
        private void closeInputStream(InputStream in) {
            try {
                in.close();
            } catch (IOException e) {
                System.out.println("Error while closing the input stream.");
            }
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  2
  •   Olf    7 年前

    基本上,你的 MyRunnable 线程在睡眠期间中断。 InterreuptedException 被抛出但被捕获。顺便说一下,这是个坏习惯 Exception 你不应该这样做。 来自javadoc:“抛出此异常时,当前线程的中断状态将被清除”。 因此,while循环将永远看不到标志。

        2
  •  1
  •   VGR    7 年前

    将呼叫替换为 sleepOneSec 方法,使用简单的thread.sleep调用。捕获InterruptedException 外部 你的 while 循环。这将导致循环自然退出:

    try {
        while (true) {
            Thread.sleep(1000);
            readFromUrl(url);
            System.out.println("Read from " + website);
        }
    } catch (InterruptedException e) {
        System.out.println("Script: Interrupted, exiting.");
    }
    
        3
  •  0
  •   togise    7 年前

    我删除了myrunable.sleeponesec,你的代码开始工作了。