StringBuffer与StringBuilder的线程安全性验证

线程安全性是指,当对一个复杂对象进行某种操作时,从操作开始到操作结束之前,该对象会经历若干中间状态,直到操作完全结束,该对象才会会到完全可用的状态。如果其他线程能够访问处于不可用中间状态的对象,使该对象产生无法预料的结果,则称该对象线程不安全,反之则称其为线程安全。
 
所以这个简单程序的考察点:
  • 线程安全性的理解
  • Java并发编程基础
  • 主动思考和分析能力,以及去求证的主动性,而不是被动接受各种结论

 

示例程序 下载如下:
Java代码    收藏代码
  1. public class StringBufferAndStringBuilderTest {  
  2.   
  3.     private static final int THREAD_NUM = 1000;  
  4.   
  5.     public static void main(String[] args){  
  6.         long startTime = System.currentTimeMillis();  
  7.         String strToReverse = "AAAABBBB";  
  8.   
  9.         StringBuffer stringBuffer = new StringBuffer(strToReverse);  
  10.         StringBuilder stringBuilder = new StringBuilder(strToReverse);  
  11.         CountDownLatch countDownLatch = new CountDownLatch(THREAD_NUM);  
  12.         CountDownLatch countDownLatch2 = new CountDownLatch(THREAD_NUM);  
  13.   
  14.         for(int i=0; i<THREAD_NUM; i++) {  
  15.             new StringBufferTaskThread(stringBuilder, countDownLatch).start();  
  16.             new StringBufferTaskThread(stringBuffer, countDownLatch2).start();  
  17.         }  
  18.   
  19.         try {  
  20.             countDownLatch.await();  
  21.             countDownLatch2.await();  
  22.             System.out.println("StringBuffer toString: " + stringBuffer.toString());  
  23.             System.out.println("StringBuilder toString: " + stringBuilder.toString());  
  24.             long endTime = System.currentTimeMillis();  
  25.             System.out.println("Running time: " + (endTime-startTime));  
  26.         } catch (InterruptedException e) {  
  27.             e.printStackTrace();  
  28.         }  
  29.     }  
  30. }  
  31. class StringBufferTaskThread extends Thread {  
  32.     private static final String STARTER = "-start";  
  33.     private static final String ENDER = "-end";  
  34.   
  35.     private Object s = null;  
  36.     private CountDownLatch countDownLatch;  // 记载运行线程数  
  37.   
  38.     public StringBufferTaskThread(StringBuilder stringBuilder, CountDownLatch countDownLatch) {  
  39.         super();  
  40.         this.s = stringBuilder;  
  41.         this.countDownLatch = countDownLatch;  
  42.     }  
  43.   
  44.     public StringBufferTaskThread(StringBuffer stringBuffer, CountDownLatch countDownLatch) {  
  45.         super();  
  46.         this.s = stringBuffer;  
  47.         this.countDownLatch = countDownLatch;  
  48.     }  
  49.   
  50.     @Override  
  51.     public void run() {  
  52.         System.out.println(Thread.currentThread().getName() + STARTER);  
  53.         for(int i=0; i<10; i++) {  
  54.             try {  
  55.                 Thread.sleep(200);  
  56.                 if(s instanceof StringBuffer){  
  57.                     ((StringBuffer) s).reverse();  
  58.                     System.out.println("Buffer->"+s.toString());  
  59.                 }else if(s instanceof StringBuilder){  
  60.                     ((StringBuilder) s).reverse();  
  61.                     System.out.println("Builder->"+s.toString());  
  62.                 }  
  63.                 Thread.sleep(200);  
  64.             } catch (InterruptedException e) {  
  65.                 e.printStackTrace();  
  66.             }  
  67.         }  
  68.         System.out.println(Thread.currentThread().getName() + ENDER);  
  69.         countDownLatch.countDown();  
  70.     }  
  71. }  
 输出 下载
Java代码    收藏代码
  1. Thread-0-start  
  2. Thread-1-start  
  3. Thread-2-start  
  4. Thread-3-start  
  5. Thread-4-start  
  6. Thread-5-start  
  7. Thread-6-start  
  8. Thread-7-start  
  9. ....  
  10. Thread-368-end  
  11. Thread-1809-end  
  12. Thread-1609-end  
  13. Thread-1810-end  
  14. Thread-1608-end  
  15. Thread-1702-end  
  16. Thread-1527-end  
  17. StringBuffer toString: AAAABBBB  
  18. StringBuilder toString: AAABBBBB  
  19. Running time: 7523 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值