• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Doubt in Threads

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

Can any one please explain how the doIt() will be executed by the two threads in the below code....



Thanks,
Ravindra.

[ UD: added CODE tags for better readability ]
[ August 19, 2007: Message edited by: Ulf Dittmer ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the meaning of while(true).
Bccause, it looks like the program will run forever. I mean the while loop will never stop.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



As two locks objects are used, threads t1 and t2 will be able to call doit() without blocking each other.
However, inside the method, things may be different, eg :
If t1 get lock1 for i1++, t2 will be unable to get lock1 for i1++ OR i2++ and will be blocking.
If t2 get lock2 for k1 & k2, t1 will be able to get lock1 for i1 OR i2
For j1 and j2, t1 and t2 may use them as they want ...
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code needs the while true to long quite a long time. Only rarely the i and j values are unequal.
k values are never unequal.

If you want a shorter output, you have to put yield-calls into the method to give other threads a chance.



produces
i
j
i
j
i
j
i
j
i
j
i
j
i
j
i
j
i
j

on my system.
Also here the k values are never unqual.

But you have forgotten something:
Quote your sources, please.

Yours,
Bu.
 
Oh. Hi guys! Look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic