Java Semaphore 信号量

来源:互联网 发布:淘宝有客服投诉电话 编辑:程序博客网 时间:2024/04/27 01:48

题目:
你的现有的银行有200美元
交易1 从银行取出100美元
交易2 往银行存20美元
两个操作同时进行

不用信号量semaphore的java代码:

package com.os;public class Withdraw implements Runnable{    private static int totalMoney = 200;    public static int getTotalMoney() {        return totalMoney;    }    public static void setTotalMoney(int totalMoney) {        Withdraw.totalMoney = totalMoney;    }    public Withdraw() {        // TODO Auto-generated constructor stub    }    @Override    public void run() {        // TODO Auto-generated method stub            try {                Thread.sleep(1000);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            setTotalMoney(totalMoney - 20);            System.out.println("withdrow: " + getTotalMoney());    }}
package com.os;public class Deposit implements Runnable{    Withdraw withdrow = new Withdraw();    @Override    public void run() {        // TODO Auto-generated method stub        try {            Thread.sleep(1000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        withdrow.setTotalMoney(withdrow.getTotalMoney() + 100);        System.out.println("deposit: "  + withdrow.getTotalMoney());    }}
package com.os;public class ThreadTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        Withdraw withdrow = new Withdraw();        Deposit deposit = new Deposit();        Thread t1 = new Thread(withdrow);        Thread t2 = new Thread(deposit);            t1.start();        t2.start();        try {            t1.join();            t2.join();        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("Finally total money: "  + withdrow.getTotalMoney());    }}

应用信号量semaphore的Java代码:

package com.os.semaphore;public class Withdraw implements Runnable {    private static int totalMoney = 200;    SemaphoreUtil su = new SemaphoreUtil();    public static int getTotalMoney() {        return totalMoney;    }    public static void setTotalMoney(int totalMoney) {        Withdraw.totalMoney = totalMoney;    }    public Withdraw() {        // TODO Auto-generated constructor stub    }    @Override    public void run() {        // TODO Auto-generated method stub        // 取钱操作        try {            su.getSemaphore().acquire();            Thread.sleep(1000);        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        setTotalMoney(totalMoney - 20);        System.out.println("withdrow: " + getTotalMoney());        su.getSemaphore().release();    }}
package com.os.semaphore;public class Deposit implements Runnable {    Withdraw withdrow = new Withdraw();    SemaphoreUtil su = new SemaphoreUtil();    @Override    public void run() {        // 存钱操作        try {            su.getSemaphore().acquire();            Thread.sleep(1000);        } catch (InterruptedException e) {            e.printStackTrace();        }        withdrow.setTotalMoney(withdrow.getTotalMoney() + 100);        System.out.println("deposit: " + withdrow.getTotalMoney());        su.getSemaphore().release();    }}
package com.os.semaphore;import java.util.concurrent.Semaphore;public class SemaphoreUtil {    // 声明和定义信号量实例    private static Semaphore semaphore = new Semaphore(1, true);    // 获取信号量实例变量    public Semaphore getSemaphore() {        return semaphore;    }}
package com.os.semaphore;public class ThreadTest {    public static void main(String[] args) {        // TODO Auto-generated method stub        Withdraw withdraw = new Withdraw();        Deposit deposit = new Deposit();        // 声明withdraw线程实例        Thread t1 = new Thread(withdraw);        // 声明deposit线程实例        Thread t2 = new Thread(deposit);        t1.start();        t2.start();        try {            t1.join();            t2.join();        } catch (InterruptedException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println("Finally total money: " + withdraw.getTotalMoney());    }}
0 0
原创粉丝点击