[ Pobierz całość w formacie PDF ]
" 2 ways to synchronize: 1. Synchronize the entire method " Declare the method to be synchronized - very common practice. " Thread should obtain the object s lock. 2. Synchronize part of the method " Have to pass an arbitrary object which lock is to be obtained to execute the synchronized code block (part of a method). " We can specify this in place object, to obtain very brief locking not very common. " wait points to remember calling thread gives up CPU calling thread gives up the lock calling thread goes to monitor s waiting pool wait also has a version with timeout in milliseconds. Use this if you re not sure when the current thread will get notified, this avoids the thread being stuck in wait state forever. " notify points to remember one thread gets moved out of monitor s waiting pool to ready state notifyAll moves all the threads to ready state Thread gets to execute must re-acquire the lock of the monitor before it can proceed. " Note the differences between blocked and waiting. Blocked Waiting Thread is waiting to get a lock on the monitor. Thread has been asked to wait. (by means of (or waiting for a blocking i/o method) wait method) Caused by the thread tried to execute some The thread already acquired the lock and synchronized code. (or a blocking i/o method) executed some synchronized code before coming across a wait call. Can move to ready only when the lock is Can move to ready only when it gets notified available. ( or the i/o operation is complete) (by means of notify or notifyAll) " Points for complex models: 1. Always check monitor s state in a while loop, rather than in an if statement. 2. Always call notifyAll, instead of notify. " Class locks control the static methods. " wait and sleep must be enclosed in a try/catch for InterruptedException. " A single thread can obtain multiple locks on multiple objects (or on the same object) " A thread owning the lock of an object can call other synchronous methods on the same object. (this is another lock) Other threads can t do that. They should wait to get the lock. " Non-synchronous methods can be called at any time by any thread. " Synchronous methods are re-entrant. So they can be called recursively. " Synchronized methods can be overrided to be non-synchronous. synchronized behavior affects only the original class. " Locks on inner/outer objects are independent. Getting a lock on outer object doesn t mean getting the lock on an inner object as well, that lock should be obtained separately. " wait and notify should be called from synchronized code. This ensures that while calling these methods the thread always has the lock on the object. If you have wait/notify in non-synchronized code 33 compiler won t catch this. At runtime, if the thread doesn t have the lock while calling these methods, an IllegalMonitorStateException is thrown. " Deadlocks can occur easily. e.g, Thread A locked Object A and waiting to get a lock on Object B, but Thread B locked Object B and waiting to get a lock on Object A. They ll be in this state forever. " It s the programmer s responsibility to avoid the deadlock. Always get the locks in the same order. " While suspended , the thread keeps the locks it obtained so suspend is deprecated in 1.2 " Use of stop is also deprecated, instead use a flag in run method. Compiler won t warn you, if you have statements after a call to stop, even though they are not reachable. 34 Chapter 8 java.lang package " Object class is the ultimate ancestor of all classes. If there is no extends clause, compiler inserts extends object . The following methods are defined in Object class. All methods are public, if not specified otherwise. Method Description Boolean equals(Object o) just does a == comparison, override in descendents to provide meaningful comparison final native void wait() Thread control. Two other versions of wait() accept timeout parameters and may throw final native void notify() InterruptedException. final native void notifyAll() native int hashcode() Returns a hash code value for the object. If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result. protected Object clone() throws Creates a new object of the same class as this object. It then initializes each of the new CloneNotSupportedException object's fields by assigning it the same value as the corresponding field in this object. No constructor is called. CloneNotSupportedException The clone method of class Object will only clone an object whose class indicates that it is is a checked Exception willing for its instances to be cloned. A class indicates that its instances can be cloned by declaring that it implements the Cloneable interface. Also the method has to be made public to be called from outside the class. Arrays have a public clone method. int ia[ ][ ] = { { 1 , 2}, null }; int ja[ ][ ] = (int[ ] [ ])ia.clone(); A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared, so ia and ja are different but ia[0] and ja[0] are same. final native Class getClass() Returns the runtime class of an object. String toString Returns the string representation of the object. Method in Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. Override to provide useful information. protected void finalize() throws Called by the garbage collector on an object when garbage collection determines that there Throwable are no more references to the object. Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored. The finalize method in Object does nothing. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup. " Math class is final, cannot be sub-classed. " Math constructor is private, cannot be instantiated. " All constants and methods are public and static, just access using class name. " Two constants PI and E are specified. " Methods that implement Trigonometry functions are native. " All Math trig functions take angle input in radians. Angle degrees * PI / 180 = Angle radians " Order of floating/double values: -Infinity --> Negative Numbers/Fractions --> -0.0 --> +0.0 --> Positive Numbers/Fractions --> Infinity " abs int, long, float, double versions available
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plnatalcia94.xlx.pl
|
|
IndeksSUN Solaris Network Security (1997) [SA388]A Witches' BibleD. HUME BADANIA DOTYCZćÂCE ROZUMU LUDZKIEGOGodeng Gert Krew i Wino 08 Lilie z JeruzalemCassJackson Lilian Braun Kot, który...18 Kot, który lubiśÂ seryChristie Agatha Pora przyplywuDeaver Jeffery Manuskrypt ChopinaDalton Tymber Triple Trouble 01 ( 18)56655233 My Everything
zanotowane.pldoc.pisz.plpdf.pisz.plown-team.pev.pl
Cytat
Długi język ma krótkie nogi. Krzysztof Mętrak Historia kroczy dziwnymi grogami. Grecy uczyli się od Trojan, uciekinierzy z Troi założyli Rzym, a Rzymianie podbili Grecję, po to jednak, by przejąć jej kulturę. Erik Durschmied A cruce salus - z krzyża (pochodzi) zbawienie. A ten zwycięzcą, kto drugim da / Najwięcej światła od siebie! Adam Asnyk, Dzisiejszym idealistom Ja błędy popełniam nieustannie, ale uważam, że to jest nieuniknione i nie ma co się wobec tego napinać i kontrolować, bo przestanę być normalnym człowiekiem i ze spontanicznej osoby zmienię się w poprawną nauczycielkę. Jeżeli mam uczyć dalej, to pod warunkiem, że będę sobą, ze swoimi wszystkimi głupotami i mądrościami, wadami i zaletami. s. 87 Zofia Kucówna - Zdarzenia potoczne |
|