• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
红狗<葬爱>贼公子
读书有三到:谓心到,眼到,口到
博客园    首页    新随笔    联系   管理    订阅  订阅

java中StringBuilder为单线程做的,StringBuffer相反

//没有sycronized修饰method,是线程不安全的,--StringBuffer来弥补这个缺点,但速度就慢了

并没有测试出来::

 1 /**
 2  * 
 3  */
 4 package com.breaver.test.string;
 5 
 6 /**
 7  * @author zzf
 8  *@date 2017年6月22日 下午11:09:36
 9  */
10 public class TestStringBuilder implements Runnable{
11     public static StringBuilder strs=new StringBuilder("my-str:");
12     private String str;
13     /**
14      * @param args
15      */
16     
17     public static void main(String[] args) {
18         // TODO Auto-generated method stub
19 //        TestStringBuilder.test();
20         new Thread(new TestStringBuilder("123")).start();
21         new Thread(new TestStringBuilder("abc")).start();
22     }
23     public TestStringBuilder(String a) {
24         super();
25         // TODO Auto-generated constructor stub
26         str=a;
27     }
28     @Override
29     public void run() {
30         // TODO Auto-generated method stub
31         try{
32             while(true){
33                 System.out.println(strs.toString());
34                 Thread.sleep(1000);
35                 strs.append(str);
36             }
37         }catch (Exception e) {
38             // TODO: handle exception
39             e.printStackTrace();
40         }
41     }
42     public static void test(){
43         StringBuilder builder =new StringBuilder();
44         System.out.println(builder.toString()+":"+builder.capacity());
45         builder.append("123");
46         System.out.println(builder.toString()+":"+builder.capacity());
47         builder.append("asssssssssssssqwqw123");
48         System.out.println(builder.toString()+":"+builder.capacity()
49         +"::fact-size:"+builder.length());
50         builder.append("Aasssssssssssssqwqw123");
51         System.out.println(builder.toString()+":"+builder.capacity()
52         +"::fact-size:"+builder.length());
53     }
54 }
View Code

 

posted @ 2017-06-22 23:27  严格的生活_才有深意  阅读(530)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3