java wait() notyfy()的使用

我们知道,并发的编程在企业中用的很多,对于并发的学习是很重要的。

现在我们想从程序上给大家一个分析,在我写这个程序的时候,只知道wait()是线程等待,

notify是唤醒线程,还有一个notifyAll是唤醒所有线程,一般情况下 我们为了保险起见,会用notifyAll来唤醒我们的线程的。

 

 

但是,你是真的理解这个意思么,下面我们来看一个程序吧。

 

 

main类

package endual;

public class MainApp {

	public static void main(String[] args) {
		Query q = new Query(0) ;
		Query1 q1 = new Query1(0) ;
		Thread thread = new Thread(new Thread1(q)) ;
		Thread thread2 = new Thread(new Thread2(q)) ;
		Thread thread3 = new Thread(new Thread3(q1)) ;
		thread.start() ;
		thread2.start() ;
		
		thread3.start() ;
	}
	
}

 

 

Query1类

package endual;

public class Query1 {

	private int value = 7;
	private boolean isFlag = false ;
	public Query1(int value) {
		
		//this.value = value ;
	}
	
	public synchronized  void getValue() {
		
		try {
			wait() ; //如果有人叫他,那么他会唤醒,然就执行下面的程序了
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("-------------------------------------------") ;
		
	}
	
	
	

}

 

query类

 

package endual;

public class Query {

	private int value = 7;
	private boolean isFlag = false ;
	public Query(int value) {
		
		//this.value = value ;
	}
	
	public synchronized  void getValue() {
		
		if (!isFlag) { //如果是正确的
			
			isFlag = true ;
			notifyAll() ;
		}
		
		try {
			wait() ;
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("我是线程1" + (value++)) ;
		
		
	}
	
	public synchronized void setValue() {
		
		if (!isFlag) { //如果是正确的
			
			try {
				wait() ;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
		}
		
		isFlag = false ;
		value-- ;
		System.out.println("我是线程2" + value) ;
		notifyAll() ;
	}
	

}

 

很显然,上面有的方法是notifyAll这个意思就是说唤醒所有等待的线程了。

 

 

三个线程:

package endual;

public class Thread1 implements Runnable{

 private Query q = null ;
 
 public Thread1(Query q) {
  this.q = q ;
 }
 
 public void run() {

  while (true) {
   
   q.getValue() ;
   
  }
  
 }

}










--------------------------------------------



package endual;

public class Thread2 implements Runnable{

 private Query q = null ;
 
 public Thread2(Query q) {
  this.q = q ;
 }
 
 public void run() {

  while (true) {
   
  q.setValue() ;
   
  }
  
 }
}




-------------------------------------



package endual;

public class Thread3 implements Runnable{

 private Query1 q1 = null ;
 
 public Thread3(Query1 q1) {
  this.q1 = q1 ;
 }
 
 public void run() {

  while (true) {
   
   q1.getValue() ;
   
  }
  
 }

}


















 

 

 

请问,打印的结果中会出现------------------------------------------------------------------

这个么?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值