It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Output Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Thread Output" Watch "Thread Output" New topic
Author

Thread Output

vivek dhiman
Greenhorn

Joined: Aug 05, 2011
Posts: 26


Above code gives me the output:
Thread-0 2 * 1 = 2
Thread-0 2 * 2 = 4
Thread-0 2 * 3 = 6
Thread-0 2 * 4 = 8
Thread-0 2 * 5 = 10
Thread-0 2 * 6 = 12
Thread-0 2 * 7 = 14
Thread-0 2 * 8 = 16
Thread-0 2 * 9 = 18
Thread-0 2 * 10 = 20
Thread-1 2 * 1 = 2
Thread-0 5 * 1 = 5
Thread-1 2 * 2 = 4
Thread-0 5 * 2 = 10
Thread-1 2 * 3 = 6
Thread-0 5 * 3 = 15
Thread-1 2 * 4 = 8
Thread-0 5 * 4 = 20
Thread-1 2 * 5 = 10
Thread-0 5 * 5 = 25
Thread-1 2 * 6 = 12
Thread-0 5 * 6 = 30
Thread-1 2 * 7 = 14
Thread-0 5 * 7 = 35
Thread-1 2 * 8 = 16
Thread-0 5 * 8 = 40
Thread-0 5 * 9 = 45
Thread-1 2 * 9 = 18
Thread-0 5 * 10 = 50
Thread-1 2 * 10 = 20
Thread-1 5 * 1 = 5
Thread-1 5 * 2 = 10
Thread-1 5 * 3 = 15
Thread-1 5 * 4 = 20
Thread-1 5 * 5 = 25
Thread-1 5 * 6 = 30
Thread-1 5 * 7 = 35
Thread-1 5 * 8 = 40
Thread-1 5 * 9 = 45
Thread-1 5 * 10 = 50

My question is why Thread - 1 not synchronized 2's table, why it is executing with 5 table.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 867

vivek dhiman wrote:class printt
{
synchronized void prnteven(int k)
{
for(int i=1;i<=10;i++)
{
System.out.println(Thread.currentThread().getName() +" "+k +" * "+ i +" = "+i*k);
try
{
Thread.sleep(500);
}
catch(Exception e){}
}
}
void prntodd(int k)
{
for(int i=1;i<=10;i++)
{

System.out.println(Thread.currentThread().getName() +" "+k +" * "+ i +" = "+i*k);
try
{
Thread.sleep(500);
}catch(Exception e){}
}
}
}
class th implements Runnable
{
printt obj=new printt();
public void run()
{
obj.prnteven(2);
obj.prntodd(5);
}
public static void main(String a[])
{
th t1=new th();
Thread o1=new Thread(t1);// Thread - 0
Thread o2=new Thread(t1);//Thread - 1
o1.start();
o2.start();
}
}
Above code gives me the output:
Thread-0 2 * 1 = 2
Thread-0 2 * 2 = 4
Thread-0 2 * 3 = 6
Thread-0 2 * 4 = 8
Thread-0 2 * 5 = 10
Thread-0 2 * 6 = 12
Thread-0 2 * 7 = 14
Thread-0 2 * 8 = 16
Thread-0 2 * 9 = 18
Thread-0 2 * 10 = 20
Thread-1 2 * 1 = 2
Thread-0 5 * 1 = 5
Thread-1 2 * 2 = 4
Thread-0 5 * 2 = 10
Thread-1 2 * 3 = 6
Thread-0 5 * 3 = 15
Thread-1 2 * 4 = 8
Thread-0 5 * 4 = 20
Thread-1 2 * 5 = 10
Thread-0 5 * 5 = 25
Thread-1 2 * 6 = 12
Thread-0 5 * 6 = 30
Thread-1 2 * 7 = 14
Thread-0 5 * 7 = 35
Thread-1 2 * 8 = 16
Thread-0 5 * 8 = 40
Thread-0 5 * 9 = 45
Thread-1 2 * 9 = 18
Thread-0 5 * 10 = 50
Thread-1 2 * 10 = 20
Thread-1 5 * 1 = 5
Thread-1 5 * 2 = 10
Thread-1 5 * 3 = 15
Thread-1 5 * 4 = 20
Thread-1 5 * 5 = 25
Thread-1 5 * 6 = 30
Thread-1 5 * 7 = 35
Thread-1 5 * 8 = 40
Thread-1 5 * 9 = 45
Thread-1 5 * 10 = 50

My question is why Thread - 1 not synchronized 2's table, why it is executing with 5 table.


Please use code tags while posting . Thanks


OCPJP 6(100 %) OCEWCD 6(91 %)
vivek dhiman
Greenhorn

Joined: Aug 05, 2011
Posts: 26

Any Help ??
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 867

vivek dhiman wrote:Any Help ??


your prntodd method is not synchrnized. there are two threads which are running/executing code of single th object. when the first thread thread -0 enters printeven , it acquires lock on the th object thus restraining thread 1 to enter the prntevn method. when first thread is done with the prntevn method, it releases the lock which is acquired by the second thread which starts executing the prnteven code. the first thread is free to enter the prntodd method because it is not synchronized. remember when a thread acquires a lock on the object NO OTHER THREAD CAN ENTER ITS i.e. OBJECT'S synchronized parts(method/block). however non-synchronized code is free for concurrent access. also keep in mind that sleep does not release the object.
vivek dhiman
Greenhorn

Joined: Aug 05, 2011
Posts: 26

Thanks for reply, it means thread - 1 also acquired the lock on prnteven function but as thread -0 has no lock on prntodd() thus the output seems mix of both of these functionality.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 867

vivek dhiman wrote:Thanks for reply, it means thread - 1 also acquired the lock on prnteven function but as thread -0 has no lock on prntodd() thus the output seems mix of both of these functionality.[/quote

yes you are right , but try to be accurate in terminology. it is not lock on prnteven function. lock is aquired on object. so you should say thread -1 acquired lock on th object.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Thread Output
 
Similar Threads
A Hint at Times is good?
How to make a main thread wait until all other threads die out?
Output even more unexpected than I was expecting!
Multiplication Table
join() in Threads