Java 如何在缓冲读取器循环中使用线程锁
我是java新手,我试图用生产者和消费者的java示例来完成一项简单的任务。 在我的例子中,我想“产生”读取文件,同时读取每一行我想停止线程。另一个线程“consumer”进入并将该列表保存在db中。如果列表中没有线程,它将停止,预线程将再次开始读取下一行。 我对代码了解得这么多。你能帮我理解我做错了什么吗?非常感谢。 我的期望是打印一行并打印“保存在数据库中”,打印另一行并打印“保存在数据库中”。但这并没有发生Java 如何在缓冲读取器循环中使用线程锁,java,multithreading,synchronization,threadpool,Java,Multithreading,Synchronization,Threadpool,我是java新手,我试图用生产者和消费者的java示例来完成一项简单的任务。 在我的例子中,我想“产生”读取文件,同时读取每一行我想停止线程。另一个线程“consumer”进入并将该列表保存在db中。如果列表中没有线程,它将停止,预线程将再次开始读取下一行。 我对代码了解得这么多。你能帮我理解我做错了什么吗?非常感谢。 我的期望是打印一行并打印“保存在数据库中”,打印另一行并打印“保存在数据库中”。但这并没有发生 import java.io.BufferedReader; import jav
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();
}
}
}
}
一个线程正在读取文件(不停止操作keep reading)并将数据添加到线程安全列表中,比如CopyOnWriteArrayList(应该在主程序中定义-我的意思是可从使用者访问)。当列表中出现某个内容时,将其写入数据库。一旦写入,从列表中删除该数据,然后进入睡眠状态,比如说500微秒。不需要在代码中同步。列表本身已同步。@bobs_007否。应该使用
sleep()
的地方很少,生产者/消费者模式不在其中。另外,List
是一个接口,实现它的许多类都是不同步的。至于OP,您可以从主类中删除join()
s,并且您的消费者不应该在使用该行之前调用notify()
(将其移动到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();
}
}