Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6283
  • 博文数量: 6
  • 博客积分: 1450
  • 博客等级: 上尉
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-24 21:52
文章分类

全部博文(6)

文章存档

2011年(2)

2010年(2)

2009年(2)

我的朋友
最近访客

分类: Java

2011-04-01 16:46:14

运用atomic实现
  1. import java.util.concurrent.atomic.AtomicInteger;
  2. public class AtomicCounter{
  3.     private AtomicInteger value = new AtomicInteger(0);
  4.     public synchronized int getValue(){
  5.         return value.get();
  6.     }
  7.     public synchronized int increment(){
  8.         int intValue;
  9.         int next;
  10.         for(;;){
  11.             intValue = value.get();
  12.             next = intValue + 1;
  13.             if(value.compareAndSet(intValue,next))
  14.                     return next;
  15.         }
  16.     }
  17. }

测试类
  1. import java.util.*;
  2. public class AtomicCounterTest{
  3.     public static void main(String[] args){
  4.         int time = Integer.parseInt(args[0]);
  5.         AtomicCounter counter = new AtomicCounter();
  6.         long start = System.currentTimeMillis();
  7.         for(int i = 0;i < time;i ++){
  8.             new AtomicThread(counter).start();
  9.         }
  10.         long end = System.currentTimeMillis();
  11.         System.out.println(end - start);

  12.     }
  13. }
  14. class AtomicThread extends Thread{
  15.     private AtomicCounter counter ;
  16.     public AtomicThread(AtomicCounter counter){
  17.         this.counter = counter;
  18.     }
  19.     public void run(){
  20.         counter.increment();
  21.     }
  22. }


运用cas实现

  1. public class SimulatedCAS{
  2.     private int value = 0;
  3.     public synchronized int getValue(){
  4.         return value;
  5.     }
  6.     public synchronized int compareAndSwap(int expectValue,int newValue){
  7.         if(expectValue == value){
  8.             value = newValue;
  9.             return expectValue;
  10.         }
  11.         return value;
  12.     }
  13. }


  14. public class CasCounter{
  15.     private SimulatedCAS value = new SimulatedCAS();
  16.     public int getValue(){
  17.         return value.getValue();
  18.     }

  19.     public int increment(){
  20.         int oldValue = value.getValue();
  21.         while(value.compareAndSwap(oldValue,oldValue + 1) != oldValue)
  22.            oldValue = value.getValue();
  23.         return oldValue + 1;
  24.     }
  25. }

测试类
  1. import java.util.*;
  2. public class CasCounterTest{
  3.     public static void main(String[] args){
  4.         int time = Integer.parseInt(args[0]);
  5.         CasCounter counter = new CasCounter();
  6.         long start = System.currentTimeMillis();
  7.         for(int i =0 ;i < time;i ++){
  8.             new CasTestThread(counter).start();
  9.         }
  10.         long end = System.currentTimeMillis();
  11.         System.out.println(end - start);
  12.     }
  13. }

  14. class CasTestThread extends Thread{
  15.     private CasCounter counter;
  16.     public CasTestThread(CasCounter counter){
  17.         this.counter = counter;
  18.     }
  19.     public void run(){
  20.         counter.increment();
  21.     }
  22. }

运用同步实现

  1. public class SynCounter{
  2.     private int value = 0;
  3.     public synchronized int getValue(){
  4.         return value;
  5.     }
  6.     public synchronized int increment(){
  7.         return this.value += 1;
  8.     }
  9. }
测试类
  1. import java.util.*;
  2. public class SynCounterTest{
  3.     public static void main(String[] args){
  4.         int time = Integer.parseInt(args[0]);
  5.         SynCounter counter = new SynCounter();
  6.         long start = System.currentTimeMillis();
  7.         for(int i = 0;i < time;i ++)
  8.         {
  9.             new SynCounterThread(counter).start();
  10.         }
  11.         long end = System.currentTimeMillis();
  12.         System.out.println(end - start);
  13.     
  14.     }
  15. }

  16. class SynCounterThread extends Thread{
  17.     private SynCounter counter;
  18.     public SynCounterThread(SynCounter counter){
  19.         this.counter = counter;
  20.     }
  21.     public void run(){
  22.         this.counter.increment();
  23.     }
  24. }





阅读(159) | 评论(0) | 转发(0) |
0

上一篇:递归向下方法实现算术表达式解析器

下一篇:没有了

给主人留下些什么吧!~~