How to use thread lock inside a buffered reader loop

673 views Asked by At

I am new to java and i was trying to do a simple task with the java example of producer and consumer. In my case, i want to "produce" read file , while reading each line i want to stop the thread. another thread "consumer"comes in and saves that list in db . if there is no thread in the list, it will stop and the pre thread will again start reading the next line. I am this far with the code. can you help me understand what i am doing wrong? Thank you. the expectation i had was print 1 line and print "save in db" print another line and print "save in db" . but it's not happening.

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

public class Processor {
    LinkedList<String[]> abc = new LinkedList<String[]>();
    int size = 1;

    // The name of the file to open.
    String fileName = "//Users//wangel//Desktop//Workbook1.csv";

    // This will reference one line at a time
    String line = null;

    private Object lock = new Object();

    public void produce() throws InterruptedException{
        //get the csv file and read
        System.out.println("Starting to read the file");
        try {
            // FileReader reads text files in the default encoding.
            FileReader fileReader = new FileReader(fileName);

            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = new BufferedReader(fileReader);

                while((line = bufferedReader.readLine()) != null) {
                    synchronized(lock){
                        String row[] = line.split(",");
                        for(int i=0; i<row.length; i++){
                            System.out.print(row[i] + "\t");
                            abc.add(row);
                        }
                        while(abc.size() == size){
                            lock.wait();
                        }
                    //lock.wait();
                    System.out.println();   
                    System.out.println("Resumed");
                    lock.notify();
                    }  
            } 

            // Always close files.
            bufferedReader.close();            
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                   
            // Or we could just do this: 
            // ex.printStackTrace();
        }

    }

    public void consume() throws InterruptedException{
        //save the input data file in the database
        synchronized(lock){

            while(true){
                while(abc.size() == 0){
                    lock.wait();
                }
                lock.notify();
                Thread.sleep(2000);
                System.out.println("Starting to save in the DB");
                abc.removeFirst();
            }
        }   
    }
}

The main class

public class Main {

    public static void main(String[] args) throws InterruptedException{
        // TODO Auto-generated method stub
        final Processor processor = new Processor();    

        Thread t1 = new Thread(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    processor.produce();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });

        Thread t2 = new Thread(new Runnable(){

            @Override
            public void run() {
                // TODO Auto-generated method stub
                try {
                    processor.consume();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        });

        t1.start();
        t2.start();

        t1.join();
        t2.join();
    }

}
0

There are 0 answers