2019年8月26日 星期一

[Java程式]_Ch14.3_同步問題的處理



歡迎訂閱頻道:杜仲教學YouTube
https://www.youtube.com/channel/UCElwt6s8Ea_V-gnu9sZZT5g?sub_confirmation=1
杜仲教學部落格:
https://derekcctu.blogspot.com/

1 則留言:

  1. class Bank{
    static int sum;
    synchronized static void add(int n){
    int tmp=sum;
    tmp=tmp+n;
    try {
    Thread.sleep((int)(Math.random()*1000));
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    sum=tmp;
    Thread t=Thread.currentThread();
    System.out.println("sum="+sum+" 當前執行緒:"+t.getName());
    }
    }
    class Customer implements Runnable{

    @Override
    public void run() {
    for(int i=1;i<=3;i++){
    Bank.add(100);
    }
    }
    }
    public class A14_1 {
    public static void main(String[] args) {
    Customer c1=new Customer();
    Customer c2=new Customer();
    Thread t1=new Thread(c1,"客戶1");
    Thread t2=new Thread(c2,"客戶2");
    t1.start();
    t2.start();
    }

    }

    回覆刪除

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...