JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Showing posts with label deadloc. Show all posts
Showing posts with label deadloc. Show all posts

java thread deadlock

10:07 PM
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in different order. A Java multithreaded program may suffer from the deadlock condition because the  synchronized  keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object. Here is an example. Example public class TestThread { public static Object Lock1 = new Object (); public static Object Lock2 = new Object (); public static void main ( String args []) { ThreadDemo1 T1 = new ThreadDemo1 (); ThreadDemo2 T2 = new ThreadDemo2 (); T1 . start (); T2 . start (); } private static class ThreadDemo1 extends Thread { public void run () { synchronized ( Lock1 ) { System . out . println ( "Thread 1: Holding lock 1..." );

.