which of tha following is not a thread safe class?
a) ArrayList b)Vector c)HashTable d)None
Answer Posted / aravinda reddy
ArrayList is by default not thread safe, but we can make it
thread safe. Below is the example how we can make it thread
safe.
public class SynchronizeList {
public static void main(String[] args) {
ArrayList<String> al=new ArrayList<String>
();
al.add("1");
al.add("2");
al.add("3");
//we can make the synchronized
Collections.synchronizedList(al);
synchronized(al) {
Iterator i = al.iterator(); // Must
be in synchronized block
while (i.hasNext())
System.out.println(i.next());
}
}
}
Vector and HashTable are by default synchronized. These are
thread safe.
Hence the answer is ArrayList.
Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
What is the flag in java?
What is set and get methods in java?
What is the need of transient variables in Java ?
How can you make sure that your singleton class will always return single instance in multi-threaded environment?
What is the use of :: in java?
What is an 8 bit word?
What is meant by object?
Why do we use string?
What are the differences between throw and throws?
Can we sort list in java?
What is the biggest integer?
What is the return type of the main method?
Which is better stringbuilder or stringbuffer?
When will we prefer to use set and list in java and why?
In a program, initializing an array of 100 KB is throwing an out of memory exception while there is 100 MB of memory available. Why?