synchronized(this)里的this什么意思?
看到了一段代码:----------------------------------------------------------------------------------------
class Zhangsan {
public void say() {
System.out.println("张三对李四说:你给我画,我就把书给你");
}
public void get() {
System.out.println("张三得到了画");
}
}
class Lisi {
public void say() {
System.out.println("李四对张三说:你给我书,我就把画给你");
}
public void get() {
System.out.println("李四得到了书");
}
}
public class Test2 implements Runnable {
private static Zhangsan zs = new Zhangsan();
private static Lisi ls = new Lisi();
private boolean flag = false;
public void run() {
if (flag) {
synchronized (zs) { //此处的zs或者说synchronized () 括号里的东西表示什么意思?
zs.say();
synchronized (ls) {
zs.get();
}
}
} else {
synchronized (ls) {
ls.say();
synchronized (zs) {
ls.get();
}
}
}
}
public static void main(String args[]) {
Test2 t1 = new Test2();
Test2 t2 = new Test2();
t1.flag = true;
t2.flag = false;
Thread thA = new Thread(t1);
Thread thB = new Thread(t2);
thA.start();
thB.start();
}
}