Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: Strange Deadlock, where it wasn't supposed to be


Message #1 by <maratishe@h...> on Wed, 14 Nov 2001 16:30:45 +0900
Hi

You are right : this is the classic, simpliest exemple of what you cannot do 
in a program. It shows you that both threads try to access a ressources held 
by the other...DEADLOCK !! Of course in your application there will be many 
threads --> so many situation to look for.

It's a bad situation very difficult to manage in a big program. If you want 
to "master" multithreading in Java read a complete book on that subject. 
It's one wonderfull thing that Java support multi-threading. What I mean is 
: it's been designed from the start like that; they did not add 
multi-threading capabilities after the language was out --> they builded it 
like that. And with multi-threading you can find solution to time consuming 
task :

You have an applet that load music for the user to hear. One thread start 
downloading the music and when he is at 20% another thread start to play it 
--> the user doesn't have to wait the complete load to hear something

You have an applet that (when first build) load a lot of BIG BIG image which 
is time consuming : make your user see a progress bar while it's loading 
(the perception of time will be altered) or make them play a small game (a 
little game that you put when things like that happens). By the time they 
finish their first tic-tac-toe game --> everything had the time to load.

Just thimk about it : the capability "to do" (or have the impression) many 
things a the same time is fantastic for you programer. It give you a great 
deal of flexibility...

Hope this helps

Mario Bigras
Sun Certified Java Programmer

ICQ : 134413971
MSN : secure_4@h...



>From: "Kevin Mukhar" <kmukhar@e...>
>Reply-To: "Professional Java" <pro_java@p...>
>To: "Professional Java" <pro_java@p...>
>Subject: [pro_java] Re: Strange Deadlock, where it wasn't supposed to be
>Date: Wed, 14 Nov 2001 15:26:41
>
>I am curious why you think the class should run without deadlocking?
>
>That class is behaving exactly as it is supposed to. It will ALWAYS
>deadlock. That is why the class is named "Deadlock." Here's why. The main()
>method creates two thread objects and starts them running. The first thread
>object locks resource1 and then goes to sleep for 50 ms. This gives the
>second thread a chance to run; it locks resource2 and then sleeps for 50
>ms. When thread 2 sleeps, thread 1 gets a chance to run again. It tries to
>get resource2. The monitor of resource2 is held by thread 2, so thread 1
>blocks waiting for thread 2 to release. Thread 2 wakes up and tries to get
>resource1 which is locked by thread 1. Thread 2 now blocks waiting for
>thread 1 to release the lock. Since both threads are waiting for the other
>to release a resource, the threads are now deadlocked.
>
>Here's a picture
>
>Thread1 - get r1 lock - sleep ------- try r2 - block
>Thread2 --------------- get r2 lock - sleep -- try r1 - block
>
>Of course, it is entirely possible that thread 2 will get to run first.
>(Just because you create the threads in a certain order does not mean they
>will get the CPU in that order.) The outcome will still be the same.
>
> > In the below piece, according to all the standard rules, the Objects are
> > supposed to be accessed in turn, and in the real life, the thread hangs 
>
> > up. Why? Can't I lock another Object being within the other Synchronize 
>
> > keyword? Any help would be great.
> >
> >
> > public class Deadlock
> > {
> >     public static void main(String[] args)
> >     {
> >
> >  final Object resource1 =3D "resource1" ;
> >  final Object resource2 =3D "resource2" ;
> >
> > new Thread(){
> >     public void run()
> >      {
> >        synchronized(resource1){
> >         System.out.println("Thread 1: locked resource 1") ;
> >         try{
> >         Thread.sleep(50) ;
> >              }
> >         catch(InterruptedException e){
> >              }
> >
> >         synchronized(resource2){
> >         System.out.println("Thread 1: locked resource 2") ;
> >                  }
> >               }
> >            }
> >          }.start() ;
> >
> >
> >     new Thread(){
> >          public void run()
> >           {
> >             synchronized(resource2){
> >             System.out.println("Thread 2: locked resource 2") ;
> >             try{
> >             Thread.sleep(50) ;
> >                  }
> >                  catch(InterruptedException e){
> >                     }
> >
> >             synchronized(resource1){
> >             System.out.println ("Thread 2: locked resource 1") ;
> >                     }
> >                  }
> >               }
> >            }.start() ;
> >        }
> >    }
>
>---
>SIGS Conference for Java Development
>Targeted, focused classes, expertise level classes
>taught by Java gurus, rigorous tutorials, and
>exhibit floor makes SIGS Conference for Java
>Development a learning experience unlike any
>other. Join over 10,000 developers and programmers
>from across the U.S. and around the world who
>have benefited from attending SIGS/101 Conferences.
>http://www.javadevcon.com


_________________________________________________________________
Téléchargez MSN Explorer gratuitement à l'adresse 
http://explorer.msn.fr/intl.asp


  Return to Index