Skip to main content

Moving o java forward part 4- JSR 166y: concurrent Package

Posted by otaviojava on October 26, 2011 at 12:34 AM PDT

 

Was added to his package to facilitate applications which needs scalable processes.

A feature is atomic variable. Variable atomic means that it cannot be divided -  it's like the S.O. with any resource (Driver CD, USB) cannot be divided but needs be used in many processes.

 

            static class AtomicCounter { 

         private AtomicInteger counter = new AtomicInteger(0);

         public void increment() {


             System.out.println(" couting " + contador.incrementAndGet());
         }

         public void decrement() {
             System.out.println("decrementing " + contador.decrementAndGet());

         }

         public int value() {
             return c.get();
         }
     }

     static class Producer implements Runnable {

         private AtomicCounter atomicCounter;

         public Producer(AtomicCounter counter) {
             this.atomicCounter = counter;


         }

         @Override
         public void run() {
             while (true) {

                 try {
                     Thread.sleep(2000);
                 } catch (InterruptedException ex) {
                     ex.printStackTrace();
                 }
                 atomicCounter.increment();


             }
         }
     }

     static class Consumer implements Runnable {

         private AtomicCounter atomicCounter;

         public Consumer(AtomicCounter counter) {
             this.atomicCounter = counter;


         }

         @Override
         public void run() {
             while (true) {

                 try {
                     Thread.sleep(4000);
                 } catch (InterruptedException ex) {
                     ex.printStackTrace();
                 }
                 atomicCounter.decrement();

             }
         }
     }

     public static void main(String[] args) {

         AtomicCounter atomicCounter = new AtomicCounter();
         Thread consumer = new Thread(new Consumer(atomicCounter));
         Thread producer = new Thread(new Producer(atomicCounter));
         consumidor.start();
         produtor.start();
         while (true) {
         }

     }

Picture 1: Simple example with consumer and producer

 

It's possible to create one Object and this object will be an atomic variable for this uses the interface java.io.Serializable.

 

     public static void main(String[] arg) { 
         AtomicReference myObject = new AtomicReference<>();
         System.out.println(meubojeto.get());
         myObject.set(new MYObject("value"));
         System.out.println(meubojeto.get().getAttribute());
         AtomicReferenceArray users = new AtomicReferenceArray(10);
       
         users.getAndSet(1, new MYObject("Setando objeto 1"));

     }

Picture 2: create atomic variable

 

In this new version there are more current collections.

 

BlockingDeque fila = new LinkedBlockingDeque<>();

ConcurrentMap map = new ConcurrentHashMap();

ConcurrentNavigableMap maps = new ConcurrentSkipListMap<>();

Picture 3: current  Collections

 

 

In concurrent process and you would like to lock any resources for other Threads -  don't use or access during that time - for this, only use the java.util.concurrent.locks.Lock.

The Executor has the main objective of realizing parallel processes and on a big scale. Now it's possible to use three new interfaces(Executor, ExecutorService, ScheduledExecutorService), These interfaces execute in the Thread pool, so the JVM manages parallel processes. Another improvement is Fork/Join, it helps in a better way for multiple processes and how it can be divided in the letter part and the recursive way.

 

 

Conclusion:

 

This article discusses improvements in the concurrent in java 7, the concurrent package.