Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Runnable in CompletableFuture multiple time

I want to execute multiple threads which will try to add concurrently to my custom list MyList , but I do not see any output when i try to get count

    public static void main(String[] args) {

        MyList<String> list = new list<String>();

        MyRunner<String> myRunner = new MyRunner<String>(list);

        ExecutorService threadPool = Executors.newFixedThreadPool(4);

        for(int i = 0; i < 20; i++) {
            CompletableFuture.runAsync(new MyRunner<String>(list));
        }

        try {
            threadPool.awaitTermination(100l, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(list.getCount());
}

Runner class :

class MyRunner<String> implements Runnable {

    MyList<String> list;

    public MyRunner(MyList <String> t) {
        this.list = t;
    }

    @Override
    public void run() {
        for(int i = 0; i < 200; i++) {
            list((String) (i + Thread.currentThread().getName()));
        }

    }
}

class MyList :

public class MyList<T> {

    Queue<T> blockingQueue;
    Lock lock;

    long count;

    public MyList() {
        blockingQueue = new LinkedList<>();
        count = 0;
        lock = new ReentrantLock();
    }

    public void add(T singleTon) {
        lock.lock();
        blockingQueue.offer(singleTon);
        count +=1;
        lock.unlock();
    }

    public long getCount() {
        return count;
    }


}

Follow up question :

using CountDownLatch the program is not ending. the number of sysout is 10001 and last output being In runnable: 9 : pool-1-thread-1

CountDownLatch implementation :

public static void main(String[] args) throws InterruptedException {

        MyList<String> mylist = new MyList<>();

        CountDownLatch latch = new CountDownLatch(10);

        ExecutorService executorService = Executors.newFixedThreadPool(4);


        for(int i = 0; i < 1000; i++) {
            CompletableFuture.runAsync(new MyRunner<String>(mylist, latch), executorService);
        }

        latch.await();

        System.out.println(mylist.count);
    }


class MyRunner<String> implements Runnable {

    MyList<String> mylist;
    CountDownLatch latch;

    public MyRunner(MyList<String> mylist, CountDownLatch latch) {
       this.latch = latch;
       this.mylist = mylist;
    }

    @Override
    public void run() {
        for(int i = 0; i < 10; i++) {
            System.out.println("In runnable: "+ i + " : "+ Thread.currentThread().getName());
            mylist.add((String)("" + i));
        }

        latch.countDown();
    }
}
like image 207
Novice User Avatar asked Mar 05 '23 14:03

Novice User


1 Answers

You invoke CompletableFuture.runAsync(Runnable runnable) that doesn't use the Executor that you created. Use instead CompletableFuture.runAsync(Runnable runnable, Executor executor) by passing your Executor instance such as :

CompletableFuture.runAsync(new MyRunner<String>(list), threadPool);
like image 109
davidxxx Avatar answered Mar 09 '23 00:03

davidxxx