Answer is A, tested
public static void main(String[] args) {
final List<String> list = new CopyOnWriteArrayList<>();
final AtomicInteger ai = new AtomicInteger(0);
final CyclicBarrier barrier = new CyclicBarrier(2, new Runnable() {
@Override
public void run() {
System.out.println(list);
}
});
Runnable r = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(1000 * ai.incrementAndGet());
list.add("X");
barrier.await();
}catch (Exception e){
}
}
};
new Thread(r).start();
new Thread(r).start();
new Thread(r).start();
new Thread(r).start();
}
output :
[X, X]
[X, X, X, X]
//CyclicBarrier works for all every 2 threads reached to the barrier
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
jduarte
Highly Voted 3 years, 6 months agosteefaand
Most Recent 5 months, 4 weeks agoMatthewTannous
2 years agoSvetleto13
3 years, 2 months agoSvetleto13
3 years, 2 months agoAbdullah_Rahahleah
3 years, 7 months agopul26
3 years, 7 months agoTarik2190
3 years, 2 months ago