有三个线程,A线程是随即不断生成字母(或数字)的,B线程是对A线程的生成的数据进行处理的,C线程监控B线程,当B线程里出现a(或1)这个数的时候,C线程有个报警行为。 
B线程里有循环队列,A线程的数据都是进入这个循环队列里的。
请大家帮帮忙吧·
 很着急用!

解决方案 »

  1.   

    不知道楼主的问题解决了没,我这有段代码,楼主需要可以看下。
    1,线程A负责产生随机数据,此处只提供字母(a~z)的随机生成,对于数字或大写字母,代码稍作修改即可。
    2,线程B维护一个循环队列,有A向队列中放入数据,当接收到的数据符合要求时,项C发出通知,C会发出警报。循环队列用到代码见以下链接http://www.examda.com/Java/zhuanye/20110303/122723359.html
    3,线程C负责具体发出告警信息。
    4,(1)处注释用于跟踪队列,(2)与(1)结合可验证循环队列。public class A implements Runnable{
    char randomChar;
    B b;
    Random rand = new Random(47);
    public A(B b){
    this.b = b;
    }

    public void run(){
    while(!Thread.interrupted()){
    randomChar = getRandomChar();
    System.out.println("get " + randomChar + " by A");
    b.putChar(randomChar);
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    }
    }
    }
    private char getRandomChar(){
    return (char) (rand.nextInt(24) + 96);
    }
    }
    public class B implements Runnable{
    QueueArray dataQueue = new QueueArray();
    C c;
    boolean needNotify = false;
    public B(C c){
    this.c = c;
    }

    public void run(){
    while(!Thread.interrupted()){
    if(needNotify){
    notifyA();
    needNotify = false;
    }
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
    public void putChar(char c){
    if(c == 'a'){
    needNotify = true;
    }
    dataQueue.enqueue(c);
    //(1) dataQueue.printQueue();
    //(2) System.out.println("The data in the front of queue is :" + dataQueue.dequeue());

    }
    private void notifyA(){
    c.setAlerm(true);
    }
    }
    public class C implements Runnable{

    boolean neadAlerm = false;

    public void run(){
    while(!Thread.interrupted()){
    if(neadAlerm){
    alerm();
    neadAlerm = false;
    }
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    private void alerm() {
    System.out.println("This is alermMessage");
    } public void setAlerm(boolean b) {
    neadAlerm = b;
    }
    }
    public class QueueArray {
        Object[] a; //对象数组,队列最多存储a.length-1个对象
        int front;  //队首下标
        int rear;   //队尾下标
        public QueueArray(){
            this(10); //调用其它构造方法
        }
        public QueueArray(int size){
            a = new Object[size];
            front = 0;
            rear =0;
        }
        /**
         * 将一个对象追加到队列尾部
         * @param obj 对象
         * @return 队列满时返回false,否则返回true
         */
        public boolean enqueue(Object obj){
            if((rear+1)%a.length==front){
                return false;
            }
            a[rear]=obj;
            rear = (rear+1)%a.length;
            return true;
        }
        /**
         * 队列头部的第一个对象出队
         * @return 出队的对象,队列空时返回null
         */
        public Object dequeue(){
            if(rear==front){
                return null;
            }
            Object obj = a[front];
            front = (front+1)%a.length;
            return obj;
        }
        
        public void printQueue(){
         for(int i = 0; i < a.length; i++){
         System.out.print(a[i]+" ");
         }
         System.out.println();
        }
    }
    public class Test {
    public static void main(String[] args) throws InterruptedException {
    C c = new C();
    B b = new B(c);
    A a = new A(b);
    System.out.println("Now work start......");
    new Thread(a).start();
    new Thread(b).start();
    new Thread(c).start();

    Thread.sleep(10000);
    System.exit(0);
    }
    }