File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Explain output of this program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Explain output of this program" Watch "Explain output of this program" New topic
Author

Explain output of this program

murali chevuri
Greenhorn

Joined: Dec 24, 2004
Posts: 6
Hi all,

I ran this program on jdk1.5. Anybody Please explain output and why...


public class Thread extends Thread{

public synchronized void method1() throws Exception {
System.out.println(Thread.currentThread().getName() + " method1");
Thread.sleep(10*1000);
System.out.println(Thread.currentThread().getName() + " method1 done");
}

public synchronized void method2() throws Exception {
System.out.println(Thread.currentThread().getName() + " method2");
Thread.sleep(10*1000);
System.out.println(Thread.currentThread().getName() + " method2 done");
}

public void run() {
try {
method1();
//System.out.println("method 1 Done");
method2();
System.out.println(Thread.currentThread().getName()+" Done");
}catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
Thread1 t1 = new Thread1();
Thread1 t2 = new Thread1();
t1.setName("T1");
t2.setName("T2");
t1.setPriority(8);
t2.setPriority(9);
t1.start();
t2.start();
System.out.println("Done");
}
}


Output:
T1 method1
T2 method1
Done
T2 method1 done
T2 method2
T1 method1 done
T1 method2
T2 method2 done
T2 Done
T1 method2 done
T1 Done
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
The ouput is like that because the JVM has been written to do that But I'm sure that's not what you intend to ask.

Can you explain what you understood and what the confusion is? The code just displays basic java thread behaviour. So, if you can explain which part of the output is confusing you, I will be able to help you with an explanation.



PS: If you are new to threads, you may want to read an online tutorial. Apart from a lesson on Threads, this will give you a good understanding of Thread.sleep() which, I guess, is creating confusion in this case. Try Sun Trail on Threading and Concurrency.
murali chevuri
Greenhorn

Joined: Dec 24, 2004
Posts: 6
Hi praveen balaji,

Thanks for your reply. My doubt is method1 is synchronized. So I am expecting until Thread1 completes the method1 Thread2 will not enter. Same case for method2 also.But output is showing method1 and method2 allowing both the threads. Why its happen like that.
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
Hi Murali,

the reason you are not able to understand the behaviour of the programme is that you are missing the concept of object locking.

"one lock per java object"

when a thread tries to enter a synchronized method or block it first acquires the lock and excutes, if lock is not available (means other thread is already inside that synchronized method or block and excuting), it will go to Runnable state.
In your case you are creating two thread object means each thread having own different lock so both of them can enter the synchronized method whenever they get chance to execute.
In order to get this problem solved you better use Runnable interface and create multiple threads and call synchronized method.This time you wont get any abnormal behaviour.

well, i answered your question best of my knowledge .
Correct me if any of my point given above is wrong.

------------- gturopzx----------------
Sanjit Kumar
Ranch Hand

Joined: Dec 04, 2006
Posts: 35
hi murali
if you have any more doubts then just mail me or let me know
murali chevuri
Greenhorn

Joined: Dec 24, 2004
Posts: 6
Hi Sanjit Kumar,

Thanks for your reply. You are correct. I got the idea. Thanks for your clarification.
[ December 12, 2006: Message edited by: murali chevuri ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Explain output of this program
 
Similar Threads
deadlock?
Needs Explanation
Join() method in Java
Question on Synchronized mehods
Thread synchronization