⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right6_2_2.htm

📁 清华大学JAVA教程
💻 HTM
字号:
<html><head><title>JAVA编程语言</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../../css/text.css" type="text/css"><script language="JavaScript" type="text/JavaScript"><!--function MM_openBrWindow(theURL,winName,features) { //v2.0  window.open(theURL,winName,features);}//--></script></head><body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">  <tr>     <td valign="top">       <table width="94%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">        <tr>          <td valign="top"> <p> <span class="pt9-black">  我们把系统中使用某类资源的线程称为消费者,产生或释放同类资源的线程称为生产者。               <br>              在下面的Java的应用程序中,生产者线程向文件中写数据,消费者从文件中读数据,这样,在这个程序中同时运行的两个线程共享同一个文件资源。通过这个例子我们来了解怎样使它们同步。<br>              <br>                 <a href="#" onClick="MM_openBrWindow('622.htm','','width=720,height=360')">--观看动画--</a><br>              <br>              <img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例6.5<a name="01"></a></font><br>                 class SyncStack{ <font color="339900">//同步堆栈类</font><br>                 private int index = 0;<font color="339900"> //堆栈指针初始值为0</font><br>                 private char []buffer = new char[6]; <font color="339900">//堆栈有6个字符的空间</font><br>              <br>                 public synchronized void push(char c){ <font color="339900">//加上互斥锁</font><br>                   while(index = = buffer.length){ <font color="339900">//堆栈已满,不能压栈</font><br>                   try{<br>                      this.wait(); <font color="339900">//等待,直到有数据出栈</font><br>                     }catch(InterruptedException e){}<br>                     }</span></p>            <p> <span class="pt9-black">   this.notify(); <font color="339900">//通知其它线程把数据出栈</font><br>                 buffer[index] = c; <font color="339900">//数据入栈</font><br>                 index++; <font color="339900">//指针向上移动</font><br>                 }</span></p>            <p class="pt9-black">   public synchronized char pop(){ <font color="339900">//加上互斥锁</font><br>                     while(index ==0){ <font color="339900">//堆栈无数据,不能出栈</font><br>                      try{<br>                         this.wait(); <font color="339900">//等待其它线程把数据入栈</font><br>                      }catch(InterruptedException e){}<br>                        }</p>            <p class="pt9-black">        this.notify(); <font color="339900">//通知其它线程入栈</font><br>                     index- -; <font color="339900">//指针向下移动</font><br>                     return buffer[index]; <font color="339900">//数据出栈</font><br>                  }<br>                     }</p>            <p class="pt9-black">    class Producer implements Runnable{ <font color="339900">//生产者类</font><br>                     SyncStack theStack; <font color="339900"><br>                      //生产者类生成的字母都保存到同步堆栈中</font><br>              <br>                     public Producer(SyncStack s){<br>                        theStack = s;<br>                     }</p>            <p class="pt9-black">       public void run(){<br>                        char c;<br>                        for(int i=0; i&lt;20; i++){<br>                          c =(char)(Math.random()*26+'A'); <font color="339900"><br>                                        //随机产生20个字符</font><br>                          theStack.push(c); //把字符入栈<br>                          System.out.println(&quot;Produced: &quot;+c); <font color="339900">//打印字符</font><br>                          try{<br>                          Thread.sleep((int)(Math.random()*1000));<br>                                   <font color="339900">/*每产生一个字符线程就睡眠*/</font><br>                          }catch(InterruptedException e){}<br>                        }<br>                     }<br>                   }</p>            <p class="pt9-black">      class Consumer implements Runnable{ <font color="339900">//消费者类</font><br>                       SyncStack theStack; <font color="339900"><br>                                //消费者类获得的字符都来自同步堆栈</font><br>              <br>                       public Consumer(SyncStack s){<br>                           theStack = s;<br>                       }</p>            <p class="pt9-black">          public void run(){<br>                           char c;<br>                           for(int i=0;i&lt;20;i++){<br>                             c = theStack.pop(); <font color="339900">//从堆栈中读取字符</font><br>                           System.out.println(&quot;Consumed: &quot;+c); <font color="339900"><br>                                           //打印字符</font><br>                           try{<br>                           Thread.sleep((int)(Math.random()*1000)); <font color="339900"><br>                                  /*每读取一个字符线程就睡眠*/</font><br>                           }catch(InterruptedException e){}<br>                       }<br>                     }<br>                   }</p>            <p class="pt9-black">      public class SyncTest{<br>                     public static void main(String args[]){<br>                       SyncStack stack = new SyncStack();<br>                 <font color="339900">//下面的消费者类对象和生产者类对象所操作的是同一个同步堆栈对象</font><br>                       Runnable source=new Producer(stack);<br>                       Runnable sink = new Consumer(stack);<br>                       Thread t1 = new Thread(source); <font color="339900">//线程实例化</font><br>                       Thread t2 = new Thread(sink); <font color="339900">//线程实例化</font><br>                       t1.start(); <font color="339900">//线程启动</font><br>                       t2.start(); <font color="339900">//线程启动</font><br>                     }<br>                   }<br>              <br>                类Producer是生产者模型,其中的 run()方法中定义了生产者线程所做的操作,循环调用push()方法,将生产的20个字母送入堆栈中,每次执行完push操作后,调用sleep()方法睡眠一段随机时间,以给其他线程执行的机会。类Consumer是消费者模型,循环调用pop()方法,从堆栈中取出一个数据,一共取20次,每次执行完pop操作后,调用sleep()方法睡眠一段随机时间,以给其他线程执行的机会。</p>            <p class="pt9-black">  程序执行结果<br>                      Produced:V<br>                      Consumed:V<br>                      Produced:E<br>                      Consumed:E<br>                      Produced:P<br>                      Produced:L<br>                      ...<br>                      Consumed:L<br>                      Consumed:P</p>            <p class="pt9-black">  在上述的例子中,通过运用wait()和notify()方法来实现线程的同步,在同步中还会用到notifyAll()方法,一般来说,每个共享对象的互斥锁存在两个队列,一个是锁等待队列,另一个是锁申请队列,锁申请队列中的第一个线程可以对该共享对象进行操作,而锁等待队列中的线程在某些情况下将移入到锁申请队列。下面比较一下wait()、notify()和notifyAll()方法:<br>              <br>                (1) wait,nofity,notifyAll必须在已经持有锁的情况下执行,所以它们只能出现在synchronized作用的范围内,也就是出现在用                     synchronized修饰的方法或类中。<br>              <br>                (2) wait的作用:释放已持有的锁,进入等待队列.<br>                <br>                (3) notify的作用:唤醒wait队列中的第一个线程并把它移入锁申请队列.<br>              <br>                (4) notifyAll的作用:唤醒wait队列中的所有的线程并把它们移入锁申请队列.</p>            <p class="pt9-black">  <font color="000099">注意:</font><br>                <b>1) suspend()和resume()</b><br>                  在JDK1.2中不再使用suspend()和resume(),其相应功能由wait()和notify()来实现。<br>              <br>                <b>2) stop()</b><br>                  在JDK1.2中不再使用stop(),而是通过标志位来使程序正常执行完毕。例6.6就是一个典型的例子。</p>            <p class="pt9-black"><span class="pt9-black"><img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"> <font color="000099">例6.6<a name="02"></a></font>               </span><br>                 public class Xyz implements Runnable {<br>                    private boolean timeToQuit=false; <font color="339900">//标志位初始值为假</font><br>                    public void run() {<br>                       while(!timeToQuit) {<font color="339900">//只要标志位为假,线程继续运行</font><br>                           …<br>                       } <br>                    }</p>            <p><span class="pt9-black">   public void stopRunning() {<br>                       timeToQuit=true;} <font color="339900">//标志位设为真,表示程序正常结束</font><br>                    }<br>                 public class ControlThread {<br>                    private Runnable r=new Xyz();<br>                    private Thread t=new Thread(r);<br>                    public void startThread() {<br>                       t.start();<br>                    }<br>                    public void stopThread() {<br>                       r.stopRunning(); }<font color="339900"><br>                             //通过调用stopRunning方法来终止线程运行</font><br>                    }</span><br>            </p>            <p>&nbsp;</p>            </td>        </tr>      </table>    </td>  </tr></table></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -