Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Thread Output

 
Ranch Hand
Posts: 157
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 157
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any Help ??
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 157
Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic