import java.util.loggi ng.Level;
import java.util.loggi ng.Logger;
/**
*
* @author Administrator
*/
public class sync
{
public static void main(String[] args)
{
Thread t= new Thread(new class1());
Thread t1= new Thread(new class1());
t.start();
t1.start();
}
}
class class1 implements Runnable
{
static int balance=10;
public synchronized void run()
{
synchronized(th is)
{
try {
get();
Thread.sleep(50 00);
set();
} catch (InterruptedExc eption ex) {
Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
}
}
}
public synchronized void set()
{
++balance;
System.out.prin tln("balance is set Thread name=" + Thread.currentT hread().getName () + " is =" + balance);
}
public synchronized void get()
{
System.out.prin tln("balance in get Thread name=" + Thread.currentT hread().getName () + "is=" + balance);
}
}
output of program:
balance in get Thread name=Thread-0 , is=10
balance in get Thread name=Thread-1 , is=10
balance is set Thread name=Thread-1 , is =11
balance is set Thread name=Thread-0 , is =12
Expected output:
balance in get Thread name=Thread-0 ,is=10
balance in get Thread name=Thread-0 ,is=11
balance is set Thread name=Thread-1 , is =11
balance is set Thread name=Thread-1 , is =12
public synchronized void run()
{
try {
get();
Thread.sleep(50 00);
set();
} catch (InterruptedExc eption ex) {
Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
}
}
According to syncronization , Thread t should first execute and than Thread t1.
but in my case Thread t first call get() method than goes to sleep (then it should execute set() as it is LOCKED due to synchronization ) but Thread t1 is able to access the get() method , which should not happen .
why its happening
Thanks in advance
import java.util.loggi ng.Logger;
/**
*
* @author Administrator
*/
public class sync
{
public static void main(String[] args)
{
Thread t= new Thread(new class1());
Thread t1= new Thread(new class1());
t.start();
t1.start();
}
}
class class1 implements Runnable
{
static int balance=10;
public synchronized void run()
{
synchronized(th is)
{
try {
get();
Thread.sleep(50 00);
set();
} catch (InterruptedExc eption ex) {
Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
}
}
}
public synchronized void set()
{
++balance;
System.out.prin tln("balance is set Thread name=" + Thread.currentT hread().getName () + " is =" + balance);
}
public synchronized void get()
{
System.out.prin tln("balance in get Thread name=" + Thread.currentT hread().getName () + "is=" + balance);
}
}
output of program:
balance in get Thread name=Thread-0 , is=10
balance in get Thread name=Thread-1 , is=10
balance is set Thread name=Thread-1 , is =11
balance is set Thread name=Thread-0 , is =12
Expected output:
balance in get Thread name=Thread-0 ,is=10
balance in get Thread name=Thread-0 ,is=11
balance is set Thread name=Thread-1 , is =11
balance is set Thread name=Thread-1 , is =12
public synchronized void run()
{
try {
get();
Thread.sleep(50 00);
set();
} catch (InterruptedExc eption ex) {
Logger.getLogge r(class1.class. getName()).log( Level.SEVERE, null, ex);
}
}
According to syncronization , Thread t should first execute and than Thread t1.
but in my case Thread t first call get() method than goes to sleep (then it should execute set() as it is LOCKED due to synchronization ) but Thread t1 is able to access the get() method , which should not happen .
why its happening
Thanks in advance
Comment