import com.google.common.collect.ConcurrentHashMultiset;
import java.util.concurrent.atomic.AtomicInteger;
public class ConcurrencyExample {
private static ConcurrentHashMultiset<String> wordCount = ConcurrentHashMultiset.create();
private static AtomicInteger totalCount = new AtomicInteger(0);
public static void main(String[] args) {
Thread t1 = new Thread(new WordCountTask("Hello World!"));
Thread t2 = new Thread(new WordCountTask("Hello Google Collect!"));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Word Count: " + wordCount);
System.out.println("Total Count: " + totalCount);
}
static class WordCountTask implements Runnable {
private String sentence;
public WordCountTask(String sentence) {
this.sentence = sentence;
}
@Override
public void run() {
String[] words = sentence.split(" ");
for (String word : words) {
wordCount.add(word);
totalCount.incrementAndGet();
}
}
}
}