SunScreenCert
asked on
Plz explain this program..
Here in this program there is a static lock, which is a lock on a class, so at a time only lock1 or lock2 can be attained. So i am not understanding the output:
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){ System.out.print(Thread.cu rrentThrea d().getNam e()); i1++; }
j1++;
synchronized(lock2){ System.out.print(Thread.cu rrentThrea d().getNam e()); k1++; k2++; }
j2++;
synchronized(lock1){ System.out.print(Thread.cu rrentThrea d().getNam e()); i2++; }
}
void check(){
try{
Thread.sleep(100);
if(i1 != i2) System.out.print("i1" + i1 + "i2" + i2);
else System.out.print("not i" + i1 + "not i2" + i2);
}catch(InterruptedExceptio n e){}
try{
Thread.sleep(100);
if(j1 != j2) System.out.print("j1" + j1 + "j2" + j2);
else System.out.print("not j1" + j1 + "not j2" + j2);
}catch(InterruptedExceptio n e){}
try{
Thread.sleep(100);
if(k1 != k2) System.out.print("k1" + k1 + "k2" + k2);
else System.out.print("not k1" + k1 + "not k2" + k2);
}catch(InterruptedExceptio n e){}
}
public static void main(String... args){
new Thread(new MyClass(),"First Thread").start();
//new Thread(new MyClass(), "Second Thread").start();
}
}
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){ System.out.print(Thread.cu
j1++;
synchronized(lock2){ System.out.print(Thread.cu
j2++;
synchronized(lock1){ System.out.print(Thread.cu
}
void check(){
try{
Thread.sleep(100);
if(i1 != i2) System.out.print("i1" + i1 + "i2" + i2);
else System.out.print("not i" + i1 + "not i2" + i2);
}catch(InterruptedExceptio
try{
Thread.sleep(100);
if(j1 != j2) System.out.print("j1" + j1 + "j2" + j2);
else System.out.print("not j1" + j1 + "not j2" + j2);
}catch(InterruptedExceptio
try{
Thread.sleep(100);
if(k1 != k2) System.out.print("k1" + k1 + "k2" + k2);
else System.out.print("not k1" + k1 + "not k2" + k2);
}catch(InterruptedExceptio
}
public static void main(String... args){
new Thread(new MyClass(),"First Thread").start();
//new Thread(new MyClass(), "Second Thread").start();
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok great explanation, so now tell me that when lock1 and lock2 acquire lock, they acquire lock on different objects, not on Class object. So they run simultaneously. But since there are two threads running so can they swap acquiring locks.
Sorry for all the silly questions. I am really not able to understand the flow or output. I even changed things this way to understand each output
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(i1 < 1){
doIt();
check();
}
}
void doIt(){
synchronized(lock1){ System.out.println(Thread. currentThr ead().getN ame()); i1++; System.out.println(i1); }
j1++;
synchronized(lock2){ System.out.println(Thread. currentThr ead().getN ame()); k1++; k2++; System.out.println(k1); System.out.println(k2); }
j2++;
synchronized(lock1){ System.out.println(Thread. currentThr ead().getN ame()); i2++; System.out.println(i2); }
}
void check(){
try{
Thread.sleep(100);
if(i1 != i2) System.out.print("i1 " + i1 + "i2 " + i2);
else System.out.print("not i1 " + i1 + "not i2 " + i2);
}catch(InterruptedExceptio n e){}
try{
Thread.sleep(100);
if(j1 != j2) System.out.print("j1 " + j1 + "j2 " + j2);
else System.out.print("not j1 " + j1 + "not j2 " + j2);
}catch(InterruptedExceptio n e){}
try{
Thread.sleep(100);
if(k1 != k2) System.out.print("k1 " + k1 + "k2 " + k2);
else System.out.print("not k1 " + k1 + "not k2 " + k2);
}catch(InterruptedExceptio n e){}
}
public static void main(String... args){
new Thread(new MyClass(),"First Thread").start();
//new Thread(new MyClass(), "Second Thread").start();
}
}
Sorry for all the silly questions. I am really not able to understand the flow or output. I even changed things this way to understand each output
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(i1 < 1){
doIt();
check();
}
}
void doIt(){
synchronized(lock1){ System.out.println(Thread.
j1++;
synchronized(lock2){ System.out.println(Thread.
j2++;
synchronized(lock1){ System.out.println(Thread.
}
void check(){
try{
Thread.sleep(100);
if(i1 != i2) System.out.print("i1 " + i1 + "i2 " + i2);
else System.out.print("not i1 " + i1 + "not i2 " + i2);
}catch(InterruptedExceptio
try{
Thread.sleep(100);
if(j1 != j2) System.out.print("j1 " + j1 + "j2 " + j2);
else System.out.print("not j1 " + j1 + "not j2 " + j2);
}catch(InterruptedExceptio
try{
Thread.sleep(100);
if(k1 != k2) System.out.print("k1 " + k1 + "k2 " + k2);
else System.out.print("not k1 " + k1 + "not k2 " + k2);
}catch(InterruptedExceptio
}
public static void main(String... args){
new Thread(new MyClass(),"First Thread").start();
//new Thread(new MyClass(), "Second Thread").start();
}
}
ASKER
ok i made this simple program to understand it
import java.util.*;
class MyCurrentDate implements Runnable{
Date d = new Date();
public void run(){
synchronized(MyCurrentDate .class){
for(int i = 0; i < 2; i++){
System.out.println(Thread. currentThr ead() + " " + d);
try{
Thread.sleep(10000);
}catch(InterruptedExceptio n e){
e.printStackTrace();
}
}
}}
}
class MyMain{
public static void main(String... args){
Thread a = new Thread(new MyCurrentDate(), "a1");
Thread b = new Thread(new MyCurrentDate(), "b1");
Thread c = new Thread(new MyCurrentDate(), "c1");
a.start();
b.start();
c.start();
System.out.println("enteri ng into threadpool");
}
}
Now since we have class lock once Thread a1 finishes execution it is only after that b1 starts..
and if we synchronize on object that is the this instance then all threads continue running.
import java.util.*;
class MyCurrentDate implements Runnable{
Date d = new Date();
public void run(){
synchronized(MyCurrentDate
for(int i = 0; i < 2; i++){
System.out.println(Thread.
try{
Thread.sleep(10000);
}catch(InterruptedExceptio
e.printStackTrace();
}
}
}}
}
class MyMain{
public static void main(String... args){
Thread a = new Thread(new MyCurrentDate(), "a1");
Thread b = new Thread(new MyCurrentDate(), "b1");
Thread c = new Thread(new MyCurrentDate(), "c1");
a.start();
b.start();
c.start();
System.out.println("enteri
}
}
Now since we have class lock once Thread a1 finishes execution it is only after that b1 starts..
and if we synchronize on object that is the this instance then all threads continue running.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER