Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java synchronized question

807580Sep 13 2010 — edited Sep 16 2010
If I am using any unsynchronized class, how do I make sure I handle synchronization externally? Is this good enough:
public class Test{

    private ArrayList al = new ArrayList();

    private synchronized void add (T t){
        al.add(t);
    }

    private synchronized void remove (T t){
        al.remove(t);
    }
....
....
....
}
I will make sure any add/delete in other methods will call these two adder deleter methods. But correct me if was wrong, this way only ensure 2 threads are adding and 2 threads are deleting. It does not handle that 1 thread is adding, and at the same time, 1 thread is deleting. How do I handle that?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 14 2010
Added on Sep 13 2010
7 comments
341 views