public static void main(String[] args) throws InterruptedException { System.setOut( new PrintStream( new OutputStream() { @Override public void write(int b) throws IOException {} })); long start = System.nanoTime(); List<Thread> threads = new ArrayList<>(); ThreadSafeFibonacciAdder adder = new ThreadSafeFibonacciAdder(); for (int i = 0; i < 4; ++i) { Thread t = new Thread(new RunnableImpl(adder)); t.start(); threads.add(t); } for (Thread t : threads) { t.join(); } System.err.println(adder.getSum()); System.err.println("Execution time: " + (System.nanoTime() - start) / 1e9); }
public void run() { for (int i = 0; i < 10_000_000; ++i) { adder.add(i % 50); } }