Using AtomicInteger as counter for List [closed]

I am learning multi threading. I have below code which implements round robin algorithm over an CopyOnWriteArrayList. List will always have a fixed number of elements let’s suppose 10.

    AtomicInteger counter = new AtomicInteger(0);
    Lock lock = new ReentrantLock(true);
    public String get(CopyOnWriteArrayList<String> fruits) {
        String next;
        try {
            lock.lock();
            counter.compareAndSet(fruits.size(), 0);
            next = fruits.get(counter.getAndIncrement());
        }finally {
            lock.unlock();
        }
        return next;
    }

Note: List only contains 10 elements not more than that ever.
I am thinking using AtomicInteger is useless here as I am already locking critical section.
Is there a way to use Atomicnteger and not lock. I am using lock as below two lines can generate race conditions.
counter.compareAndSet(fruits.size(), 0);
fruits.get(counter.getAndIncrement());

Trying to create thread safe counter that will be used to get element from List.
I need rotation here as if the counter rises to 10 than it will be reverted back to 0 to implement round robin.

New contributor

Ahmed Bilal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

I am thinking using AtomicInteger is useless here as I am already
locking critical section.

If counter is accessed only while the lock is locked then that is correct. An AtomicInteger provides no advantage over a random other mutable int wrapper under those circumstances.

Is there a way to use Atomicnteger and not
lock. I am using lock as below two lines can generate race condition.
counter.compareAndSet(fruits.size(), 0);
fruits.get(counter.getAndIncrement());

More specifically, the issue is that fruits.size(), fruits.get(), counter.compareAndSet() and counter.getAndIncrement() all need to be coherent. That is, to avoid the possibility of the get() failing with an IndexOutOfBoundsException despite the list being non-empty, you need the size reported by fruits.size() not to be larger than the actual size at the time of the fruits.get(), and you need the value of counter after the compareAndSet() to be the same one observed by the getAndIncrement().

Trying to create thread safe counter that will be used to get element
from List.

An AtomicInteger is thread-safe. So is a CopyOnWriteArrayList. The issue here is that that is not enough. Thus, you have mischaracterized the issue. It is not that you need a thread-safe counter, but rather that you need a thread-safe iterative get() operation.

You could conceivably use the list’s iterator instead of retrieving by index. However, that will iterate over a snapshot of the list as of when the iterator was created, and it will not automatically wrap around, both of which distinguish this approach from your idea.

You could conceivably skip the locking and instead catch and recover from an IndexOutOfBoundsException when one occurs. This is harder than it may sound, and ugly, and left as an exercise.

You have not disclosed the context for this, so that we could decide what alternatives would best serve the purpose, but my best guess would be that you do want to use a lock, and that you probably would be fine with an ordinary int counter. In that case, you may also need to protect mutations of the list with the same lock, at which point, you would not be getting much advantage from using CopyOnWriteArrayList in place of a plain ArrayList.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật