Index
Pacynski Tomasz Maskarada
wiedza i zycie2
Testament matarese'a
306 33 (3)
Forsyth Weteran
19 (102)
Sandemo Margit Saga O Ludziach Lodu
Verne Juliusz Robur zdobywca
Pod redakcją Adama Bilikiewicza Psychiatria (dla studentów medycyny) (2)
Opcja Paryska
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • bydgoszczanin.xlx.pl

  • [ Pobierz całość w formacie PDF ]
    .synchronizedCollection(new ArrayList());List list = Collections.synchronizedList(new ArrayList());Set s = Collections.synchronizedSet(new HashSet());Map m = Collections.synchronizedMap(new HashMap());}} ///:~In this case, you immediately pass thenew container through the appropriate “synchronized” method; thatway there’s no chance of accidentally exposing the unsynchronized version.[ Add Comment ]Fail fastThe Java containers also have a mechanismto prevent more than one process from modifying the contents of a container.Theproblem occurs if you’re iterating through a container and some otherprocess steps in and inserts, removes, or changes an object in that container.Maybe you’ve already passed that object, maybe it’s ahead of you,maybe the size of the container shrinks after you callsize( )—there are many scenarios for disaster.The Javacontainers library incorporates a fail-fast mechanism that looks for anychanges to the container other than the ones your process is personallyresponsible for.If it detects that someone else is modifying the container, itimmediately produces a ConcurrentModificationException.This is the“fail-fast” aspect—it doesn’t try to detect a problemlater on using a more complex algorithm.[ Add Comment ]It’s quite easy to see thefail-fast mechanism in operation—all you have to do is create an iteratorand then add something to the collection that the iterator is pointing to, likethis://: c09:FailFast.java// Demonstrates the "fail fast" behavior.import java.util.*;public class FailFast {public static void main(String[] args) {Collection c = new ArrayList();Iterator it = c.iterator();c.add("An object");// Causes an exception:String s = (String)it.next();}} ///:~The exception happens because somethingis placed in the container after the iterator is acquired from thecontainer.The possibility that two parts of the program could be modifying thesame container produces an uncertain state, so the exception notifies you thatyou should change your code—in this case, acquire the iteratorafter you have added all the elements to the container.[ Add Comment ]Note that you cannot benefit from thiskind of monitoring when you’re accessing the elements of a Listusing get( ).[ Add Comment ]UnsupportedoperationsIt’s possible to turn an array intoa List with the Arrays.asList( ) method://: c09:Unsupported.java// Sometimes methods defined in the// Collection interfaces don't work!import java.util.*;public class Unsupported {private static String[] s = {"one", "two", "three", "four", "five","six", "seven", "eight", "nine", "ten",};static List a = Arrays.asList(s);static List a2 = a.subList(3, 6);public static void main(String[] args) {System.out.println(a);System.out.println(a2);System.out.println("a.contains(" + s[0] + ") = " +a.contains(s[0]));System.out.println("a.containsAll(a2) = " +a.containsAll(a2));System.out.println("a.isEmpty() = " +a.isEmpty());System.out.println("a.indexOf(" + s[5] + ") = " +a.indexOf(s[5]));// Traverse backwards:ListIterator lit = a.listIterator(a.size());while(lit.hasPrevious())System.out.print(lit.previous() + " ");System.out.println();// Set the elements to different values:for(int i = 0; i < a.size(); i++)a.set(i, "47");System.out.println(a);// Compiles, but won't run:lit.add("X"); // Unsupported operationa.clear(); // Unsupporteda.add("eleven"); // Unsupporteda.addAll(a2); // Unsupporteda.retainAll(a2); // Unsupporteda.remove(s[0]); // Unsupporteda.removeAll(a2); // Unsupported}} ///:~You’ll discover that only a portionof the Collection and List interfaces are actually implemented.The rest of the methods cause the unwelcome appearance of something called anUnsupportedOperationException.You’ll learn all about exceptions inthe next chapter, but the short story is that the Collectioninterface—as well as some of the other interfaces in theJava containers library—contain “optional” methods, whichmight or might not be “supported” in the concrete class thatimplements that interface.Calling an unsupported method causes anUnsupportedOperationException to indicate a programming error.[ Add Comment ]“What?!?” you say,incredulous.“The whole point of interfaces and base classes isthat they promise these methods will do something meaningful! This breaks thatpromise—it says that not only will calling some methods not performa meaningful behavior, they will stop the program! Type safety was just thrownout the window!”[ Add Comment ]It’s not quite that bad [ Pobierz całość w formacie PDF ]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • aceton.keep.pl
  • 
    Wszelkie Prawa Zastrzeżone! Kawa była słaba i bez smaku. Nie miała treści, a jedynie formę. Design by SZABLONY.maniak.pl.