Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: How to avoid the Deadlock in the below program

  1. #1
    Junior Member
    Join Date
    Nov 2009
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to avoid the Deadlock in the below program

    Hello Friends,

    I am learning java now.I know the following program has deadlock, now i trying the avoid the deadlock for this
    program so please post your some suggestions how to avoid the deadlock for the below.
    class A
    {
    	synchronized void foo(B b)
    	{
    		System.out.println(Thread.currentThread().getName()+"  Entered into A.foo()");
     
    		try
    		{
    			Thread.sleep(1000);
    		}
    		catch (InterruptedException e)
    		{
    			System.out.println("A Caught Exception");
    		}
     
    		System.out.println(Thread.currentThread().getName()+" Now trying to Access b.last()");
    		b.last();
    		System.out.println(Thread.currentThread().getName()+" Now trying to Access b.Info()");
    		b.info();
     
    	}
    	synchronized void last()
    	{
    		System.out.println(Thread.currentThread().getName()+"  Entered into A.last()");
     
    	}
    	void msg()
    	{
    		System.out.println(Thread.currentThread().getName()+"  Entered into A.msg()");
    	}
    }
    class B
    {
    	synchronized void bar(A a)
    	{
    		System.out.println(Thread.currentThread().getName()+"  Entered into B.bar()");
     
    		try
    		{
    			Thread.sleep(1000);
    		}
    		catch (Exception e)
    		{
    			System.out.println("B Caught Exception");
    		}
    		System.out.println(Thread.currentThread().getName()+" Now trying to Access A.last()");
    		a.last();
    		System.out.println(Thread.currentThread().getName()+" Now trying to Access A.msg()");
    		a.msg();
    	}
    	synchronized void last()
    	{
    		System.out.println(Thread.currentThread().getName()+"  Entered into B.last()");
    	}
    	void info()
    	{
    		System.out.println(Thread.currentThread().getName()+"  Entered into B.info()");
    	}
    }
    class  DeadLockTest2 implements Runnable
    {
    	A a=new A();
    	B b=new B();
     
    	DeadLockTest2()
    	{
    		Thread.currentThread().setName("Thread1");
    		System.out.println(Thread.currentThread().getName()+" Is Now Running");
    		new Thread(this,"Thread2").start();
    		System.out.println(Thread.currentThread().getName()+" Is Now Running");
    		System.out.println(Thread.currentThread().getName()+" is trying to get the lock on A.foo()");
    		a.foo(b);
    		System.out.println(Thread.currentThread().getName()+" is got  the lock on A.foo()");
    		nonSyn();
    	}
    	public void run()
    	{
    		System.out.println(Thread.currentThread().getName()+" Is Now Running");
    		System.out.println(Thread.currentThread().getName()+" is trying to get the lock on B.bar()");
    		b.bar(a);
    		System.out.println(Thread.currentThread().getName()+" is got  the lock on B.bar()");
    		nonSyn();
     
    	}
    	public void nonSyn()
    	{
    		a.msg();
    		b.info();
    	}
    	public static void main(String[] args) 
    	{
    		new DeadLockTest2();
    	}
    }
    Last edited by helloworld922; April 15th, 2010 at 09:40 PM.


Similar Threads

  1. Develop Superfast Programs - Avoid Threads
    By freespirit in forum The Cafe
    Replies: 2
    Last Post: March 18th, 2010, 01:49 AM
  2. Identify and avoid some of the pitfalls in learning to use generics
    By JackyRock in forum Java Theory & Questions
    Replies: 0
    Last Post: February 6th, 2010, 05:12 AM