• 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 with thread

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source http://www.valoxo.ch/jr/mocks/mock01a.html Q52

What is the output of the following code when compiled and run? Select two correct answers.
class A {
static int i=0;
public synchronized void aMethod(){ i++; }
public void bMethod(){
synchronized(this){ i--; }
}
}
public class Question52 implements Runnable {
A a = new A();
public static void main(String[] args) {
Thread t1 = new Thread(new Question52());
Thread t2 = new Thread(new Question52());
t1.start(); t2.start();
}
public void run(){
for(int i=0;i<10000000;i++){
a.aMethod(); a.bMethod();
}
System.out.print("a.i="+a.i+" ");
}
}

A. The output will always be a.i=0 a.i=0

B. The output cannot be determined.

C. The output will always be a.i=0 a.i=-1

D. t1 and t2 access the same member variable a.i.

E. Compilation error.

Ans:B,D
Doubt: how ans is B, how to go about s tricking such type of question?

pls help,
Gitesh
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh ,
Answer is correct. just do it carefully.
E is fine as program is compiling.
B ->
Static block runs when class is loaded,the very first thing.
Instance initializer runs when trying new'ng the class,before constructor runs.then constructor runs
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i am sorry ...i replied in wrong post.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh,
In thread there is no predictable output.When a.aMethod() run & when a.bMethod(); run you dont know.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dolly,
both the methods aMethod() and bMethod()use the same A reference a and as methods are synchronized only 1 thread will acquire lock....as both the methods are in run()..

Well in short i'm totally confused

SOS!! HELP!!
GiTeSh
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class ThreadDemo {
synchronized void a() { actBusy(); }
static synchronized void b() { actBusy(); }
static void actBusy() {
try { Thread.sleep(1000); }
catch (InterruptedException e) {}
}
public static void main(String[] args) {
final ThreadDemo x = new ThreadDemo();
final ThreadDemo y = new ThreadDemo();
Runnable runnable = new Runnable() {
public void run() {
int option = (int) (Math.random() * 4);
switch (option) {
case 0: x.a(); break;
case 1: x.b(); break;
case 2: y.a(); break;
case 3: y.b(); break;
} }
};
Thread thread1 = new Thread(runnable);
Thread thread2 = new Thread(runnable);
thread1.start();
thread2.start();
} }
Self Test Answers 753
754 Chapter 9: Threads
Which of the following pairs of method invocations could NEVER be executing at the same
time? (Choose all that apply.)
A. x.a() in thread1, and x.a() in thread2
B. x.a() in thread1, and x.b() in thread2
C. x.a() in thread1, and y.a() in thread2
D. x.a() in thread1, and y.b() in thread2
E. x.b() in thread1, and x.a() in thread2
F. x.b() in thread1, and x.b() in thread2
G. x.b() in thread1, and y.a() in thread2
H. x.b() in thread1, and y.b() in thread2
Answer:
A , F and H. A is incorrect because synchronized instance methods called on the same
instance, block each other. F and H could not happen because synchronized static
methods in the same class block each other, regardless of which instance was used to call
the methods. (An instance is not required to call static methods; only the class.)
�˚ C could happen because synchronized instance methods called on different instances
do not block each other. B, D, E, and G could all happen because instance methods and
static methods lock on different objects, and do not block each other.

Please read this care fully, this example is from the K & B book, chapter-9, example-15. I think it will help you.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gitesh,

Regarding the first question posted, we have two runnable instances created and each have their own copy of Class A. Even though thread is calling the synchronized method of class A and thread acquires a lock on the Object, it would not block the other thread to continue with its execution and in fact very well can call the synchronized method of class A but on a different object. Since both the threads execute concurently, there is no way we can determine its output. I'm really not sure, If this is clarifying your doubt, but this is what I think.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Gitesh

The thing is that here variable "i" is static i.e class variable. And for thread to access class varible , they need to acquire "Class Lock". And only one thread can have access to lock at one time, so here both thread cannot access class varibale "i" at same time.

And moreover if you read the java specification for threads, you will come to know that thread behaviour is dependent on underlying OS and other environments. So their behaviour is always unpredicatable.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!!

I think the explanation given by Devashish Roy
is great!!!
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think in this case the Class lock would be acuired. Method methodA is declared synchronized, therefore synchronized on this(object) and methodB's body is syncrhonized on (this) object. To be synchronized on the current object (this) is not the same as to be synchronized on the Class.
Which object/Class the synchronization is done upon, in the case of syncrhornized methods is decided depending on wheather the method is static or not, if it is static method, then synchronization is on the Class itself, if not it is on this (object). If just part of the method is synchronized, what comes after the keyword synchronized(...) is what we synchronize on. In the code above it would be 'this'.

Please let me know if you disagree, but this is my understanding of it.
 
You got style baby! More than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic