File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Output

 
vivek dhiman
Ranch Hand
Posts: 152
Netbeans IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vivek dhiman
Ranch Hand
Posts: 152
Netbeans IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any Help ??
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 152
Netbeans IDE Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic