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 do you mean by multithreaded program?

557


What is the use of protected in java?

536


What causes memory leak in java?

484


What is logical variable?

516


What is meant by stack and queue?

608






What is string substring?

563


Is list ordered in java?

545


Can a lock be acquired on a class in java programming?

536


What is the difference between access specifiers and access modifiers in java?

578


Explain garbage collection in java?

549


Which is the best sorting technique in java?

529


What does it mean that strings are immutable?

578


When is the finalize() called?

704


What does snprintf return?

557


What access modifiers can be used for methods?

563