如果发生FileNotException,如何停止生产者线程和消费者线程

时间:2016-01-19 15:32:17

标签: java multithreading

我正在从某个位置读取csv文件。

请告诉我如何在这种情况下找不到Producer Thread和Consumer Thread incase文件?

下面是我的程序,它创建两个线程,Producer和Consumer Threads来读取文件中的数据

package com.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;
import au.com.bytecode.opencsv.CSVReader;

public class TestProgram {
    public static void main(String args[]) {
        final String startToken = ",Nifty 50 Gainers";
        final String endToken = "50 Losers";
        final PipedWriter pipedWriter = new PipedWriter();
        PipedReader pipedReaderTmp = null;
        try {
            pipedReaderTmp = new PipedReader(pipedWriter);
        } catch (IOException e) {
        }
        final PipedReader pipedReader = pipedReaderTmp;
        // Consumer
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    CSVReader csvReader = new CSVReader(pipedReader);
                    while (true) {
                        String[] line = csvReader.readNext(); // blocks until the next line is available
                        if (line == null)
                            break; // end of stream has been reached
                        if (line != null && line.length > 3) {
                            String indices_name = line[1];
                            if (indices_name != null) {

                                System.out.println(indices_name);
                            }

                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {

                    e.printStackTrace();
                }
            }
        }).start();

        // Producer
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BufferedReader br = new BufferedReader(new FileReader(
                            "C:\\Users\\ravikiranv\\Downloads\\MA050116.csv"));
                    String line = null;
                    while ((line = br.readLine()) != null) {
                        if (startToken.equals(line))
                            break;
                    }
                    while ((line = br.readLine()) != null) {
                        if (line.contains((endToken))) {
                            break;
                        } else {
                            pipedWriter.write(line + '\n');
                        }
                    }
                    pipedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

2 个答案:

答案 0 :(得分:1)

请参阅Javadoc PipedReader.read()

  

抛出:       IOException - 如果管道损坏,未连接,关闭,或发生I / O错误。

所以只需close,另一端即可获得IOException

注意:PipedReader有一些issues - 您可能会发现使用类似“BlockingQueue”的内容更安全/更好。

答案 1 :(得分:0)

只有在知道文件存在的情况下,才能并且应该实例化并启动线程。这样可以保证您免受内部阻止线程的影响。

File file = new File("C:\\Users\\ravikiranv\\Downloads\\MA050116.csv");

if (file.exists()) {
     new Thread(new Runnable() { ... }).start();
     ...
}

如果您因某种原因不想这样做,您可以保留对消费者线程的引用并从生产者中停止它。有关如何执行此操作的示例,请参阅this answer