Mouss Diouf

+ Follow
since Mar 25, 2017
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mouss Diouf

Hi Stephan van Hulst,

Thank you for the cow To be honest most of the time when I have code question, I post question on stackoverflow. But here I decided to post my question on coderanch and I do not regret because you guys have been patient with me and allowed me to find the answer on my own. I really like that.

Thank you and all coderanch community
Hi Piet,

That's a good exercise, I will try your code. For "You will see that the program again hangs, but no doubt you can explain that as well" thanks to you, I think yes I can explain that. The CB's barrier is never reached since the number of threads in the pool is smaller the CB's barrier count, that is why the CB is never released.

Hi Piet,

The answer to "What happens then" is the program hangs.
To be completely honest I have tested the code with some "System.out.println" before proposing an explanation. Anyway let's take the previous code and change just this part, like you proposed "the size of your ThreadPool. Suppose you set this size to one less than the number of tasks that you submit" :

And here is my explanation :
1. The 3 threads available in the pool are consumed to perform the 3 first submitted tasks. Since the task has been submitted 4 times, there is one submit that is waiting a thread to be available.
2. The 3 threads execute performTask and since the barrier of CyclicBarrier is equal to 3, the CyclicBarrier c1 and c2 are released and the 3 threads finish the task.
3. The 4th submit can now be handled because there are threads available. Only one thread is required. This thread execute removeAnimals() method and get stuck in c1.await(). Because c1 needs 3 threads to call c1.await() to be released. Like my previous answer, the program will not hang if number of submitted tasks is a multiple of 3.

Am I right SuperPiet?

PS : I have google-translated "Zeur", since you guided me to find myself the answer to my question, I prefer "Super" than "Zeur"  
Hi Piet,

The answers to your questions for me:

How many times do you call 'c1.await()'

4 times because I call the method performTask 4 times

what count are your CyclicBarriers constructed

count is equal to 3 for both c1 and c2

what about the size of your ThreadPool

the pool contains 4 available threads

According to the definition : The CyclicBarrier takes in its constructors a limit value, indicating the number of threads to wait for. As each thread finishes, it calls the await() method on the cyclic barrier. Once the specified number of threads have each called await(), the barrier is released and all threads can continue.

I think I got it, the answer to my question is here "Once the specified number of threads have each called await(), the barrier is released". Since the await() is called by 4 threads, the first three threads allow to release the barrier but the last (the 4th) thread calls await and waits for 2 other threads to call await() in order to be released, since these 2 other threads never come, the program hangs in await() method called by the 4th thread. If my understanding is true, if I call performTask 6 times (or any multiple of 3), it will not hang anymore => I have tested and that's indeed the case.

Do you confirm my understanding sir Piet?
Can you tell me why this code hangs?

Can you tell me why this is not a good way to write code (OCP studyguide page 9):


I am preparing OCA 8 certification with the book OCA Java SE 8 Programmer I Study Guide Exam 1Z0-808 and I would have a question concerning Compiler Enhancement on chapter 5 page 241. It is written "Java compiler automatically inserts a call to the no-argument constructor super() if the first statement is not a call to the parent constructor." For me it is incomplete, the complete would be "Java compiler automatically inserts a call to the no-argument constructor super() if the first statement is not a call to the parent constructor, or a call to this()" this() meaning any constructor in the class with or without parameters, am I write?