This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Query about threads

 
Ranch Hand
Posts: 156
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its khalid moghul book question

Can any body explain how the answer is b?

public class MyClass extends Thread {
static Object lock1 = new Object();
static Object lock2 = new Object();

static volatile int i1, i2, j1, j2, k1, k2;

public void run() { while (true) { doit(); check(); } }

void doit() {
synchronized(lock1) { i1++; }
j1++;
synchronized(lock2) { k1++; k2++; }
j2++;
synchronized(lock1) { 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[] args) {
new MyClass().start();
new MyClass().start();
}
}

Select the one correct answer.

a)The program will fail to compile.

b)One cannot be certain whether any of the letters i, j, and k will be printed during execution.

c)One can be certain that none of the letters i, j, and k will ever be printed during execution.

d)One can be certain that the letters i and k will never be printed during execution.

e)One can be certain that the letter k will never be printed during execution.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ANS:
...............
b is incorrect ans,

k1==k2 is never true, i.e k value is never priented,
 
Seetharamireddy Mittala
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ANS:
...............
b is incorrect ans,

k1==k2 is never true, i.e k value is never priented,
 
author
Posts: 23878
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B is the correct answer. You can't be certain if I, J, or K will be printed. Here's why...

J is obvious. There is no lock protecting it, hence, anything could happen.

I is less obvious. There is a lock protecting it, but there is a point between the two operations where there is no lock.

K is really subtle. You would think it is safe since there is a lock and it protects both operations. However, the check() method is also accessing the K variable, and without the protection of any lock.

In fact, you can just use the last reason, to say that you can't be certain of I, J, and K.

Henry
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything can be printed . in case of k , if check were a synch method ,
we can be sure that k can never be printed .
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ramakrishna RangaRaoOne AllamOne:
Anything can be printed . in case of k , if check were a synch method ,
we can be sure that k can never be printed .



Ya RamaKrishna would be true, if check() method is locked by "lock2".
by the way answer 'b' is correct.

 
Dinner will be steamed monkey heads with a side of tiny ads.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic