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


Please Help Members By Posting Answers For Below Questions

What is the flag in java?

791


What is set and get methods in java?

733


What is the need of transient variables in Java ?

799


How can you make sure that your singleton class will always return single instance in multi-threaded environment?

776


What is the use of :: in java?

764


What is an 8 bit word?

796


What is meant by object?

777


Why do we use string?

773


What are the differences between throw and throws?

812


Can we sort list in java?

781


What is the biggest integer?

799


What is the return type of the main method?

783


Which is better stringbuilder or stringbuffer?

702


When will we prefer to use set and list in java and why?

708


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?

761