解决线程安全问题

在进行线程代码的实现时会存在部分安全问题
比如下列的售票例子

public class DemoMyThread {
	public static void main(String[] args) {
		Runnable run = new Runnableimpl(); // 多态
		//开放三个线程
		new Thread(run).start();
		new Thread(run).start();
		new Thread(run).start();
	}
}

class Runnableimpl implements Runnable {
	private int ticket = 100;

	@Override
	public void run() {
		while (true) {
			if (ticket > 0) {
				try {
					Thread.sleep(10);
					System.out.println(Thread.currentThread().getName() + "正在售出第" + ticket + "张票");
				ticket--;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

代码的输出就出现了重复售票和售出不存在的票等安全问题

要解决线程安全问题有三种方法:

1.同步代码块
在实现了共享数据的代码前创建一个同步代码块的锁对象
锁对象的作用:
把同步代码块锁住,只让一个线程在同步代码块中执行。

public class DemoMyThread {
	public static void main(String[] args) {
		Runnable run = new Runnableimpl(); // 多态
		//开放三个线程
		new Thread(run).start();
		new Thread(run).start();
		new Thread(run).start();
	}
}

class Runnableimpl implements Runnable {
	private int ticket = 100;
	//创建一个锁对象
	Object obj = new Object();
	@Override
	public void run() {
		while (true) {
			//同步代码块
			synchronized (obj) {
			if (ticket > 0) {
				try {
					Thread.sleep(10);
					System.out.println(Thread.currentThread().getName() + "正在售出第" + ticket + "张票");
				ticket--;
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
  }
}

2.同步方法
把访问了共享数据的代码抽取出来放在一个方法中

package Thread;

public class Demo04 {
	public static void main(String[] args) {
		Runnable run = new Runnableimpl(); // 多态
		// 开放三个线程
		new Thread(run).start();
		new Thread(run).start();
		new Thread(run).start();
	}
}

class Runnableimpl implements Runnable {
	private int ticket = 100;

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

	public synchronized void soldTicket() {
		if (ticket > 0) {
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + "正在售出第" + ticket + "张票");
			ticket--;
		}
	}
}

3.Lock锁
在成员位置创建一个ReentrantLock对象,再调用接口中的lock方法和unlock方法对代码进行锁的获取和释放

package Thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Demo04 {
	public static void main(String[] args) {
		Runnable run = new Runnableimpl(); // 多态
		// 开放三个线程
		new Thread(run).start();
		new Thread(run).start();
		new Thread(run).start();
	}
}

class Runnableimpl implements Runnable {
	private int ticket = 100;
	//在成员位置创建一个ReentrantLock对象
	Lock l = new ReentrantLock();
	@Override
	public void run() {
		while (true) {
			//获取锁
			l.lock();
				if (ticket > 0) {
					try {
						Thread.sleep(10);
						System.out.println(Thread.currentThread().getName() + "正在售出第" + ticket + "张票");
						ticket--;
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}finally {
						//释放锁
						l.unlock();
					}
				}
			}
		}
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值