{{theTime}}

Search This Blog

Total Pageviews

Showing posts with label Java Threads. Show all posts
Showing posts with label Java Threads. Show all posts

Multi Thread Example using Execution Service

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import static java.util.Arrays.asList ;


public class ExecutorServiceMultiThreadSubstraction
{    
    static class Substract implements Callable<Long> {
        private long b;
        private long a;
        Substract(long a, long b){
            this.a = a ;
            this.b = b ;
        }
        @Override
        public Long call() throws Exception
        {
            return a-b;
        }
        
        public static void main(String str[]) throws Exception{
            ExecutorService executor = Executors.newFixedThreadPool(5) ;
            List<Future<Long>> results = executor.invokeAll(asList(new Substract(0,10), new Substract(11,13), new Substract(14,15) )) ;            
            executor.shutdown() ;            
            for(Future<Long> result: results)
                System.out.println(result.get()) ;
        }
    }
}

Thread safe Singleton Design Pattern Java Implementation

public class SingletonClass{
private static SingletonClass instance = null;
private SingletonClass() { }
public static synchronized SingletonClass getInstance() {
if (instance == null) {
instance = new SingletonClass();
}
return instance;
}
}

What is CountDownLatch

CountDownLatch is a versatile synchronization tool.


- It Allows one or more threads to wait until a work is done in other threads
- Used as on/off latch or gate
- Threads invoking await() method waits at the gate
- The count cannot be reset
- TO reset the count, you may check with CyclicBarrier
- Useful property is that it doesn't require that threads calling countDown wait for the count to reach zero before proceeding

Optimizing Java Applications for Low-Latency Microservices

Introduction Microservices architecture has become a go-to for building scalable, modular systems, but achieving low latency in Java-based...