Tuesday, December 10, 2019
Statement The java.util.Vector
Question: Describe about the Statement the java.util.Vector class should not be used anymore? Answer: I agree with the statement that java.util.Vector class should not be used anymore. The Vector class can be replaced by the ArrayList class. Although Vector class andArrayList classare very similar in nature and both these classes represent an array that can grow dynamically. The Vector class has been in existence since the first version of JDK and the ArrayList class was introduced as a part of the collections framework in the Java 2 Standard Edition 1.2 (JDK 1.2). You can discover the accompanying javadoc remark for the Vector class: "As of the Java 2 stage v1.2, this class was retrofitted to actualize the List interface, making it an individual from the Java Collections Framework. Not at all like the new gathering executions, Vector is synchronized. In the event that a string safe usage is not required, it is prescribed to utilize ArrayList as a part of spot of Vector." The main difference between the two is that the all methods of the Vector class are synchronized, while methods of the ArrayList class are not synchronized, this means that the Vector class is thread-safe whereas the ArrayList class is not thread-safe. By synchronization we mean the ability to control the access of multiple threads to a common resource. Synchronization is based on an internal entity known as the lock or monitor. There is a lock associated with every object. A thread that needs consistent access to an object has to acquire a lock on the object before it can proceed, after getting the lock it can perform the tasks on the object and when it is finished with the task it releases the object and the object is free now. If a thread T2 asks for a lock while some other thread T1 has already taken the lock then the thread T2 will have to wait till the thread T1 releases the object. In simple terms synchronization means no two threads can simultaneously access the block/method of a class. The thread which approaches the object first for a lock takes the lock and till this lock is not released no other thread can access any of these synchronized block/methods. Vectorssynchronize on every individual operation. But this thing is something that you would never want to do. This activity hits the performance of the program very badly. Generally one would want to synchronize acomplete sequenceof operations rather than each individual operation. Synchronizing individual operations is not only less safe but slower also, why should we take a lock again and again when it is enough to take out a lock for the whole sequence of operation once. This approach has the overhead of locking when we do not require it. Therefore this approach to synchronization is flawed in most of the situations. The vector class joins the "resized show" gathering execution together with the "synchronize each operation", this is an alternate case of poor plan and does not give an acceptable division of concern. ArrayList which was introduced as a part of the collection framework should be preferred over the vector. ArrayList are by default not synchronized thus when we do not require the synchronization aspect it saves the overhead cost of synchronization. But when we have to consider the synchronization aspect, then we can use the methods in java.util.Collections. The Collections.synchronizedList() method can be used for achieving synchronization in the ArrayList. ArrayList can completely take place of the Vector and if more efficient. To put things straight we can: 1. Make use of the ArrayList if we do not require any bit of synchronization. 2. We can use the Collections.synchronizedList if we have to consider the synchronization aspect of the array. 3. We can use the CopyOnWriteArrayList if we have very few write operations and a significant no of read operations. But this approach creates an entirely new array on a write/add operation and hence it can be slow. 4. At last we can use the native Array if performance is a concern and synchronization is not required. One more difference between the Vector and the ArrayList is resizing. Both the objects hold the content using the native Array. Therefore when the Array is completely full and a new element is to be inserted then the Array is resized. The vector by default doubles the size of its Array, while the ArrayList increases the size of its Array by 50%.This could take a toll on the performance while adding new elements when the Array is full. We can show the difference between the two classes by comparing the performance of the two classes on same set of data and operations. In the program we take an ArrayList and a Vector and add elements to both and compare the time taken on both the objects. We also iterate over both the objects and find the difference in time of both. We find that the time taken by Vector is much more than that taken by the ArrayList. Code: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package javaapplication3; import java.util.*; import java.util.concurrent.TimeUnit; /** * * */ public class ComparePerformance { private static final int SIZE = 1000000; private static final int RUNS = 500; private static final Integer ONE = Integer.valueOf(1); static boolean var = false; static class Run { private final ListInteger list; Run(final ListInteger list) { this.list = list; } public long addElem() { long startTime = System.nanoTime(); for (int i = 0; i SIZE; i++) { list.add(ONE); } return System.nanoTime() - startTime; } public long iterateElem() { long startTime = System.nanoTime(); for (int i = 0; i SIZE; i++) { var=list.isEmpty(); } return System.nanoTime() - startTime; } } public static void main(final String[] args) { long arrayListTotalAdd = 0L; long vectorTotalAdd = 0L; long arrayListTotalIterate = 0L; long vectorTotalIterate = 0L; for (int i = 0; i RUNS; i++) { if (i % 50 == 49) { System.out.println("Run " + (i + 1)); } arrayListTotalAdd += new Run(new ArrayListInteger()).addElem(); arrayListTotalIterate += new Run(new ArrayListInteger()).iterateElem(); vectorTotalAdd += new Run(new VectorInteger()).addElem(); vectorTotalIterate += new Run(new VectorInteger()).iterateElem(); } System.out.println(); System.out.println("Runs: "+RUNS+", list size: "+SIZE); output(arrayListTotalAdd, "List add"); output(vectorTotalAdd, "Vector add"); output(arrayListTotalIterate, "List iterate"); output(vectorTotalIterate, "Vector iterate"); } private static void output(final long value, final String name) { System.out.println(name + " total time: " + value + " (" + TimeUnit.NANOSECONDS.toMillis(value) + " " + "ms)"); long avg = value / RUNS; System.out.println(name + " average time: " + avg + " (" + TimeUnit.NANOSECONDS.toMillis(avg) + " " + "ms)"); } } References Bailey, D., Bailey, D. (2000).Java elements. Boston: McGraw-Hill. Cahoon, B., McKinley, K. (2005). Recurrence analysis for effective array prefetching in Java.Concurrency Computat.: Pract. Exper.,17(5-6), 589-616. doi:10.1002/cpe.851 Jacobson, N., Thornton, A. (2004). It is time to emphasize arraylists over arrays in Java-based first programming courses.ACM SIGCSE Bulletin,36(4), 88. doi:10.1145/1041624.1041666 Sarang, P. (2012).Java programming. New York: McGraw-Hill.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.