java - How to use thread lock inside a buffered reader loop -


i new java , trying simple task java example of producer , consumer. in case, want "produce" read file , while reading each line want stop thread. thread "consumer"comes in , saves list in db . if there no thread in list, stop , pre thread again start reading next line. far code. can me understand doing wrong? thank you. expectation had print 1 line , print "save in db" print line , print "save in db" . 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;      // name of file open.     string filename = "//users//wangel//desktop//workbook1.csv";      // reference 1 line @ time     string line = null;      private object lock = new object();      public void produce() throws interruptedexception{         //get csv file , read         system.out.println("starting read file");         try {             // filereader reads text files in default encoding.             filereader filereader = new filereader(filename);              // 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();                     }               }               // close files.             bufferedreader.close();                     }         catch(filenotfoundexception ex) {             system.out.println(                 "unable open file '" +                  filename + "'");                         }         catch(ioexception ex) {             system.out.println(                 "error reading file '"                  + filename + "'");                                // or this:              // ex.printstacktrace();         }      }      public void consume() throws interruptedexception{         //save input data file in database         synchronized(lock){              while(true){                 while(abc.size() == 0){                     lock.wait();                 }                 lock.notify();                 thread.sleep(2000);                 system.out.println("starting save in 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();     }  } 


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -