java - Running Threads on a File Method -
hi try run thread on "threadedsort", can not use traditional void run method because returns void. tried using synchronised method don't think made difference...same reentrant method, don't know i'm doing wrong.
private static string[] getdatathread(file file) throws ioexception { arraylist<string> data = new arraylist<string>(); bufferedreader in = new bufferedreader(new filereader(file)); // read data file until end of file reached while (true) { string line = in.readline(); if (line == null) { // end of file reached break; } else { //synchronized(line){ lock.lock(); try{ data.add(line); }finally{ lock.unlock(); } //} } } //close input stream , return data in.close(); return data.toarray(new string[0]); } } public static string[] threadedsort(file[] files) throws ioexception, interruptedexception { string[] sorteddata = new string[0]; (file file : files) { string[] data = getdatathread(file); thread.sleep(100); data = mergesort.mergesort(data); sorteddata = mergesort.merge(sorteddata, data); } return sorteddata; }
i not sure why have use synchronized blocks or renentrant locks matter in code. seems have used locks on blocks no resource used in block shared in way.
if want run method threadedsort in parallel , resulting data object can specify data returned class variable , fetch object later on.
Comments
Post a Comment