Created
March 15, 2015 16:12
-
-
Save madhub/ddf5abadc4b6a061e249 to your computer and use it in GitHub Desktop.
Jdk8 AdderTest
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
/** | |
* Created by madhub_2 on 3/15/2015. | |
*/ | |
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package javaapplication1; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.TimeUnit; | |
import java.util.concurrent.atomic.AtomicLong; | |
import java.util.concurrent.atomic.LongAdder; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* | |
* @author madhub_2 | |
*/ | |
class DemoClass { | |
public static void main(String[] args) { | |
// Utilize a DoubleAdder or LongAdder to contain the value that is being summed across multiple threads in order | |
// to ensure safe handling | |
ExecutorService es = Executors.newFixedThreadPool(5); | |
LongAdder sumOfAllValues = new LongAdder(); | |
AtomicLong sum = new AtomicLong(); | |
CountDownLatch startSignal = new CountDownLatch(5); | |
es.submit( () -> { | |
try { | |
startSignal.await(); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(DemoClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
for( int i =0; i < 10;i++) { | |
sumOfAllValues.add(i); | |
sum.addAndGet(i); | |
} | |
}); | |
es.submit( () -> { | |
try { | |
System.out.println("Thread-"+Thread.currentThread().getId()+" waiting"); | |
startSignal.await(); | |
System.out.println("Thread-"+Thread.currentThread().getId()+" started"); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(DemoClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
for( int i =0; i < 10;i++) { | |
sumOfAllValues.add(i); | |
sum.addAndGet(i); | |
} | |
}); | |
es.submit( () -> { | |
try { | |
System.out.println("Thread-"+Thread.currentThread().getId()+" waiting"); | |
startSignal.await(); | |
System.out.println("Thread-"+Thread.currentThread().getId()+" started"); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(DemoClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
for( int i =0; i < 10;i++) { | |
sumOfAllValues.add(i); | |
sum.addAndGet(i); | |
} | |
}); | |
es.submit( () -> { | |
try { | |
System.out.println("Thread-"+Thread.currentThread().getId()+" waiting"); | |
startSignal.await(); | |
System.out.println("Thread-"+Thread.currentThread().getId()+" started"); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(DemoClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
for( int i =0; i < 10;i++) { | |
sumOfAllValues.add(i); | |
sum.addAndGet(i); | |
} | |
}); | |
es.submit( () -> { | |
try { | |
System.out.println("Thread-"+Thread.currentThread().getId()+" waiting"); | |
startSignal.await(); | |
System.out.println("Thread-"+Thread.currentThread().getId()+" started"); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(DemoClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
for( int i =0; i < 10;i++) { | |
sumOfAllValues.add(i); | |
sum.addAndGet(i); | |
} | |
}); | |
while ( startSignal.getCount() != 0 ) { | |
startSignal.countDown(); | |
} | |
es.shutdown(); | |
try { | |
es.awaitTermination(1, TimeUnit.DAYS); | |
} catch (InterruptedException ex) { | |
Logger.getLogger(DemoClass.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
System.out.println("Done "+sumOfAllValues.longValue()); | |
System.out.println("Done "+sum.longValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment