Java线程同步(含详细解释/代码实例)

一、为解决线程安全问题,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”获得监视权后执行同步代码块,这就实现了两个线程对共享资源操作的同步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪&

感谢您的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值