public class synTest {
public static void main ( String[ ] args) {
AccountingSync accountingSync = new AccountingSync ( ) ;
new Thread ( ( ) - > {
accountingSync. run ( ) ;
} ) . start ( ) ;
}
}
class AccountingSync implements Runnable {
static AccountingSync instance= new AccountingSync ( ) ;
static int i= 0 ;
public void run ( ) {
for ( int j= 0 ; j< 10000000 ; j++ ) {
synchronized ( instance) {
i++ ;
}
}
System. out. println ( i) ;
}
}
public class synTest1 {
public static void main ( String[ ] args) throws InterruptedException {
AccountingSyn1 accountingSync = new AccountingSyn1 ( ) ;
Thread t1 = new Thread ( accountingSync) ;
Thread t2 = new Thread ( accountingSync) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
class AccountingSyn1 implements Runnable {
static AccountingSync instance= new AccountingSync ( ) ;
static int i= 0 ;
public synchronized void increase ( ) {
i++ ;
}
public void run ( ) {
for ( int j = 0 ; j < 10000000 ; j++ ) {
increase ( ) ;
}
System. out. println ( i) ;
}
}
public class synTest2 {
public static void main ( String[ ] args) throws InterruptedException {
Thread t1 = new Thread ( new AccountingSyn2 ( ) ) ;
Thread t2 = new Thread ( new AccountingSyn2 ( ) ) ;
t1. start ( ) ;
t2. start ( ) ;
}
}
class AccountingSyn2 implements Runnable {
static AccountingSync instance= new AccountingSync ( ) ;
static int i= 0 ;
public static synchronized void increase ( ) {
i++ ;
}
public void run ( ) {
for ( int j = 0 ; j < 10000000 ; j++ ) {
increase ( ) ;
}
System. out. println ( i) ;
}
}