Consider the following piece of code:
public class Test
{
private List<Object> list;
public Object get(int id)
{
return list.get(id);
}
public void add(Object el)
{
list.add(el);
}
public List<Object> getList()
{
return list;
}
}
I have to make sure that it is thread safe (there will be no synchronization errors). I am pretty new to this thing, so my guesses are:
We add
synchronizedto theadd()andget()methods and we addsynchronized(Test.class)to thegetList()method;We make
liststatic and all the methods static, then we addsynchronized;
This is what I came up with, it's possible that neither of them are right though.