java - Synchronized writing and reading in file with not join object variable of file -
i'm trying implement threads concurrency in java. consists of trying write in file last time. there 2 threads: a
- creating file , checking if right line in file , b
- searching file , trying rewrite file “good” line. “winner” thread must have string in file. thread checks if file has line, file has 1 line. threads have file path.
public class implements runnable { private file file; private thread t; public a(string patch,string filename) { t = new thread(this); createfile(patch, filename); //t.setdaemon(true); t.start(); } @override public void run() { bufferedreader reader; while (!thread.currentthread().isinterrupted()) { try { reader = new bufferedreader(new inputstreamreader(new fileinputstream(file))); if (reader.readline().charat(0) == 'b') { system.out.println("a try took file: " + file.getname()); write(); } else { system.out.println("a took file: " + file.getname()); } } catch (filenotfoundexception e) { system.out.println("file read a" + e.tostring()); } catch (ioexception e) { system.out.println("file read a"+e.tostring()); } } } private void write() { try { printwriter printwriter = new printwriter(file); printwriter.println("a took file: " + file.getname()); system.out.println("a took file: " + file.getname()); printwriter.close(); } catch (exception e) { system.out.println("file write a"); } } public file createfile(string patch,string filename) { file file = new file(patch,filename+".txt"); try { printwriter printwriter = new printwriter(file); printwriter.println("a took file: " + file.getname()); system.out.println("a took file: " + file.getname()); printwriter.close(); } catch (exception e) { system.out.println("file create a"); } return file; } } public class b implements runnable { private file file; private thread t; public b(string patch,string filename) { t = new thread(this); //t.setdaemon(true); findfile(patch, filename); t.start(); } @override public void run() { bufferedreader reader; while (!thread.currentthread().isinterrupted()) { try { reader = new bufferedreader(new inputstreamreader(new fileinputstream(file))); if (reader.readline().charat(0) == 'a') { system.out.println("b try took file: " + file.getname()); write(); } else { system.out.println("b took file: " + file.getname()); } } catch (filenotfoundexception e) { system.out.println("file read b" + e.tostring()); } catch (ioexception e) { system.out.println("file read b"+e.tostring()); } } } private void write() { try { printwriter printwriter = new printwriter(file); printwriter.println("b took file: " + file.getname()); system.out.println("b took file: " + file.getname()); printwriter.close(); } catch (exception e) { system.out.println("file write b"); } } public file findfile(string patch,string filename) { file file= null; file folder = new file(patch); file[] listoffiles = folder.listfiles(); bufferedreader reader; (int = 0; < listoffiles.length; i++) { file = listoffiles[i]; if (file.getname().equals(filename + ".txt")) { break; } } return file; } }
i want synchronize in way access file in threads. in code have java.lang.nullpointerexception
when use readline()
, think because threads don’t have synchronized access file (after every finished operation file must have 1 line). can’t use synchronized method or block, because threads don’t have joint variable of file. there way of making synchronized writing , reading in file?
you can use reentrantreadwritelock synchronize. parse same lock synchronize both , b. here have modified b. can same well.
public static class b implements runnable { private file file; private thread t; private reentrantreadwritelock lock; public b(string patch,string filename, reentrantreadwritelock lock) { t = new thread(this); this.lock = lock; //t.setdaemon(true); findfile(patch, filename); t.start(); } @override public void run() { bufferedreader reader; while (!thread.currentthread().isinterrupted()) { try { lock.readlock().lock(); reader = new bufferedreader(new inputstreamreader(new fileinputstream(file))); if (reader.readline().charat(0) == 'a') { lock.readlock().unlock(); lock.writelock().lock(); system.out.println("b try took file: " + file.getname()); write(); lock.writelock().unlock(); } else { system.out.println("b took file: " + file.getname()); } } catch (filenotfoundexception e) { system.out.println("file read b" + e.tostring()); } catch (ioexception e) { system.out.println("file read b"+e.tostring()); } } } private void write() { try { printwriter printwriter = new printwriter(file); printwriter.println("b took file: " + file.getname()); system.out.println("b took file: " + file.getname()); printwriter.close(); } catch (exception e) { system.out.println("file write b"); } } public file findfile(string patch,string filename) { file file= null; file folder = new file(patch); file[] listoffiles = folder.listfiles(); bufferedreader reader; (int = 0; < listoffiles.length; i++) { file = listoffiles[i]; if (file.getname().equals(filename + ".txt")) { break; } } return file; } }
Comments
Post a Comment