1

I am reading a csv file from a location.

Could you please tell me how can I stop the Producer Thread and Consumer Thread incase file is not found in this case ?

Below is my program which creates two threads, Producer and Consumer Threads to read the data from the file

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();
    }
}
liam_g
  • 314
  • 1
  • 5
  • 15
Kiran
  • 59
  • 7

2 Answers2

1

See the Javadoc for PipedReader.read().

Throws: IOException - if the pipe is broken, unconnected, closed, or an I/O error occurs.

So just close it and the other end wwil get an IOException.

Note: The PipedReader has some issues - you may find it safer/better to use something like a `BlockingQueue.

Community
  • 1
  • 1
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

You can and should instantiate and start the Threads only if you know the file exists. This keeps you safe from the concerns to stop threads from within.

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

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

If you don't want that for a reason, you could keep a reference to the consumer thread and stop it from within the producer. For an example how to do that, see this answer

Community
  • 1
  • 1
wonderb0lt
  • 2,035
  • 1
  • 23
  • 37