Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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

confused on thread topic

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still confused.
please help
 
Adil El mouden
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waiting for help...
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
    Bookmark Topic Watch Topic
  • New Topic