生产消费

来源:互联网 发布:同济大学软件学院电话 编辑:程序博客网 时间:2024/04/25 16:52
/*notify和wait方法
--this.notify();
 1.功能:
       不是叫醒正在执行this.notify();的当前线程
       而是叫醒一个现在正在wait this对象的其它线程,如果有多个线程正在wait this对象
       通常是叫醒最先wait this对象的线程,但具体是叫醒哪一个
       这是由系统调度器控制,程序员无法控制
---------------------------------------------------------------
aa.wait
--将执行aa.wait()的当前线程转入阻塞状态,让出CPU的控制权
--释放对aa的锁定


aa.notify()
--假设执行aa.notify()的当前线程为T1
--如果当前某个时刻有其它线程因为执行了aa.wait()而陷入阻塞状态,则叫醒其中的一个
--所谓叫醒某个线程就是令该线程从因为wai而陷入阻塞状态转入就绪状态


aa.notifyAll()
--叫醒其它所有的因为执行了aa.wait()而陷入阻塞状态的线程
-----------------------------------------------------------------
*/
       




class SynStack 
{
private char[] data = new char[6];
private int cnt = 0;//表示数组有效元素的个数
public synchronized void push(char ch)//synxhronized使线程不能同时执行
{
while(cnt == data.length)
{
try
{
this.wait();
}
catch(Exception e)
{

}
   }
   this.notify();//唤醒线程//如果该语句没有因this.wait阻塞的话,叫醒是没用的
data[cnt] = ch;
++cnt;
System.out.printf("生产线程正在生产第%d个产品,该产品是:%c\n",cnt,ch);
   
}
public synchronized char pop()
{
char ch;
while(cnt == 0)
{
try
{
this.wait();
}
catch(Exception e)
{

}
}
this.notify();
ch = data[cnt-1];
System.out.printf("消费线程正在消费第%d个产品,该产品是:%c\n",cnt,ch);
--cnt;
return ch;
}
}
class Producer implements Runnable 
{
private SynStack ss = null;
public Producer(SynStack ss)
{
this.ss = ss;
}
public void run() // 不能抛出异常,本身run()是没有抛出异常的
{
//push('a');//error
char ch;
try
{
Thread.sleep(2000);//需要对异常进行处理
}
catch(Exception e)
{
//可以省略不处理
}
for(int i = 0; i<20; ++i)
{
// try
// {
// Thread.sleep(1000);
// }
// catch(Exception e)
// {
//
// }
ch = (char)('a' + i);
 ss.push(ch);
}
}
}
class Consumer implements Runnable
{
private SynStack ss = null;
public Consumer(SynStack ss)
{
this.ss = ss;

}
   public void run()
   {
    for(int i = 0; i<20;++i)
    {
      try
    {
Thread.sleep(1000);
   }
   catch(Exception e)
    {

  }
    // System.out.printf("%c\n",ss.pop());
    ss.pop();
   }
   }
}
public class TestStack
{
public static void main(String[] args)
{
SynStack ss = new SynStack();
Producer p = new Producer(ss);
Consumer c = new Consumer(ss);
Thread t1 = new Thread(p);
t1.start();

Thread t2 = new Thread(c);
t2.start();
}
}
原创粉丝点击