多线程之线程安全问题

多线程之线程安全问题

使用同步代码块方式

class WindowsSynchronized extends Thread {
    private static int ticket = 100;

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

public class ThreadTestSynchronized {
    public static void main(String[] args) {
        WindowsSynchronized windows1 = new WindowsSynchronized();
        WindowsSynchronized windows2 = new WindowsSynchronized();
        WindowsSynchronized windows3 = new WindowsSynchronized();

        windows1.setName("A1:");
        windows2.setName("A2:");
        windows3.setName("A3:");

        windows1.start();
        windows2.start();
        windows3.start();

    }
}

*使用同步方法方式

class WindowsSynchronizedFuncion extends Thread {
    private static int ticket = 100;

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

    public static synchronized void show() {
        synchronized (WindowsSynchronized.class) {
            if (ticket > 0) {
                System.out.println(Thread.currentThread().getName() + ":卖票,票号为: " + ticket);
                ticket--;
            }
        }
    }
}

public class ThreadSynchronizedFunction {
    public static void main(String[] args) {
        Thread thread1 = new WindowsSynchronizedFuncion();
        Thread thread2 = new WindowsSynchronizedFuncion();
        Thread thread3 = new WindowsSynchronizedFuncion();

        thread1.setName("窗口1:");
        thread2.setName("窗口2:");
        thread3.setName("窗口3:");

        thread1.run();
        thread2.run();
        thread3.run();

    }
}

*使用lock 锁方式

class Account {

    private double balance;
    private ReentrantLock lock = new ReentrantLock();

    public Account(double balance) {
        this.balance = balance;
    }

    public void deposit(double amt) {

        lock.lock();
        try {
            if (amt > 0) {
                balance += amt;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ":存钱成功。余额为:" + balance);
            }
        } finally {
            lock.unlock();
        }

    }

}

class Customer implements Runnable {

    Account account = new Account(0);

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            account.deposit(1000);
        }
    }
}

public class AccountTest {
    public static void main(String[] args) {
        Customer customer = new Customer();

        Thread thread1 = new Thread(customer);
        Thread thread2 = new Thread(customer);

        thread1.setName("甲");
        thread2.setName("乙");

        thread1.start();
        thread2.start();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值