• 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

synchronization--->JQ+

 
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID :952739431860
Given the following program, which one of these statements is true?
public class TestClass 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)
{
workWithLocks();
workWithoutLocks();
}
}
void workWithLocks()
{
synchronized(lock1) { i1++ ; i2++; } // Line 1
synchronized(lock2) { k1++ ; k2++ ; } //Line 2
j1++; j2++;
}
void workWithoutLocks()
{
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 TestClass().start();
new TestClass().start();
}
}
I can not understand the meaning of Line 1 & Line 2.What i know about synchronized statements/block is that, the object's reference pass as a parameter in synchronized block, is the only object which has monitor.No other object can access that code within synchronized block.
In the above pgm composition is done.Every object of TestClass will have two objects of Object class.And access is allowes to these objects i.e lock1 & lock2.And NOT to the object of TestClass.
This is what my concept.I know it may be wrong.Please clarify me this.
Bye.
Viki.

------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A search gave me the following links where this topic is discussed:
http://www.javaranch.com/ubb/Forum24/HTML/012981.html
http://www.javaranch.com/ubb/Forum24/HTML/006363.html
http://www.javaranch.com/ubb/Forum24/HTML/009820.html
http://www.javaranch.com/ubb/Forum24/HTML/010183.html
http://www.javaranch.com/ubb/Forum24/HTML/012544.html
http://www.javaranch.com/ubb/Forum24/HTML/013433.html
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Vikrama Sanjeeva
Ranch Hand
Posts: 782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnax Valentin.Its actually my mistake that first i have to do a search,if i faild to get topic then i have to look for new post!.
But the case with me is that i am left with very few dayz.And it seems that dayz are racing.Anyhow i apologize!
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
reply
    Bookmark Topic Watch Topic
  • New Topic