java线程池的例子

用java线程池模拟存取款操:

1、定义账户信息:

package thread;

class MyCount {
   private String oid; // 账号
    private int cash; // 账户余额

    MyCount(String oid, int cash) {
	this.oid = oid;
	this.cash = cash;
    }

    /**
     * 存款
     * 
     * @param x
     *            操作金额
     * @param name
     *            操作人
     */
    public synchronized void saving(int x, String name) {
	if (x > 0) {
	    cash += x; // 存款
	    System.out.println(name + "存款" + x + ",当前余额为" + cash);
	}
	notifyAll(); // 唤醒所有等待线程。
    }

    /**
     * 取款
     * 
     * @param x
     *            操作金额
     * @param name
     *            操作人
     */
    public synchronized void drawing(int x, String name) {
	if (cash - x < 0) {
	    try {
		wait();
	    } catch (InterruptedException e1) {
		e1.printStackTrace();
	    }
	} else {
	    cash -= x; // 取款
	    System.out.println(name + "取款" + x + ",当前余额为" + cash);
	}
	notifyAll(); // 唤醒所有存款操作
    }

}

2、存款操作线程:

package thread;

public class DrawThread extends Thread {

    private String name; // 操作人
    private MyCount myCount; // 账户
    private int x; // 存款金额

    DrawThread(String name, MyCount myCount, int x) {
	this.name = name;
	this.myCount = myCount;
	this.x = x;
    }

    public void run() {
	myCount.drawing(x, name);
    }

}


3、取款操作线程:

package thread;

public class SaveThread extends Thread {

    private String name; // 操作人
    private MyCount myCount; // 账户
    private int x; // 存款金额

    SaveThread(String name, MyCount myCount, int x) {
	this.name = name;
	this.myCount = myCount;
	this.x = x;
    }

    public void run() {
	myCount.saving(x, name);
    }

}


4、使用线程池模拟操作:


 

package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {
    public static void main(String[] args) {
         public static final int POOL_SIZE = 4;	
         // 创建并发访问的账户
	MyCount myCount = new MyCount("95599200901215522", 10000);
	// 创建一个线程池
	ExecutorService pool = Executors.newFixedThreadPool(Runtime
                         .getRuntime().availableProcessors() * POOL_SIZE);//利用机器CPU数量初始化线程池容量
	Thread t1 = new SaveThread("张三", myCount, 2000);
	Thread t2 = new SaveThread("李四", myCount, 3600);
	Thread t3 = new DrawThread("王五", myCount, 2700);
	Thread t4 = new SaveThread("老张", myCount, 600);
	Thread t5 = new DrawThread("老牛", myCount, 1300);
	Thread t6 = new DrawThread("胖子", myCount, 800);
	// 执行各个线程
	pool.execute(t1);
	pool.execute(t2);
	pool.execute(t3);
	pool.execute(t4);
	pool.execute(t5);
	pool.execute(t6);
	// 关闭线程池
	pool.shutdown();
    }

}

 

你可能感兴趣的:(Java)