Posts

Showing posts from September, 2017

Java Program for CountDownLatch

package com.java.thread.concurrent;importjava.util.concurrent.CountDownLatch;importjava.util.concurrent.ExecutorService;importjava.util.concurrent.Executors;classProcessorimplements Runnable {private CountDownLatch latch; Processor(CountDownLatch latch){this.latch= latch;}@Overridepublicvoidrun(){ System.out.println("Started.");try{ Thread.sleep(3000);}catch(InterruptedException e){ e.printStackTrace();} latch.countDown(); System.out.println("Completed.");}}publicclassCountDownThread{publicstaticvoidmain(String[] args){ CountDownLatch downLatch =new CountDownLatch(3); ExecutorService executorService=Executors.newFixedThreadPool(3);for(int a=0; a<3; a++){ executorService.submit(new Processor(downLatch));}try{ downLatch.await();}catch(InterruptedException e){ e.printStackTrace();} System.out.println("Finished.&quo…

Java program for Fixed Thread Pool

package com.java.thread.executor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Processor implements Runnable {

    private int id;

    public Processor(int id) {
        super();
        this.id = id;
    }

    @Override
    public void run() {
        System.out.println("Starting.... " + id + "by thread " + Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Completed.... " + id +  "by thread " + Thread.currentThread().getName());
    }

}

public class ProcessorExecutor {

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);

        for(int a=0; a<5; a++){
            service.submit(new Processor(a));
        }

        service.shutdown();

        System.out.print…

Java program to find duplicate element in array

package com.java;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;

public class FindDuplicateElementInArray {

    public static void main(String[] args) {

        int[] intArr = new int[] { 1, 2, 1, 2, 1, 3, 4, 6, 2, 8 };

        Map<Integer, Integer> map = new LinkedHashMap<Integer, Integer>();

        for (int i = 0; i < intArr.length; i++) {
            // take first element and then matched complete array
            int temp = intArr[i];
            int count = 0;

            for (int j = 0; j < intArr.length; j++) {
                if (temp == intArr[j]) {
                    // element matched -- break
                    count++;
                }
            }
            map.put(temp, count);
        }

        Set<Integer> duplicate = new LinkedHashSet<Integer>();
        Set<Integer> noDuplicate = new LinkedHashSet<Integer>();

        for (int i = 0…

BigInteger to String conversion problem in Java

Issue when converting the BigInteger to String.

Above code was creating an issue as , when the value of macObject.getMACHINECODE() is coming as null,
Then valueOf() method of String was converting its value as null String as "null".

So when machinecode variable is being checked for null, it is getting passed through as the converted value,
was String.

And code was not working as expected.


String machinecode = String.valueOf(macObject.getMACHINECODE());
if (machinecode != null) {
// some code
}

So the solution is as below,

BigInteger machineCode = macObject.getMACHINECODE());
if(machineCode != null){
machineCode .toString();
// some code
}