• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Thread Question from Khalid book!

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* Given the following code, which statements concerning the
objects referenced through the memeber variables i, j and k are true,
given that any thread may call the methods a, b and c at any time?
*/
class Counter
{ int v=0;
synchronized void inc(){v++;}
synchronized void dec(){ v--;}
}
public class cQ7ed5
{
Counter i;
Counter j;
Counter k;

public synchronized void a()
{
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b()
{
i.inc();j.inc();k.inc();
System.out.println("b");
i.dec();j.dec();k.dec();
}
public void c()
{
k.inc();
System.out.println("c");
k.dec();
}
}
/* 1 i.v is guaranteed always to be 0 or1
2 j.v is guaranteed always to be 0 or 1
3 k.v is guaranteed always to be 0 or 1
etc
*/
the answer is 1, 2, Could somebody explain it more clearly and write a program to run it?
thanks!
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,
public synchronized void a()
{
i.inc();
System.out.println("a");
i.dec();
}// These are synchronized methods.
public synchronized void b()// The thread that acquires the object lock by entering
{// one of these methods will prevent any other thread
i.inc();j.inc();k.inc();// from accessing any other synchronized methods till
System.out.println("b");// it releases the lock after exiting from the method.
i.dec();j.dec();k.dec();// So if i is incremented in a() (to 1),it is guarenteed to be
// decremented in a() (to 0) before method b() can be executed.
// Similarly j can take 1 and 0 only
}
public void c()//This method is not synchronized.
{// So a thread can execute this method
// even if another has the object lock.
k.inc();// So this can be asynchronously modified
System.out.println("c");// ie k can be incremented in method b() to 1
k.dec();// and another thread may execute c() if the
}//scheduler permits it so that k.inc in c() can make it 2.
}

Hope that helps.
 
Pravin Panicker
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sorry for the previous post the spaces did'nt come right.
I'll explain again :
a() and b() are synchronized methods.

The thread that acquires the object lock by entering
one of these methods (say a()) will prevent any other thread
from accessing the other synchronized method ( b() and vice versa) till
it releases the lock after exiting from the method.
So if i is incremented in a() (to 1),it is guarenteed to be
decremented in a() (to 0) before method b() can be executed.
Similarly j can take 1 and 0 only since it is inc'ed and dec'td only in method
b().
c() method is not synchronized.
So a thread can execute this method
even if another has the object lock.
ie k can be incremented in method b() to 1
and another thread may execute c() before k is decremented in b(), if the
scheduler permits it, so that k.inc in c() can make it 2.
 
Jeff Sky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I am confused here, is the j.v shared by other instance, v is not static variable, could you explain it?
thanks!
------------------
Jeff
 
Jeff Sky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, I mean k.v . it is not static variable!
------------------
Jeff
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,
Pravin's second reply is right on the money! You must not associate threads with instances. One instance can launch many threads ... Therefore the example doesn't need any variables to be static to lead to the correct answers. The example assumes only on instance of class cQ7ed5.
Regards,
Manfred.
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jeff,
Please note that k.v is changed inside a non-synchronized method and i.v and j.v are changed inside synchronized method.
also note that increment and decrement are done in all the methods. Now when synchronized methods are called no other synchronized methos will be executed and so if a variable is incremented then it will be surely decremented. so in between the increment and decrement operation value can be 0 and 1 not anything else.
But a non-synchronized method dont have this restriction. now consider the case that a thread executes either b() or c()and increments k.v and now before it gets time to execute decrement operation another thread starts then k.v is again incremented and now the value is 1+1 = 2 . so we cant guarantee that k.v is 0 or 1.
Cherry
 
Jeff Sky
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Manfred:
I know I am confused with the basic concept of thread here!
Does it mean, if you create a instance , then the instance call call start() method many times, it means lauch many thread, but every thread exist for just this instance , so they do not need use static variable, just instance variable.
so they use the same varible v!
right?
thanks!

Originally posted by Manfred Leonhardt:
Hi Jeff,
Pravin's second reply is right on the money! You must not associate threads with instances. One instance can launch many threads ... Therefore the example doesn't need any variables to be static to lead to the correct answers. The example assumes only on instance of class cQ7ed5.
Regards,
Manfred.



------------------
Jeff
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. That is how I understand it.
Manfred.
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*************************************************************
hi Manfred:
I know I am confused with the basic concept of thread here!
Does it mean, if you create a instance , then the instance call call start() method many times, it means lauch many thread, but every thread exist for just this instance , so they do not need use static variable, just instance variable.
so they use the same varible v!
right?
thanks!
*************************************************************
From the above, I think you mean the instance is a Thread object instance. Actually, you can't call start many times unless the Thread object is dead. o.w. you will get IllegalStateException.
I think the question here is creating different Thread objects and give the same object reference cQ7ed5 for it. In different Thread objects' run method, they will also call that cQ7ed5's a, b and c method asynchronously.
 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic