package api.day08;
public class SynchronizedDemo02 {
public static void main(String[] args) {
final Shop shop=new Shop();
Thread t1=new Thread(){
public void run() {
shop.buy();
};
};
Thread t2=new Thread(){
public void run() {
shop.buy();
};
};
Thread t3=new Thread(){
public void run() {
shop.buy();
};
};
t1.start();
t2.start();
t3.start();
}
}
class Shop{
public void buy() {
Thread t=Thread.currentThread();
System.out.println(t+"正在挑选衣服...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (this){
System.out.println(t+"正在试衣服");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(t+"结账离开");
}
}