一、为解决线程安全问题,Java引入监视器(monitor)来保证共享数据的
同步性
。任何对象都可作为一个监视器,关键词synchronized修饰某个对象后,该对象就成为监视器。二、
同步代码块只能有一个线程独占执行。
三、同步代码块作用:多个线程第共享资源操作容易引起冲突,这些容易引起冲突的代码块称之为临界区,在临界区通过引入监视器,并用synchronized使多个线程在临界区同步起来,从而避免可能引起的冲突。
四、Synchronized的三种用法:
1、synchronized代码块:监视器为指定的对象。
2、synchronized方法:监视器为this对象。
3、synchronized静态方法:监视器为相应的类。
一、关键词synchronized加在run方法前
代码展示:
class Resource implements Runnable{
    volatile public int i;
    public Resource(int _i){
        i=_i;
    }
    public synchronized void run(){ //关键词synchronized加在run方法前
        while(true){
                if(i>0){
                    try{Thread.sleep(200);}
                    catch (Exception e){}
                        i--;
                        System.out.println(Thread.currentThread().getName()+" "+i);
                    }
                else{
                    System.out.println(Thread.currentThread().getName());
                    break;
                }
        }
    }
}
public class TestSecurity {
    public static void main(String[] args){
        Resource m=new Resource(4);
        Thread t1=new Thread(m);
        Thread t2=new Thread(m);
        t1.start();
        t2.start();
    }
}运行结果如下:
    
   
    补充说明:本程序仅Thread-0完成了i的自减过程,因
    
     关键字synchronized加在run()方法前
    
    ,监视器为Resource对象。系统调度的结果使
    
     Thread-0在此刻先获得了Resource监视权,在没有完成Resource对象run方法调用前,Thread-1线程无法获得该监视器所有权
    
    。
   
二、synchronized代码块
代码展示:
class Resource1 implements Runnable{
    volatile public int i;
    volatile public Integer it;
    public Resource1(int _i){
        i=_i;
        it=new Integer(i);//new 一个Integer对象的时候,实际上是生成了一个指针指向此对象;
    }
    public  void run(){
        while(true){
            synchronized (it) {//synchronized代码块
                if (i > 0) {
                    try {
                        Thread.sleep(200);
                    } 
                    catch (Exception e) {}
                    i--;
                    System.out.println(Thread.currentThread().getName() + " " + i);
                }
                else {
                    System.out.println(Thread.currentThread().getName());
                    break;
                }
            }
        }
    }
}
public class TestSecurity1 {
    public static void main(String[] args){
        Resource1 m=new Resource1(4);
        Thread t1=new Thread(m);
        Thread t2=new Thread(m);
        t1.start();
        t2.start();
    }
}运行结果如下:
    
   
    补充说明:
    
     此代码增加了一个Resource对象的属性it,它引用一个对象,此对象充当监视器,用synchronized(it)表示,并构成一个同步代码块。
    
    当“Thread-1”执行到synchronized(it)代码块时,其获得了该监视权,“Thread-1”未执行完同步代码,“Thread-0”因无法获得监视权而
    
     不能进入
    
    同步代码块。“Thread-1”执行完同步代码块后,释放监视权,“Thread-0”获得监视权后执行同步代码块,这就实现了两个线程对共享资源操作的同步。
   
 
