my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused on thread topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused on thread topic" Watch "confused on thread topic" New topic
Author

confused on thread topic

Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Hi,
please read this code:

Q:
Given the following program, which statement is true?

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.

1) The program will fail to compile.

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

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

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

5) One can be certain that the letter k will never be printed during execution.

A: 2

Please i'm confused
need more explanation
Thank you
[ July 19, 2005: Message edited by: John Edwin ]

SCWCD 1.4(Loading...), SCJP 1.4(98%), Bachelor of Engineering (computer science)
Saloni Mangla
Greenhorn

Joined: Apr 19, 2005
Posts: 3
qoute
-----------------------------------------------
Given the following program, which statement is true?

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();
}
}
--------------------------------------------

As seen the vaiables are defined as volatile, what i know of volatile variables is that "they tell the JVM that a thread
accessing the variable must always reconcile its own private copy of the variable with the master copy in memory."
But again, you can ask that the same is declared as 'static' so should it allow to make a private copy??? (I'll look for an answer for myself on this).

so going by the definition of volatile modifiers, you cannot be certain about the values of i1,i2,j1,j2,k1,k2 as they are increamented with reference to object lock1,lock2,lock1 and then are being compared with refrence to the an instance.

This is how I justify the answer 2.
Hope it makes sense.

-Saloni
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

Originally posted by Saloni Mangla:
qoute
"they tell the JVM that a thread
accessing the variable must always reconcile its own private copy of the variable with the master copy in memory."


can someone explain this concept plz, thanx.


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Still confused.
please help
Adil El mouden
Ranch Hand

Joined: Jun 01, 2005
Posts: 82
Waiting for help...
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Originally posted by Marzo Kaahn:


can someone explain this concept plz, thanx.


A volatile variable is not allowed to have a local copy of variable for any thread. So effectivly at any time all threads look at the same value.
The regular variable are copied to each thread and time to time they are synched with main memory.


Devender Thareja
SCEA, SCBCD, SCJP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused on thread topic