This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multi threading

 
sri rallapalli
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody please let me know the ans and how for the following code.

public class Myclass extends Thread
{
static Object lok1 = new Object();
static Object lok2 = new Object();
static volatile int i1,i2,j1,j2,k1,k2;
public void run()
{
while(true)
{
doit();
check();
}
}
void doit()
{
synchronized(lok1)
{
i1++;
}
j1++;
synchronized(lok2)
{
k1++;k2++;
}
j2++;
synchronized(lok1)
{
i2++;
}
}
void check()
{
if(i1!=i2) System.out.println("i");
if(j1!=j2) System.out.println("j");
if(k1!=k2) System.out.println("k");
}
public static void main(String arg[])
{
new Myclass().start();
new Myclass().start();
}
}

thanks,
sri.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic