实现线程安全的三种方式

实现线程安全的三种方式

一、Synchronized

1. 同步方法


public class MyThread extends Thread{
    private int ticket = 10000;

	//重写run()方法
    public void run(){
        while (ticket > 0){
            this.getTicket();
        }

    }
	//非静态的同步方法同步监视器为this(类的对象)
	//静态的同步方法同步监视器为类本身(MyThread.Class)
    public synchronized void  getTicket(){
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "--got ticket with no." + ticket);
                ticket--;
            }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread thread1 = new Thread(myThread);
        Thread thread2 = new Thread(myThread);
        Thread thread3 = new Thread(myThread);
        thread1.setName("用户1");
        thread2.setName("用户2");
        thread3.setName("用户3");
        //调用start()方法开启多线程执行
        thread1.start();
        thread2.start();
        thread3.start();
    }

1. 同步代码块

public class MyThread2 extends Thread {
    private int ticket = 10000;
    //重写run()方法
    public void run(){
        while (ticket > 0) {
            synchronized (this) {
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "--got ticket with no." + ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }

    }
    public static void main(String[] args) {
        MyThread2 myThread2 = new MyThread2();
        Thread thread1 = new Thread(myThread2);
        Thread thread2 = new Thread(myThread2);
        Thread thread3 = new Thread(myThread2);
        thread1.setName("用户1");
        thread2.setName("用户2");
        thread3.setName("用户3");
        //调用start()方法开启多线程执行
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

二、手动Lock


public class MyThread3 implements Runnable {
    private int ticket = 100;
    //启用公平锁
    private ReentrantLock lock = new ReentrantLock(true);

    @Override
    public void run() {
        while (ticket > 0) {
            try {
            	//手动上锁
                lock.lock();
                if (ticket > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "--got ticket with no." + ticket);
                    ticket--;
                } else {
                    break;
                }
            } finally {
            	//手动释放锁
                lock.unlock();
            }
        }
    }

    public static void main(String[] args) {
        MyThread3 myThread3 = new MyThread3();
        Thread thread1 = new Thread(myThread3);
        Thread thread2 = new Thread(myThread3);
        Thread thread3 = new Thread(myThread3);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值