第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

模擬多線程轉(zhuǎn)賬demo

標(biāo)簽:
Java
package com.chengxi.multithread.transfermoney;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Author: CHENGXI
 */
public class MyTransfer {
    public static void main(String[] args) {
        Account src = new Account(10000);
        Account target = new Account(10000);
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                src.transactionToTarget(1, target);
                countDownLatch.countDown();
            }).start();
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("src = " + src.getBalance());
        System.out.println("target = " + target.getBalance());
    }
}

// 单例的资源管理对象
class Allocator {
    private Allocator() {}
    public static Allocator getInstance() {
        return InnerAllocator.instance;
    }

    static class InnerAllocator {
        private static final Allocator instance = new Allocator();
    }

    private List<Account> locks = new ArrayList<>();

    // 一次申请所有资源
    public synchronized void apply(Account src, Account tag) {
        System.out.println(Thread.currentThread().getName() + " 拿到获取资源的锁");
        while (locks.contains(src) || locks.contains(tag)) {
            try {
                System.out.println(Thread.currentThread().getName() + " 条件队列已有资源, 开始等待");
                this.wait();
                System.out.println(Thread.currentThread().getName() + " 已被唤醒并拿到锁, 继续执行");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread() + " 往条件队列里面添加资源");
        locks.add(src);
        locks.add(tag);
        System.out.println(Thread.currentThread().getName() + " 资源添加完成");
        System.out.println();
    }

    // 归还所有资源
    public synchronized void release(Account src, Account tag) {
        System.out.println(Thread.currentThread().getName() + " 转账完成, 释放资源, 并唤醒所有线程");
        locks.remove(src);
        locks.remove(tag);
        this.notifyAll();
        System.out.println();
    }
}

class Account {
    private Integer balance;
    public Account(Integer balance) {
        this.balance = balance;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    // 转账方法
    public void transactionToTarget(Integer money, Account target) {
        System.out.println(Thread.currentThread().getName() + " 尝试获取Allocator单例");
        Allocator.getInstance().apply(this, target);
        System.out.println(Thread.currentThread().getName() + " 获取到单例对象和资源, 开始转账");
        this.balance -= money;
        target.setBalance(target.getBalance() + money);
        Allocator.getInstance().release(this, target);
    }
}

输出结果:

Thread-0 尝试获取Allocator单例

Thread-3 尝试获取Allocator单例

Thread-1 尝试获取Allocator单例

Thread-2 尝试获取Allocator单例

Thread-4 尝试获取Allocator单例

Thread-2 拿到获取资源的锁

Thread[Thread-2,5,main] 往条件队列里面添加资源

Thread-2 资源添加完成


Thread-2 获取到单例对象和资源, 开始转账

Thread-0 拿到获取资源的锁

Thread-0 条件队列已有资源, 开始等待

Thread-3 拿到获取资源的锁

Thread-3 条件队列已有资源, 开始等待

Thread-1 拿到获取资源的锁

Thread-1 条件队列已有资源, 开始等待

Thread-4 拿到获取资源的锁

Thread-4 条件队列已有资源, 开始等待

Thread-2 转账完成, 释放资源, 并唤醒所有线程


Thread-4 已被唤醒并拿到锁, 继续执行

Thread[Thread-4,5,main] 往条件队列里面添加资源

Thread-4 资源添加完成


Thread-4 获取到单例对象和资源, 开始转账

Thread-1 已被唤醒并拿到锁, 继续执行

Thread-1 条件队列已有资源, 开始等待

Thread-3 已被唤醒并拿到锁, 继续执行

Thread-3 条件队列已有资源, 开始等待

Thread-0 已被唤醒并拿到锁, 继续执行

Thread-0 条件队列已有资源, 开始等待

Thread-4 转账完成, 释放资源, 并唤醒所有线程


Thread-0 已被唤醒并拿到锁, 继续执行

Thread[Thread-0,5,main] 往条件队列里面添加资源

Thread-0 资源添加完成


Thread-0 获取到单例对象和资源, 开始转账

Thread-3 已被唤醒并拿到锁, 继续执行

Thread-3 条件队列已有资源, 开始等待

Thread-1 已被唤醒并拿到锁, 继续执行

Thread-1 条件队列已有资源, 开始等待

Thread-0 转账完成, 释放资源, 并唤醒所有线程


Thread-1 已被唤醒并拿到锁, 继续执行

Thread[Thread-1,5,main] 往条件队列里面添加资源

Thread-1 资源添加完成


Thread-1 获取到单例对象和资源, 开始转账

Thread-3 已被唤醒并拿到锁, 继续执行

Thread-3 条件队列已有资源, 开始等待

Thread-1 转账完成, 释放资源, 并唤醒所有线程


Thread-3 已被唤醒并拿到锁, 继续执行

Thread[Thread-3,5,main] 往条件队列里面添加资源

Thread-3 资源添加完成


Thread-3 获取到单例对象和资源, 开始转账

Thread-3 转账完成, 释放资源, 并唤醒所有线程


src = 9995

target = 10005



點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評論
感謝您的支持,我會繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號

舉報(bào)

0/150
提交
取消