
目录
线程通信的例子:使用两个线程打印1-100。线程1,线程2,交替打印
涉及的3个方法:
运行结果:
把锁改为其他对象
运行结果报错:
改进,把notify和wait的调用者改为obj
sleep()和wait()的异同?
线程通信的例子:使用两个线程打印1-100。线程1,线程2,交替打印
涉及的3个方法:
wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器
  notify():一旦执行此方法,就会唤醒wait的一个线程。如果有多个线程被wait,就会唤醒优先级高的线程
 notifyAll():一旦执行此方法,就会唤醒所有被wait的线程
  说明:
 1. wait(),notify(),notifyAll(),3个方法必须使用在同步代码块或同步方法中。 
2.wait(),notify(),notifyAll(),3个方法的调用者必须是同步代码块或者同步方法中的同步监视器(锁),否则会出现IllegalMonitorStateException
3. wait(),notify(),notifyAll()3个方法都是定义在java.lang.Object类中
class Number implements Runnable{
	private int number=1;
	
	@Override
	public void run() {
		while(true) {
			
//			this是当前类的对象,在本例中,this是nb
			synchronized (this) {
			
				this.notifyAll();
			if(number<=100) {
				
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				System.out.println(Thread.currentThread().getName()+":"+number);
				number++;
				
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}else {
				break;
			}
			
			}
		}
	}
}
public class CommunicationTest {
	public static void main(String[] args) {
		Number nb=new Number();
		
		Thread th1=new Thread(nb);
		Thread th2=new Thread(nb);
		
		th1.setName("线程1");
		th2.setName("线程2");
		
		th1.start();
		th2.start();
	}
}
运行结果:
线程1:1
 线程2:2
 线程1:3
 线程2:4
 线程1:5
.............
线程1:95
 线程2:96
 线程1:97
 线程2:98
 线程1:99
 线程2:100
把锁改为其他对象
把synchronized(this)改为synchronized (obj)
class Number implements Runnable{
	private int number=1;
	public Object obj=new Object();
	@Override
	public void run() {
		while(true) {
			
//			this是当前类的对象,在本例中,this是nb
			synchronized (obj) {
			
				this.notifyAll();
			if(number<=100) {
				
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				System.out.println(Thread.currentThread().getName()+":"+number);
				number++;
				
				try {
					this.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}else {
				break;
			}
			
			}
		}
	}
}
public class CommunicationTest {
	public static void main(String[] args) {
		Number nb=new Number();
		
		Thread th1=new Thread(nb);
		Thread th2=new Thread(nb);
		
		th1.setName("线程1");
		th2.setName("线程2");
		
		th1.start();
		th2.start();
	}
}
运行结果报错:
Exception in thread "线程2" Exception in thread "线程1" java.lang.IllegalMonitorStateException
改进,把notify和wait的调用者改为obj
class Number implements Runnable{
	private int number=1;
	public Object obj=new Object();
	@Override
	public void run() {
		while(true) {
			
			synchronized (obj) {
			
				obj.notifyAll();
			if(number<=100) {
				
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				System.out.println(Thread.currentThread().getName()+":"+number);
				number++;
				
				try {
					obj.wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}else {
				break;
			}
			
			}
		}
	}
}
public class CommunicationTest {
	public static void main(String[] args) {
		Number nb=new Number();
		
		Thread th1=new Thread(nb);
		Thread th2=new Thread(nb);
		
		th1.setName("线程1");
		th2.setName("线程2");
		
		th1.start();
		th2.start();
	}
}
sleep()和wait()的异同?
1.相同点:一旦执行方法,都可以使得当前的线程进入阻塞状态。
2.不同点:(1)两个方法声明的位置不同:Thread类中声明sleep(),Object 类中声明wait()
(2)调用的要求不同,sleep()可以在任何需要的场景下调用,wait()必须使用在同步代码块或者同步方法中
(3)是否释放同步监视器:如果两个方法都使用在同步代码块或同步方法中,sleep不会释放锁,wait()会释放锁



















