倒计时锁演示程序。不等待倒计时锁结束。

huangapple go评论95阅读模式
英文:

CountdownLatch Demo program .Not waiting for coutdown latch to get over

问题

import java.util.concurrent.CountDownLatch;

public class CDLDemo implements Runnable {

    static int count = 0;

    CountDownLatch cdl = new CountDownLatch(5);

    void checkForAwait() {
        cdl.countDown();
        System.out.println("Start " + cdl.getCount());
        CDLTask1 cdlTask1 = new CDLTask1(cdl);
        CDLTask2 cdlTask2 = new CDLTask2(cdl);
        CDLTask3 cdlTask3 = new CDLTask3(cdl);
        CDLTask4 cdlTask4 = new CDLTask4(cdl);

        Thread t1 = new Thread(cdlTask1);
        Thread t2 = new Thread(cdlTask2);
        Thread t3 = new Thread(cdlTask3);
        Thread t4 = new Thread(cdlTask4);

        t1.start();
        t2.start();
        t3.start();
        t4.start();
        try {
            cdl.await();
            System.out.println("All countdownlatch over");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        checkForAwait();
    }

    public static void main(String[] args) throws InterruptedException {
        CDLDemo cdlDemo = new CDLDemo();
        Thread t1 = new Thread(cdlDemo, "Thread1");
        t1.start();
        t1.join();
        System.out.println("All completed");
    }

    static int getCount() {
        return count++;
    }

    static class CDLTask1 implements Runnable {
        CountDownLatch cdl;

        public CDLTask1(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public void run() {
            getCount();
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + " " + Thread.currentThread().getName());
        }
    }

    static class CDLTask2 implements Runnable {
        CountDownLatch cdl;

        public CDLTask2(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public void run() {
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + " " + Thread.currentThread().getName());
        }
    }

    static class CDLTask3 implements Runnable {
        CountDownLatch cdl;

        public CDLTask3(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
       public void run() {
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + " " + Thread.currentThread().getName());
        }
    }

    static class CDLTask4 implements Runnable {
        CountDownLatch cdl;

        public CDLTask4(CountDownLatch cdl) {
            this.cdl = cdl;
        }

        @Override
        public void run() {
            cdl.countDown();
            System.out.println("Available count :" + cdl.getCount() + " " + Thread.currentThread().getName());
        }
    }
}
英文:

In this program why All countdownlatch over message printed in between .Although it should wait for all the countdown latch to get over .As an extra Thread is started in the main method but that should be handled as cdl.countDown() method is called to handle countdown for this thread .
Why its violating countdown latch ?

import java.util.concurrent.CountDownLatch;
public class CDLDemo implements Runnable {
static int count = 0;
CountDownLatch cdl = new CountDownLatch(5);
void checkForAwait() {
cdl.countDown();
System.out.println("Start " + cdl.getCount());
CDLTask1 cdlTask1 = new CDLTask1(cdl);
CDLTask2 cdlTask2 = new CDLTask2(cdl);
CDLTask3 cdlTask3 = new CDLTask3(cdl);
CDLTask4 cdlTask4 = new CDLTask4(cdl);
Thread t1 = new Thread(cdlTask1);
Thread t2 = new Thread(cdlTask2);
Thread t3 = new Thread(cdlTask3);
Thread t4 = new Thread(cdlTask4);
t1.start();
t2.start();
t3.start();
t4.start();
try {
cdl.await();
System.out.println("All countdownlatch over");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
checkForAwait();
}
public static void main(String[] args) throws InterruptedException {
CDLDemo cdlDemo = new CDLDemo();
Thread t1 = new Thread(cdlDemo, "Thread1");
t1.start();
t1.join();
System.out.println("All completed");
}
static int getCount() {
return count++;
}
static class CDLTask1 implements Runnable {
CountDownLatch cdl;
public CDLTask1(CountDownLatch cdl) {
this.cdl = cdl;
}
@Override
public void run() {
getCount();
cdl.countDown();
System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
}
}
static class CDLTask2 implements Runnable {
CountDownLatch cdl;
public CDLTask2(CountDownLatch cdl) {
this.cdl = cdl;
}
@Override
public void run() {
cdl.countDown();
System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
}
}
static class CDLTask3 implements Runnable {
CountDownLatch cdl;
public CDLTask3(CountDownLatch cdl) {
this.cdl = cdl;
}
@Override
public void run() {
cdl.countDown();
System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
}
}
static class CDLTask4 implements Runnable {
CountDownLatch cdl;
public CDLTask4(CountDownLatch cdl) {
this.cdl = cdl;
}
@Override
public void run() {
cdl.countDown();
System.out.println("Available count :" + cdl.getCount() + "" + Thread.currentThread().getName());
}
}
}

答案1

得分: 0

Sure, here's the translation:

CDLTask中,您的打印语句位于cdl.countDown()之后,这就是为什么您在其中间得到了该消息。在线程在倒数和打印语句之间被抢占时,您的主线程可能会在所有打印完成之前被标记。

英文:

Your print statement in CDLTask is after cdl.countDown() that why you get the message all over in between. The thread may be preempted between the count down and print statements then your main may be signaled before all the prints are done.

huangapple
  • 本文由 发表于 2020年8月14日 18:38:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63411163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定