线程同步
还是那个经典问题卖票
一。各自单线程卖自己的票
public class CellTicket {
public static void main(String[] args) {
Windows w1= new Windows();
Windows w2= new Windows();
Windows w3= new Windows();
w1.start();
w2.start();
w3.start();
}
}
class Windows extends Thread{
private Integer ticket=100;
@Override
public void run() {
while (ticket>0){
System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
ticket--;
}
}
}
二。多线程操作公共的100张票,实现runnable接口的方式实现多线程,在run方法中使用sleep方法,出现售卖负数的票的情况
public class CellTicket {
public static void main(String[] args) {
myRunnables resource= new myRunnables();
Thread w1=new Thread(resource);
Thread w2=new Thread(resource);
Thread w3=new Thread(resource);
w1.start();
w2.start();
w3.start();
}
}
class myRunnables implements Runnable{
private Integer ticket=100;
public void run() {
while (ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
ticket--;
}
}
}
以上两个问题是我们学习多线程中太常见的情况了,说下下面的情况
一)使用继承Thread的方式怎么实现售卖同一个资源的票数,方法将共享资源加上上static关键字
class Windowss extends Thread{
//加static
private static Integer ticket=100;
public void run() {
while (ticket>0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"卖出第"+ticket+"张票");
ticket--;
}
}
}
那么这两种实现共享数据的方式,我们建议采用哪种呢?答案是Runnable接口的方式,因为java是单继承的,如果继承了Thread类就没办法继承别的类了。
出现负票是因为,多个线程同时拿到了同一张票,这个问题的解决就需要我们用到synchronized(同步)
synchronized关键字
synchronized关键字修饰代码块或者方法,称为同步代码块和同步方法
class myRunnables implements Runnable {
private Integer ticket = 100;
Object o=new Object();
public void run() {
while (true) {
synchronized (o) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票");
ticket--;
}
}
}
}
}
解决了卖重票和卖负数的情况
使用synchronized关键字修饰代码块需要一个同步锁:上面代码中用到的同步锁,就是我new的Object对象。
同步锁需要具有以下特征才能实现同步效果:
①可以是任意对象,注意任意的都可以
②该同步锁对象必须是唯一的,多个线程必须使用同一个同步锁对象。当由进程执行方法时,获取同步锁,只有取得锁才能执行逻辑。没有取得就要等待。
以下方法就是锁不是唯一的,导致同步失败,因为每个线程进入执行方法时都新建里一把锁,而不是Runnable接口中的锁是唯一的。
class myRunnables implements Runnable {
private Integer ticket = 100;
public void run() {
Object o=new Object();
while (true) {
synchronized (o) {
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票");
ticket--;
}
}
}
}
}
那么怎么样才能保证同步方法的锁唯一呢?
首先我们实现多线程操作共享资源以共有两种方式:
①继承Thread类使用static关键字修饰共享资源的方式====>用当前的class
类当锁,比如Window.class,引文一个class类型是唯一的。
②实现Runnable接口的方式====>使用当前类this关键字
class myRunnables implements Runnable {
private Integer ticket = 100;
public void run() {
while (true) {
synchronized (this) {
。。。。。。。。。。。。。。。。。。。。。。。。。。
}
}
}
}
class Windowss extends Thread {
private static Integer ticket = 100;
public void run() {
synchronized (Windowss.class) {
。。。。。。。。。。。。。。。。。。。。。。。
}
}
}
}
以上说的是同步代码块实现同步,下面介绍同步方法
一。使用实现Runable接口的方式
class myRunnables implements Runnable {
private Integer ticket = 100;
public void run() {
while (true) {
show();
}
}
public synchronized void show(){
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票");
ticket--;
}
}
}
二。使用继承Thread接口的方式
class Windowss extends Thread {
private static Integer ticket = 100;
public synchronized void run() {
while (true) {
show();
}
}
public static synchronized void show(){
if (ticket > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票");
ticket--;
}
}
}
需要注意的是,使用继承Thread类的方式实现共享相同的资源时,需要在同步方法上加上synchronized关键字,但是使用实现接口的方式却不用,这是为什么呢?
原因就是:在我们使用同步代码块的时候,我们就提到过,实现接口的方式的同步锁可以使用this,这里就是同步方法默认的同步锁就是默认使用this。当然在实现接口的方式,使用this做同步锁没有问题,当使用继承Thread的方式的时候,每一个线程的this代表的对象不是唯一的,同步锁失效。所以需要加上static修饰同步方法。
懒汉式的单例模式实现线程安全
//线程不安全的单例模式
class Singleton{
private static Singleton instance=null;
private Singleton(){}
public static Singleton getInstance(){
if(instance==null){
instance= new Singleton();
}
return instance;
}
}
//同步代码块实现线程安全的单例
class Singleton{
private static Singleton instance=null;
private Singleton(){}
//这里最好的同步锁就是Singleton.class,因为这方法是static的
public static Singleton getInstance(){
synchronized (Singleton.class){
if(instance==null){
instance= new Singleton();
}
return instance;
}
}
}
//同步方法实现线程安全的单例
class Singleton{
private static Singleton instance=null;
private Singleton(){}
//直接方法是static的 方法上加synchronized 默认同步锁是 类.class
public static synchronized Singleton getInstance(){
if(instance==null){
instance= new Singleton();
}
return instance;
}
}
思考: 苹果13发布了,我们去线下店排队买,因为疫情影响,同时店里只能有一个人。这时候店里其实只有一台苹果13,第一个人买完,从后门走了,然后这个时候,人们是挨个进去以后,才知道卖没了从后门出去,其他人继续排队,还是这个时候店员,在门口贴一个告示牌。苹果13卖完了。排队的人拿到这个消息都不等着就离开了呢?
上面的问题不就是卖没了算有人还排对的情况,这个时候在加个if判断就可以了
class Singleton {
private static Singleton instance = null;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
这里是自己的一点小反思:在工作中遇到的多线程和线程安全的问题不多,虽然知道他很重要,就看了课本上的基础的部分,以为多线程就是,Thread,Runnable,run,start,synchronized这些一起同时出现的。直到刚才自己写这个单例模式的时候,思考了以下,其实不是的,他们是相互独立的,又是相互依存的。其中Thread,Runnable,run start可以看作是生成和启动线程的。他们不一定会导致线程安全问题,(当不是多个线程同时操作同一个资源女的时候,就不存在线程安全问题,你多线程去查询,一辈子也不可能不安全),因此我们应该理解他们更接近线程这个本质的地方。至于synchronized是解决线程安全的一种方式,即使代码中不存在Thread,Runnable,run,start,只要有多个人同时操作统一数据的情况,就会有线程安全问题。所以不要混淆,他们一定是同时存在的,这种看法是不对的。synchronized可以单独出现,其他的就是创建启动流程的时候使用。