Created
June 8, 2011 02:22
-
-
Save karanmalhi/1013661 to your computer and use it in GitHub Desktop.
Comparison between Semaphore and executor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.IOException; | |
import java.util.Enumeration; | |
import java.util.concurrent.ConcurrentLinkedQueue; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.Semaphore; | |
import java.util.concurrent.TimeUnit; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipException; | |
import java.util.zip.ZipFile; | |
public class JarProcessor { | |
public static void main(String[] args) { | |
// there are 4 jars in the directory , number of entries in jar = [12, 203, 1178, 1551] | |
File dir = new File("/home/karan/jars"); | |
// processWithSemaphore(dir); | |
processWithExecutor(dir); | |
} | |
private static void processWithSemaphore(File dir) { | |
final ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); | |
final int total = 4; | |
final Semaphore threads = new Semaphore(total); | |
try { | |
long ps = System.nanoTime(); | |
for (final File file : dir.listFiles()) { | |
if (!file.isFile()) | |
continue; | |
threads.acquire(); | |
// FORK | |
Thread runnable = new Thread() { | |
public void run() { | |
try { | |
// ++++++++++++++++++++++++++++++++++ | |
// do some heavy lifting with 'file' | |
// ++++++++++++++++++++++++++++++++++ | |
queue.add(new ComputeSize().compute(file)); | |
} catch (Exception e) { | |
System.out.println("FAILED Task " | |
+ file.getAbsolutePath()); | |
} finally { | |
threads.release(); | |
} | |
} | |
}; | |
runnable.setDaemon(true); | |
runnable.start(); | |
} | |
// JOIN | |
try { | |
threads.acquire(total); | |
} catch (InterruptedException e) { | |
Thread.interrupted(); | |
} | |
// DONE | |
System.out.println("FINISHED" + " " | |
+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - ps)); | |
System.out.println(queue); | |
} catch (InterruptedException e) { | |
Thread.interrupted(); | |
throw new RuntimeException("processing failed", e); | |
} | |
} | |
private static void processWithExecutor(File dir) { | |
final ConcurrentLinkedQueue<Integer> queue = new ConcurrentLinkedQueue<Integer>(); | |
ExecutorService exec = Executors.newFixedThreadPool(4); | |
try { | |
long ps = System.nanoTime(); | |
for (final File file : dir.listFiles()) { | |
if (!file.isFile()) | |
continue; | |
exec.execute(new Runnable() { | |
public void run() { | |
try { | |
// ++++++++++++++++++++++++++++++++++ | |
// do some heavy lifting with 'file' | |
// ++++++++++++++++++++++++++++++++++ | |
queue.add(new ComputeSize().compute(file)); | |
} catch (Exception e) { | |
System.out.println("FAILED Task " | |
+ file.getAbsolutePath()); | |
} | |
} | |
}); | |
} | |
exec.shutdown(); | |
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); | |
// DONE | |
System.out.println("FINISHED" + " " | |
+ TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - ps)); | |
System.out.println(queue); | |
} catch (InterruptedException e) { | |
Thread.interrupted(); | |
throw new RuntimeException("processing failed", e); | |
} | |
} | |
} | |
class ComputeSize{ | |
public int compute(File file) throws ZipException, IOException{ | |
int size = 0; | |
for (int i = 0; i < 1000; i++) { | |
ZipFile zipFile = new ZipFile(file); | |
Enumeration<? extends ZipEntry> entries = zipFile.entries(); | |
while (entries.hasMoreElements()) { | |
ZipEntry zipEntry = (ZipEntry) entries.nextElement(); | |
size++; | |
} | |
} | |
return size; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment