package Thread;
public class Demo4 {
static Object locker = new Object();
static int i;
public static void main(String[] args) {
Thread t1 = new Thread() {
@Override
public void run() {
go();
System.out.println("金莲走进了商场");
System.out.println("金莲开始挑衣服");
synchronized (locker) {
}
{
System.out.println("金莲走进试衣间");
}
System.out.println("金莲结账");
}
};
Thread t2 = new Thread() {
@Override
public void run() {
go();
System.out.println("西门庆走进了商场");
System.out.println("西门庆开始挑衣服");
synchronized (locker) {
System.out.println("西门庆走进试衣间");
}
System.out.println("西门庆结账");
}
};
t1.start();
t2.start();
}
public static synchronized void go() {
System.out.println("要出门"+i++);
}
}