There is no sycronized modifier method, is thread unsafe,--stringbuffer to compensate for this shortcoming, but the speed is slow
And it's not tested:
1 /**2 * 3 */4 Packagecom.breaver.test.string;5 6 /**7 * @authorZzf8 * @date June 22, 2017 PM 11:09:369 */Ten Public classTeststringbuilderImplementsrunnable{ One Public StaticStringBuilder strs=NewStringBuilder ("My-str:"); A PrivateString str; - /** - * @paramargs the */ - - Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub + //teststringbuilder.test (); - NewThread (NewTeststringbuilder ("123") . Start (); + NewThread (NewTeststringbuilder ("abc")) . Start (); A } at PublicTeststringbuilder (String a) { - Super(); - //TODO auto-generated Constructor stub -Str=A; - } - @Override in Public voidrun () { - //TODO auto-generated Method Stub to Try{ + while(true){ - System.out.println (strs.tostring ()); theThread.Sleep (1000); * strs.append (str); $ }Panax Notoginseng}Catch(Exception e) { - //Todo:handle Exception the e.printstacktrace (); + } A } the Public Static voidTest () { +StringBuilder Builder =NewStringBuilder (); -System.out.println (builder.tostring () + ":" +builder.capacity ()); $Builder.append ("123"); $System.out.println (builder.tostring () + ":" +builder.capacity ()); -Builder.append ("asssssssssssssqwqw123"); -System.out.println (builder.tostring () + ":" +builder.capacity () the+ ":: fact-size:" +builder.length ()); -Builder.append ("Aasssssssssssssqwqw123");WuyiSystem.out.println (builder.tostring () + ":" +builder.capacity () the+ ":: fact-size:" +builder.length ()); - } Wu}View Code
StringBuilder is single-threaded in Java, stringbuffer the opposite