如何在缓冲的阅读器循环中使用线程锁

时间:2015-06-15 04:48:44

标签: java multithreading synchronization threadpool

我是java的新手,我试图用生产者和消费者的java例子做一个简单的任务。 在我的情况下,我想“生成”读取文件,同时读取每行我想停止线程。另一个线程“consumer”进来并将该列表保存在db中。如果列表中没有线程,它将停止,并且预线程将再次开始读取下一行。 我对代码这么远了。你能帮我理解我做错了什么吗?谢谢。 我的期望是打印1行并打印“在db中保存”打印另一行并打印“保存在db”。但它没有发生。

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();
            }
        }   
    }
}

主要课程

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 个答案:

没有答案