java线程安全问题

线程笔记

import sun.applet.Main;

import java.security.PrivateKey;

/**
 * @author
 * @create 2021-01-14 22:35
 */
class ThreadDemo implements Runnable {

    private int ticket = 100;

    @Override
    public void run() {
        while (true) {
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + ":卖票票号为" + ticket);
                ticket--;
            } else {
                break;

            }
        }

    }
}

class ThreadText {
    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo();
        Thread thread1 = new Thread(threadDemo);
        Thread thread2 = new Thread(threadDemo);
        Thread thread3 = new Thread(threadDemo);
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();

    }

}

方式一 同步代码块

synchronized (同步监视器){
        //需要被同步的代码
    }
 @Override
    public void run() {
        while (true) {

            synchronized (ThreadDemo.class){
                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + ":卖票票号为" + ticket);
                    ticket--;
                } else {
                    break;

                }
            }


        }

    }
  • 操作共享数据的代码,即为同步代码,不能包裹多也能包裹少
  • 同步监视器:俗称所lock,任何类的对象都可以充当锁,
  • 要求:多个线程必须共用一把锁

方式二 同步方法

同步方法仍然涉及同步监视器,只是不需要显示声明

package com.company;

class ThreadDemo implements Runnable{
    private int ticket = 100;

    @Override
    public void run() {
        while (ticket>0) {
           showTicket();
        }
    }
//同步方法
    private synchronized void showTicket() {
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + ":票号" + ticket);
            ticket--;
        }
    }
}


public class Main {
    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo();
        Thread thread1 = new Thread(threadDemo);
        Thread thread2 = new Thread(threadDemo);
        Thread thread3 = new Thread(threadDemo);
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

jdk1.5解决线程安全问题lock方式

package com.company;
import java.util.concurrent.locks.ReentrantLock;

class ThreadDemo implements Runnable{
    private int ticket = 100;
    //实例化 ReentrantLock,使用锁, true表示公平锁,可不加
    private ReentrantLock lock = new ReentrantLock(true);
    
    @Override
    public void run() {
        while (ticket>0) {
            try {
                //用锁
                lock.lock();

                if (ticket > 0) {
                    System.out.println(Thread.currentThread().getName() + ":票号" + ticket);
                    ticket--;
                } else {
                    break;
                }
            } finally {
                //解锁
                lock.unlock();
            }
       }
    }

}
public class Main {

    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo();
        Thread thread1 = new Thread(threadDemo);
        Thread thread2 = new Thread(threadDemo);
        Thread thread3 = new Thread(threadDemo);
        thread1.setName("窗口1");
        thread2.setName("窗口2");
        thread3.setName("窗口3");
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

线程通信交替打印

notify()唤醒线程
wait() 阻塞,并且释放锁,只能在同步代码块中使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

yzhSWJ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值