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 vector capacity in java?
What is the constructor?
What is thread count in java?
I want to control database connections in my program and want that only one thread should be able to make database connection at a time. How can I implement this logic?
What is method overloading in JAVA? Why is it not present in C ?
Give reasons supporting that string is immutable.
What is default exception handling in java?
What is runtime locatable code?
What are measurable parameters?
What is character in data type?
what is abstract class in Java?
What is a subsequence of a string?
What are the disadvantages of using inner classes?
How can you read an integer value from the keyword when the application is runtime in java? example?
What is java command?