Skip to content

Instantly share code, notes, and snippets.

@VincentTatan
Created October 14, 2015 06:27
Show Gist options
  • Save VincentTatan/05e39b1adf690d23da01 to your computer and use it in GitHub Desktop.
Save VincentTatan/05e39b1adf690d23da01 to your computer and use it in GitHub Desktop.
package Thread;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by kevinsteppe on 2/10/15.
*/
public class FileTest {
public static final int numLoops = 200;
public static final int numComputations = 3000;
public static final String fileA = "src/Resources/fileA.txt";
public static final String fileB = "src/Resources/fileB.txt";
public static void main(String[] args) throws Exception {
Lock lock = new ReentrantLock();
Thread t1 = new Thread(new FileA(lock));
Thread t2 = new Thread(new FileB(lock));
final long startTime = System.currentTimeMillis();
t1.start();
// t1.join();
t2.start();
t2.join();
final long endTime = System.currentTimeMillis();
System.out.println("Total execution time: " + (endTime - startTime));
}
protected static void complicatedComputation(int i) {
BigInteger j = new BigInteger("2");
for (int z = 2; z < FileTest.numComputations; z++) {
j = j.multiply(new BigInteger(String.valueOf(i)));
}
}
}
class FileA implements Runnable {
Lock lock;
public FileA(Lock lock) {
this.lock = lock;
}
public void run() {
lock.lock();
PrintWriter writer = null;
try {
for (int i = 0; i < FileTest.numLoops / 5; i++) {
writer = new PrintWriter(new BufferedWriter(new FileWriter(FileTest.fileA, true)));
for (int j = 0; j < 5; j++) {
FileTest.complicatedComputation(i);
writer.println("The first file" + j);
}
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
lock.unlock();
}
}
class FileB implements Runnable {
Lock lock;
public FileB(Lock lock) {
this.lock = lock;
}
public void run() {
lock.lock();
PrintWriter writer = null;
try {
for (int i = 0; i < FileTest.numLoops / 5; i++) {
writer = new PrintWriter(new BufferedWriter(new FileWriter(FileTest.fileA, true)));
for (int j = 0; j < 5; j++) {
FileTest.complicatedComputation(i);
writer.println("The second file" + j);
}
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
}
lock.unlock();
}
}
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The first file0
The first file1
The first file2
The first file3
The first file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
The second file0
The second file1
The second file2
The second file3
The second file4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment