Posts

Showing posts from November, 2007

How To Avoid Deadlock In java

How to avoid deadlocks One of the best ways to prevent the potential for deadlock is to avoid acquiring more than one lock at a time, which is often practical. However, if you have to acquire more then one lock for some purpose then acquire multiple locks in a consistent, defined order. Depending on how your program uses locks, it might not be complicated to ensure that you use a consistent locking order. You can define a lock acquisition ordering on the set of locks and ensure that you always acquire locks in that order. Once the lock order is defined, it simply needs to be well documented to encourage consistent use throughout the program. Following are the zest about how we can avoid deadlock in the context of java, Narrow down the synchronization's scope to as small a block as possible. Try to avoid locking multiple objects if possible.  Use a consistent lock order, by which if multiple locks need to be acquired will be acquired in a p

What Is Deadlock In java

A condition that occurs when two processes are each waiting for the other to complete before proceeding. The result is that both processes hang . Deadlocks occur most commonly in multitasking and client/server environments. Ideally, the programs that are deadlocked, or the operating system , should resolve the deadlock, but this doesn't always happen. The Following examples depicts how we can have deadlock in java. p ublic class MyDeadlockTest {   public static void main(String[] args)   {     final Object object1 = "object1" ;     final Object object2 = "object2" ;     // t1 tries to lock object1 then object2     Thread t1 = new Thread()     {       public void run()       {         // Lock resource 1         synchronized (object1)         {           System. out .println( "Thread 1: locked object 1" );           try           {             Thread.sleep(50);           }           catch

How Equals and == Works in Java

The "Object" Class in Java provides a method with name "equals" which is used to test if two objects are equal. And the "==" is a operator. This operator when used for primitives, returns true if two primitive values are equal. And for Object references, this "==" operator returns true when they are referring to the same object. The object "Object" which all other Java objects extend has an equals method which allows you to check to see if two objects are equal, which means you call equals on any object you want. Objet's implementation of equals does the following ( The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values xand y, this method returns true if and only if x and y refer to the same object ( x==y has the value true ). When you compare two instances using ==, you are actually comparing their memory addresses to see if they are ref

Working With JDBC

Connecting to a database In order to connect to a database, you need to perform some initialization first. Your JDBC driver has to be loaded by the Java Virtual Machine classloader, and your application needs to check to see that the driver was successfully loaded. We'll be using the ODBC bridge driver, but if your database vendor supplies a JDBC driver, feel free to use it instead. // Attempt to load database driver try { // Load Sun's jdbc-odbc driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance(); } catch (ClassNotFoundException cnfe) // driver not found { System.err.println ("Unable to load database driver"); System.err.println ("Details : " + cnfe); System.exit(0); } We try to load the JdbcOdbcDriver class, and then catch the ClassNotFoundException if it is thrown. This is important, because the application might be run on a non-Sun virtual machine that doesn't include the ODBC bridge, such as Microsoft's J