public class TestSynchonized { static StringBuffer s = new StringBuffer("Hello"); public static void main(String args[]){ MyThread t = new MyThread(); t.start(); MyThread.MoidfyString(s); System.out.println("main------"+s); } } class MyThread extends Thread{ public void run(){ MyThread.MoidfyString(TestSynchonized.s); } public synchronized static void MoidfyString(StringBuffer s){ //s = s+"World"; s.append("World"); System.out.println("thread-----"+s); } }
2、使用synchronized 修饰代码块,那么
原文:https://www.cnblogs.com/ansonwan/p/13290577.html