package com.ghgj.cn.thread;
public class ThreadDome{
private static Object lock = new Object();//声明一个全局变量
private static int i=0;
public static void main(String[] args) {
Thread thread1 = new Thread("朱茵"){
public void run(){
while(i<10){//出口
synchronized (lock) {//对象锁
System.out.println(Thread.currentThread().getName()+i++);//线程名
lock.notify();//提前释放锁
try {
lock.wait(1000);//锁"朱茵"
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
Thread thread2 = new Thread("周星驰"){
public void run(){
while(i<10){//出口
synchronized (lock) {//对象锁
System.out.println(Thread.currentThread().getName()+i++);//线程名
lock.notify();//提前释放锁
try {
lock.wait(1000);//锁"周星驰"
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
thread1.start();
thread2.start();
}
}