Java线程:新特征-原子量
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
/**
* Java线程:新特征-原子量
*
* @author leizhimin 2009-11-6 9:53:11
*/
public class Test {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
Runnable t1 = new MyRunnable("张三", 2000);
Runnable t2 = new MyRunnable("李四", 3600);
Runnable t3 = new MyRunnable("王五", 2700);
Runnable t4 = new MyRunnable("老张", 600);
Runnable t5 = new MyRunnable("老牛", 1300);
Runnable t6 = new MyRunnable("胖子", 800);
//执行各个线程
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
pool.execute(t6);
//关闭线程池
pool.shutdown();
}
}
class MyRunnable implements Runnable {
private static AtomicLong aLong = new AtomicLong(10000); //原子量,每个线程都可以自由操作
private String name; //操作人
private int x; //操作数额
MyRunnable(String name, int x) {
this.name = name;
this.x = x;
}
public void run() {
System.out.println(name + "执行了" + x + ",当前余额:" + aLong.addAndGet(x));
}
}
王五执行了2700,当前余额:16300
老张执行了600,当前余额:16900
老牛执行了1300,当前余额:18200
胖子执行了800,当前余额:19000
张三执行了2000,当前余额:21000
Process finished with exit code 0
王五执行了2700,当前余额:18300
老张执行了600,当前余额:18900
老牛执行了1300,当前余额:20200
胖子执行了800,当前余额:21000
李四执行了3600,当前余额:15600
Process finished with exit code 0
李四执行了3600,当前余额:15600
老张执行了600,当前余额:18900
老牛执行了1300,当前余额:20200
胖子执行了800,当前余额:21000
王五执行了2700,当前余额:18300
Process finished with exit code 0
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.atomic.AtomicLong;
/**
* Java线程:新特征-原子量
*
* @author leizhimin 2009-11-6 9:53:11
*/
public class Test {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
Lock lock = new ReentrantLock(false);
Runnable t1 = new MyRunnable("张三", 2000,lock);
Runnable t2 = new MyRunnable("李四", 3600,lock);
Runnable t3 = new MyRunnable("王五", 2700,lock);
Runnable t4 = new MyRunnable("老张", 600,lock);
Runnable t5 = new MyRunnable("老牛", 1300,lock);
Runnable t6 = new MyRunnable("胖子", 800,lock);
//执行各个线程
pool.execute(t1);
pool.execute(t2);
pool.execute(t3);
pool.execute(t4);
pool.execute(t5);
pool.execute(t6);
//关闭线程池
pool.shutdown();
}
}
class MyRunnable implements Runnable {
private static AtomicLong aLong = new AtomicLong(10000); //原子量,每个线程都可以自由操作
private String name; //操作人
private int x; //操作数额
private Lock lock;
MyRunnable(String name, int x,Lock lock) {
this.name = name;
this.x = x;
this.lock = lock;
}
public void run() {
lock.lock();
System.out.println(name + "执行了" + x + ",当前余额:" + aLong.addAndGet(x));
lock.unlock();
}
}
王五执行了2700,当前余额:14700
老张执行了600,当前余额:15300
老牛执行了1300,当前余额:16600
胖子执行了800,当前余额:17400
李四执行了3600,当前余额:21000
Process finished with exit code 0
本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/222541