Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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.
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic