Deadlock occurs when two or more threads are blocked while waiting for each other. For example, the first thread is blocked on the second thread, waiting for a resource that the second thread holds. The second thread does not release this resource until it acquires a resource held by the first thread.
A few months ago, developerWorks posted some excerpts from my book, Practical Java, published by Addison-Wesley. I will initially use this column on developerWorks to answer some questions asked by readers and to respond to various comments about the excerpts.
The finally keyword is the best addition to the Java exception handling model over other language's models. The finally construct enables code to execute whether or not an exception occurred. Using finally is good to maintain the internal state of an object and to clean up non-memory resources. Without finally, your code is more convoluted. For example, the following is how you must write code to free non-memory resources without the benefit of finally:
A common misconception exists that parameters in Java are passed by reference. They are not. Parameters are passed by value. The misconception arises from the fact that all object variables are object references. This leads to some unexpected results if you do not understand exactly what is happening.
When frequently accessing variables, you need to consider from where they are accessed. Is the variable static , on the stack, or an instance variable of a class? Where you store a variable has a significant impact on the performance of code that accesses it.
When variables are shared between threads, they must always be accessed properly in order to ensure that correct and valid values are manipulated. The JVM is guaranteed to treat reads and writes of data of 32 bits or less as atomic. This might lead some programmers to believe that access to shared variables does not need to be synchronized or the variables declared volatile.
The synchronized keyword locks objects. Because the object is locked inside of synchronized code, what does that mean to the object and to changes you make to its object reference? Synchronizing on an object locks only the object. You must be careful, however, not to reassign an object reference of a locked object. What happens if you do?