Java中解决线程安全的三种方式

解决线程安全的三种方式

方法一: 同步代码块-----锁对象可以是任意对象

			synchronized ( 锁对象 ) {
					你需要的逻辑	
											 }
//定义一个类sellTicket1实现Runnab1e接口
public class sellTicket1 implements Runnable {
  
    int ticket = 100;
			//定义一个锁对象
    public static final Object lock = new Object();
//重写run()方法
    @Override
    public void run() {
        while (true) {
        //同步代码块
            synchronized (lock) {
            
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票");
                    ticket--;
                }
            }
        }
    }
}

class TextTicket1 {
    public static void main(String[] args) {
  	//创建sellTicket1类对象
        sellTicket1 truck = new sellTicket1();
	//创建Thread类的对象,把sellTicket1对象作为参数,后面是线程名称
        Thread thread = new Thread(truck, "窗口一");
        Thread thread1 = new Thread(truck, "窗口二");
        Thread thread2 = new Thread(truck, "窗口三");
	//调用start方法,启动线程
        thread.start();
        thread1.start();
        thread2.start();
    }
}

方法二: 同步方法-----锁对象时this,即本类对象

               修饰符  synchronized 返回值类型 方法名(方法参数)
               {
                            需要同步执行的代码
                }
public class sellTicket2 implements Runnable{
        int ticket = 100;
        
        //重写run()方法
        @Override
        public void run() {
            while (true) {
                   sellTickets();
            }
        }
			//同步方法
        public synchronized void sellTickets(){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票");
                ticket--;
            }
        }
}

    class TextTicket2 {
        public static void main(String[] args) {
      //创建sellTicket2类对象
            sellTicket2 truck = new sellTicket2();
	//创建Thread类的对象,把sellTicket2对象作为参数,后面是线程名称
            Thread thread = new Thread(truck, "窗口一");
            Thread thread1 = new Thread(truck, "窗口二");
            Thread thread2 = new Thread(truck, "窗口三");
		//调用start方法,启动线程
            thread.start();
            thread1.start();
            thread2.start();
        }

方法三:------lock锁(锁对象是Lock接口对象)

           Lock lock = new ReentrantLock();
public class sellTicket3 implements Runnable{
     
        int ticket = 100;
    Lock lock = new ReentrantLock();
//重写run()方法
    @Override
    public void run() {
        while (true) {
        
			//上锁
            lock.lock();
            if (ticket > 0) {
                try {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票");
                    ticket--;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                
					//释放锁
                    lock.unlock();
                }
            }
        }
    }
}

    class TextTicket3 {
        public static void main(String[] args) {
         //创建sellTicket3类对象
            sellTicket3 truck = new sellTicket3();
		//创建Thread类的对象,把对象truck作为参数,后面是线程名称
            Thread thread = new Thread(truck, "窗口一");
            Thread thread1 = new Thread(truck, "窗口二");
            Thread thread2 = new Thread(truck, "窗口三");
		//调用start方法,启动线程
            thread.start();
            thread1.start();
            thread2.start();
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

清淡的粥

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值