package com.thread.demo;public class SynchronizedDemo {
/**
 * @尝试同步代码块,在代码块内,将获得对象的锁,使得在该代码块执行期间其他线程不能修改对象.
 */
public static void main(String[] args) {
StringBuffer sb=new StringBuffer("A");
MyThread mt=new MyThread(sb);
mt.setName("mt");
mt.start();

MyThread mk=new MyThread(sb);
mk.setName("mk");
mk.start();

MyThread ms=new MyThread(sb);
ms.setName("ms");
ms.start();
}}
class MyThread extends Thread{
private StringBuffer sb;
public  MyThread(StringBuffer sb){
this.sb=sb;
}
public void run(){
synchronized (sb){//这里加锁的是sb,把sb换成this可不可以,区别有什么
for(int i=0;i<99;i++){
System.out.print(Thread.currentThread().getName()+":"+ sb);
System.out.println();
}
char temp=sb.charAt(0);
++temp;
sb.setCharAt(0, temp);
}
}
}
学习中不理解这个this,求高人指点