i just finished a chapter in a book about the basics of Thread(threading).. and i want to focus myself on the deadlock issue so i can finish the chapter clearly,(im not yet into going to "heavy concurrency", i just want to understand some fundamentals crystal clear)..
im trying to create or rather produce a deadlock scenario.. heres the code..
public class DeadLockSample { private Foo foo; private Bar bar; public DeadLockSample() { foo = new Foo(); bar = new Bar(); } public static void main(String[] args) { Runnable job1 = new DeadLockSample().new Job1(); Runnable job2 = new DeadLockSample().new Job2(); Thread threadA = new Thread(job1, "Thread A"); Thread threadB = new Thread(job2, "Thread B"); threadA.start(); threadB.start(); } private class Job1 implements Runnable { @Override public void run() { foo.foo(); } } private class Job2 implements Runnable { @Override public void run() { foo.foo(); } } private class Foo { public synchronized void foo() { System.out.println(Thread.currentThread().getName() + " Enters foo()"); System.out.println(Thread.currentThread().getName() + " will sleep..."); System.out.println(Thread.currentThread().getName() + " locked the access to foo().."); try { Thread.sleep(200); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " woke up!"); bar.bar(); } } private class Bar { public synchronized void bar() { System.out.println(Thread.currentThread().getName() + " Enters bar()"); System.out.println(Thread.currentThread().getName() + " will sleep..."); System.out.println(Thread.currentThread().getName() + " locked the access to bar().."); try { Thread.sleep(200); } catch (InterruptedException ex) { ex.printStackTrace(); } System.out.println(Thread.currentThread().getName() + " woke up!"); } } }
the output
based on what i understand(an A.B.C explanation on the book) a synchronized method will prohibit a multiple access from different threads..Thread A Enters foo() Thread A will sleep... Thread A locked the access to foo().. Thread B Enters foo() Thread B will sleep... Thread B locked the access to foo().. Thread B woke up! Thread A woke up! Thread B Enters bar() Thread B will sleep... Thread B locked the access to bar().. Thread A Enters bar() Thread A will sleep... Thread A locked the access to bar().. Thread B woke up! Thread A woke up!
Thread-A enters foo(), so it should have the object lock or key, then it should be locked from other threads, then Thread-A sleeps, kicking it out of running state(back to runnable), making Thread-B(running) access foo(), then how come Thread-B access foo()? if it DOESNT hold the object key the Thread-A has? i just want to skip this part, and conitnue on other things that i need to study, i just want to ingest-digest this thing before i turn my face on other place, thanks for any help...
edit: and Thread-A should not drop the key right?, until it finishes a call to the method, until then Thread-B could have the key to access foo()..