• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explain output of this program

 
murali chevuri
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi murali
if you have any more doubts then just mail me or let me know
 
murali chevuri
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic