英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论