有一个缓冲池,大小一定
一个线程读,一个线程写.

解决方案 »

  1.   

    你是不知道该怎么实现还是不太理解多线程的同步啊。
      

  2.   

    一个时钟:import java.awt.Graphics;
    import java.awt.Color;
    import java.applet.Applet;
    import java.text.*;
    import java.util.*;public class ClockDemo extends Applet implements Runnable {
      
      SimpleDateFormat formatter;  // ノㄓ陪ボ丁妓Α
      Date currentDate;            // 瞷丁
      Thread clockThread = null;
      boolean keepRunning = true;
      
      public void init() {
        int x,y;
        formatter = new SimpleDateFormat ("EEE MMM dd hh:mm:ss yyyy", Locale.getDefault());
        currentDate = new Date();
      }
      
      public void start() {
        if(clockThread == null) {
          clockThread = new Thread(this);
          clockThread.start();
        }
      }
      
      public void run() {
        while(keepRunning) {
          repaint();
          
          try { Thread.sleep(1000); }
          catch(InterruptedException e) {}
        }
      }
      
      // ノよ猭stopㄓ璶―磅︽氨ゎ磅︽
      public void pleaseStop() { keepRunning = false; }
      
      public void paint(Graphics g) {
        int s, m, h;
        String today;    currentDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("s",Locale.getDefault());
        try {
          s = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
          s = 0;
        }
        formatter.applyPattern("m");
        try {
          m = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
          m = 0;
        }
        formatter.applyPattern("h");
        try {
          h = Integer.parseInt(formatter.format(currentDate));
        } catch (NumberFormatException n) {
          h = 0;
        }    formatter.applyPattern("EEE MMM dd HH:mm:ss yyyy");
        today = formatter.format(currentDate);    Clock myClock = new Clock(h, m, s);
        g.drawString(today, 0, 160);
        myClock.show(g, 70, 70, 70);
      }
    }class Clock {
      Clock(int hrs, int min, int sec) {
        hour = hrs % 12;
        minute = min;
        second = sec;
      }  void show(Graphics g, int center_x, int center_y, int radius) {
        int hrs_nid_len = (int)(radius * 0.5),     // ﹚皐
            min_nid_len = (int)(radius * 0.7),     // ﹚だ皐
            sec_nid_len = (int)(radius * 0.85);    // ﹚皐
        double theta;    // 礶牧
        g.drawOval(center_x - radius, center_y - radius,
                   radius * 2, radius * 2);
                   
        g.drawString("9",0,center_y); 
        g.drawString("3",center_x*2-10,center_y);
        g.drawString("12",center_x,10);
        g.drawString("6",center_x,center_y*2);
                   
        // 礶皐
        theta = (double)(hour * 60 * 60 + minute * 60 + second) /
                43200.0 * 2.0 * Math.PI;
        drawNiddle(g, Color.blue, center_x, center_y, hrs_nid_len, theta);    // 礶だ皐
        theta = (double)(minute * 60 + second) /
                3600.0 * 2.0 * Math.PI;
        drawNiddle(g, Color.blue, center_x, center_y, min_nid_len, theta);    // 礶皐
        theta = (double)second / 60.0 * 2.0 * Math.PI;
        drawNiddle(g, Color.red, center_x, center_y, sec_nid_len, theta);
      }  private void drawNiddle(Graphics g, Color c, int x, int y,
                                            int len, double theta) {
        g.setColor(c);
        g.drawLine(x, y, (int)(x + len * Math.sin(theta)),
                         (int)(y - len * Math.cos(theta)));
      }  int hour, minute, second;
    }