Welcome to the Java Programming Q&A Forum

greenspun.com : LUSENET : Java Programming : One Thread


This forum is intended to provide support to people who are learning Java, studying for Java certification, or have any other general Java related programming problems.

Java Main is the web-site related to this forum, and lives at http://www.javamain.com

The forum is by Java programmers for Java programmers, so start asking those questions!

-- Andrew Mackie (admackie@javamain.co.uk), November 15, 1999


I'd learned Java 9 months back by going through "Java - The Complete Reference -by Naughton and Herb Schild". But, I am away from Java Programming. ( I was busy in doing a project in VB-SQL Server). I now planned for Java Cert. Exam . Anybody interested to taking up the exam in 3/4 week of December, interested in working/preparing out synchronously can mail me and we can reach the success. My home page got good links to different Java Cert Sites and mock exams. Please do visit once. http://members.rediff.com/vamsikalyan/myhome.html

-- Vamsi Kalyan Poondla (vamsy_kalyan@rediffmail.com), November 26, 1999.

I don't think I am ready for any sort of certification exams thus far.

I got me this puzzle just yesterday, and can't fix it. 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 = "resource1" ; final Object resource2 = "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() ; } }

-- marat (maratishe@hotmail.com), November 13, 2001.

Moderation questions? read the FAQ