同步方法解决内存安全问题(同步卖票案例)

共有100张票,4个窗口同时卖票

1、是多线程环境

2、有共享数据

3、有多条语句操作数据

满足三个条件会出现安全问题

解决办法:

同步代码块

synchronized(任意对象){

多条语句操作共享数据的代码

}

public class ylh1 implements Runnable {
    private static int tickets = 100;
   Object o=new Object();

    @Override
    public void run() {

        while (true) {
                synchronized (o) {
                    if (tickets > 0) {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
                        tickets--;
                    }
                }

        }

    }

}

public class demo {
    public static void main(String[] args) {
        ylh1 y=new ylh1();

        Thread t1=new Thread(y,"窗口1");
        Thread t2=new Thread(y,"窗口2");
        Thread t3=new Thread(y,"窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}

 

 

 

不会出现重复卖票了 

 同步方法:

就是把synchronized关键字加到方法上

package com;

public class ylh1 implements Runnable {
    private static int tickets = 100;
   Object o=new Object();
   int a=0;

        @Override
        public void run() {

            while (true) {


                if(a%2==0){

                    synchronized (o) {
                        if (tickets > 0) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
                            tickets--;
                        }
                    }
                }
                else {
                    sell();
                }
                a++;
            }

        }
        private static synchronized void sell () {

            if (tickets > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
                tickets--;

            }
        }
}

出现问题,因为同步代码块锁的的o对象

非静态同步方法的锁对象是this

所以将o改为this即可解决

静态同步方法的锁对象是类的字节码文件

如下:

package com;

public class ylh1 implements Runnable {
    private static int tickets = 100;
   Object o=new Object();
   int a=0;

        @Override
        public void run() {

            while (true) {


                if(a%2==0){

                    synchronized (ylh1.class) {
                        if (tickets > 0) {
                            try {
                                Thread.sleep(100);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
                            tickets--;
                        }
                    }
                }
                else {
                    sell();
                }
                a++;
            }

        }
        private static synchronized void sell () {

            if (tickets > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + "正在售票第" + tickets + "张");
                tickets--;

            }
        }
}

public class demo {
    public static void main(String[] args) {
        ylh1 y=new ylh1();

        Thread t1=new Thread(y,"窗口1");
        Thread t2=new Thread(y,"窗口2");
        Thread t3=new Thread(y,"窗口3");
        t1.start();
        t2.start();
        t3.start();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值