Java线程安全—synchronized

 1.同步代码块

同步代码块:

synchronized(同步监视器){

    需要被同步的代码

说明:

1.多线程安全主要针对共享数据,多个线程共同操作相同的数据;

2.同步监视器:锁,可以是任意一个类的对象。

   注意的:要求多个线程共用一个锁。

补充:在实现Rannable 接口创建的多线程方式中,我们考虑使用this作为同步监视器。

2.在继承Thread类创建多线程的方式中,慎用this当监视器,考虑用当前类作为监视器,MyThread.class   (类的属性)

//示例: 售卖100张门票,实现多个窗口售卖
//线程安全synchronized :同步代码块
// 1.实现Runna接口
public class ThreadDemo1 implements  Runnable{

    private static int ticketNum = 100;// 共有100张门票
    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (ticketNum > 0) {
                    System.out.println(Thread.currentThread().getName() + "售卖的门票号:" + ticketNum);
                    ticketNum--;
                }
            }
            if (ticketNum <= 0) {
                return;//为了减少synchronized 中代码,结束判断放在外部
            }
        }
    }

    public static void main(String[] args) {
        //所有的线程共用一个锁,因为都是通过一个Ticket 对象创建的线程,因此用this作为锁
        Ticket thread = new Ticket();
        Thread t1 = new Thread(thread);
        t1.start();
        Thread t2 = new Thread(thread);
        t1.start();
        Thread t3 = new Thread(thread);
        t1.start();
    }

继承Thread实现多线程,同步代码块实现线程安全

public class ThreadDemo2 extends  Thread {

    public ThreadDemo2(String name)
    { super(name);}
    private static int ticketNum = 100;// 共有100张门票

    @Override
    public void run() {
        while (true) {
            synchronized (ThreadDemo2.class) {
                if (ticketNum > 0) {
                    System.out.println(Thread.currentThread().getName() + "售卖的门票号:" + ticketNum);
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    ticketNum--;
                }
            }
            if (ticketNum <= 0) {
                return;//为了减少synchronized 中代码,结束判断放在外部
            }
        }
    }



    public static void main(String[] args) {
        //所有的线程共用一个锁,因为都是通过一个Ticket 对象创建的线程,因此用this作为锁
        ThreadDemo2 t1 = new ThreadDemo2("窗口1");
        t1.start();
        ThreadDemo2 t2 = new ThreadDemo2("窗口2");
        t2.start();
        ThreadDemo2 t3 = new ThreadDemo2("窗口3");
        t3.start();
    }
}

 

 线程安全:方法二 同步方法

synchronized修饰的方法,没有显示同步监视器,实际是存在的,

如果是非静态类,默认同步监视器是this;

如果是静态类,默认同步监视器是类

//示例: 售卖100张门票,实现多个窗口售卖
//线程安全synchronized :同步代码块
// 1.实现Runna接口
public class ThreadDemo1 implements Runnable {

    private static int ticketNum = 100;// 共有100张门票

    @Override
    public void run() {
        while (true) {
            buyTicket();
        }
    }

    public synchronized void buyTicket()  {
        if (ticketNum > 0) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(Thread.currentThread().getName() + "售卖的门票号:" + ticketNum);
            ticketNum--;
        }
    }

    public static void main(String[] args) {
        //所有的线程共用一个锁,因为都是通过一个Ticket 对象创建的线程,因此用this作为锁
        ThreadDemo1 thread = new ThreadDemo1();

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

 

实现Runnable 接口,同步方法实现线程安全

 

 

/示例: 售卖100张门票,实现多个窗口售卖
//线程安全synchronized :同步代码块
// 1.实现Runna接口
public class ThreadDemo2 extends  Thread {

    public ThreadDemo2(String name)
    { super(name);}
    private static int ticketNum = 100;// 共有100张门票

    @Override
    public void run() {
        while (true) {
            ThreadDemo2.buyTicket();
        }
    }
//    同步监视器是类:静态方法
    public synchronized static void buyTicket(){

            if (ticketNum > 0) {
                System.out.println(Thread.currentThread().getName() + "售卖的门票号:" + ticketNum);
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                ticketNum--;
            }
    }





    public static void main(String[] args) {
        //所有的线程共用一个锁,因为都是通过一个Ticket 对象创建的线程,因此用this作为锁
        ThreadDemo2 t1 = new ThreadDemo2("窗口1");
        t1.start();
        ThreadDemo2 t2 = new ThreadDemo2("窗口2");
        t2.start();
        ThreadDemo2 t3 = new ThreadDemo2("窗口3");
        t3.start();

}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值