详细:
https://blog.csdn.net/u011702479/article/details/82262823
https://www.cnblogs.com/su-feng/p/6659064.html
当对字符串进行修改的时候,需要使用 StringBuffer 和 StringBuilder 类。
在线程安全上,StringBuilder是线程不安全的,而StringBuffer是线程安全的
但是StringBuilder效率更高,多数情况下用这个
/** * @author sr * @date 2021/1/18 */ public class ThreadTest { public static StringBuilder SB = new StringBuilder(); public static void main(String[] args) { for (int i = 0; i < 5; i++) { Thread t =new Thread(new MyRun()); t.start(); } try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("字符串共有"+SB.length()+"字符"); } }
/** * @author sr * @date 2021/1/18 */ public class MyRun implements Runnable{ @Override public void run() { for (int i = 0; i < 100; i++) { ThreadTest.SB.append("1"); } } }