* If it has not written the EXIT flag then it will go into wait stage and
* will wait for READER to signal that it safe to write now.
*/
public static class FileWrite implements Runnable {
public void run() {
try {
fileLock.lock();
for (int i = 0; i < NO_OF_LINES; i++) {
PrintWriter writer = new PrintWriter(new File(fileName));
if (i != NO_OF_LINES - 1) {
int random = new SecureRandom().nextInt();
System.out.println("WRITER WRITING " + random);
writer.println(random);
writer.close();
// signallng to READER that its safe to read now.
condition.signal();
System.out.println("Writer waiting");
condition.await();
} else {
writer.println(EXIT_FLAG);
System.out.println("WRITER EXITING ");
writer.close();
// AS it was an exit flag so no need to wait, just
// signal the reader.
condition.signal();
}
}
} catch (Exception e) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!");
e.printStackTrace();
} finally {
fileLock.unlock();
// Delete the file, require in case if one wants to run demo
// again.
File file = new File(fileName);
file.delete();
try {
file.createNewFile();
} catch (Exception e) {
}
}
}
}
/**
* This thread will read from the file and inform the writer thread to write
* again. If it has not read the EXIT flag then it will go into wait stage
* and will wait for WRITER to signal that it safe to read now.
*/
public static class FileRead implements Runnable {
public void run() {
String data = null;
fileLock.lock();
try {
while (true) {
BufferedReader reader = new BufferedReader(new FileReader(fileName));
data = reader.readLine();
System.out.println("READ DATA - " + data);
reader.close();
if (data == null || !data.equals(EXIT_FLAG)) {
condition.signalAll();
System.out.println("Reader Waiting");
condition.await();
} else {
System.out.println("READER EXITING");
condition.signal();
break;
}www.2cto.com
}
} catch (Exception e) {
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!EXCEPTION!!!!!!!!!!!!!!!!!!!!!!!!");
e.printStackTrace();
} finally {
fileLock.unlock();
}
}
}
}
作者:沉默是金