/* CSE 373, Winter 2013, Marty Stepp This program reads many files in parallel and counts the total number of unique words across all files. It is an example of concurrently accessing a shared resource, in this case, a Set of words. original: Starting to read bible ... Done reading bible. Starting to read gettysburg ... Done reading gettysburg. Starting to read hamlet ... Done reading hamlet. Starting to read tomsawyer ... Done reading tomsawyer. There are 23207 unique words. Took 16616 ms. parallel: Starting to read bible ... Starting to read hamlet ... Starting to read gettysburg ... Starting to read tomsawyer ... Done reading hamlet. Done reading bible. Done reading tomsawyer. Done reading gettysburg. There are 23207 unique words. Took 11474 ms. */ import java.io.*; import java.util.*; public class WordCounts { public static void main(String[] args) throws FileNotFoundException, InterruptedException { Set words = new HashSet(); long startTime = System.currentTimeMillis(); String[] files = {"bible", "gettysburg", "hamlet", "tomsawyer"}; Thread[] threads = new Thread[files.length]; for (int i = 0; i < files.length; i++) { // readFile(file, words); threads[i] = new Thread(new FileReader(files[i], words)); threads[i].start(); } for (int i = 0; i < threads.length; i++) { threads[i].join(); } long endTime = System.currentTimeMillis(); long elapsed = endTime - startTime; System.out.println("There are " + words.size() + " unique words."); System.out.println("Took " + elapsed + " ms."); } public static void readFile(String file, Set words) throws FileNotFoundException { System.out.println("Starting to read " + file + " ..."); Scanner input = new Scanner(new File(file + ".txt")); while (input.hasNext()) { String word = input.next(); // must "synchronize" this block so that no more than one thread is trying // to modify the collection at a time synchronized (words) { words.add(word); } } System.out.println("Done reading " + file + "."); } }