Android(Java) 带定时器的生产者/消费者

发布于 2024-11-29 11:44:50 字数 1659 浏览 0 评论 0原文

我正在 android 中测试一个简单的生产者/消费者示例,这就是我正在做的事情。

我有两个 EditText 框,一个是生产者,另一个是消费者。该应用程序还有一个按钮,一旦按下该按钮,两个计时器就会启动,生产者在消费者消费的同时进行生产。这是我的代码:

   submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
  Timer producerTimer = new Timer();
  producerTimer .schedule(new TimerTask(){
    @Override
    public void run(){
    producer();
  }
  },100, 300);

  Timer consumerTimer = new Timer();
  consumerTimer .schedule(new TimerTask(){
    @Override
    public void run(){
    consumer();
  }
  },100, 300);
}
});

}

现在介绍其他方法:

public void producer(){
    this.runOnUiThread(producer_Tick);
}

public void consumer(){
    this.runOnUiThread(consumer_Tick2);
}

private Runnable producer_Tick = new Runnable(){
    public void run(){
        put(i++);

    }
};

private Runnable consumer_Tick= new Runnable(){
    public void run(){
        int result = get();
        consumerBox.append(Integer.toString(result) + "\n");

    }
};

这是我的同步方法:

public synchronized void put(int val){
    if (!empty){
        try{
            wait();
        }catch (InterruptedException e) {Log.d(TAG,"Error Putting");}
    }

    producerBox.append(Integer.toString(val) + "\n");
    empty = false;
    buffer=val;
    notify();
}

public synchronized int get(){
    if (empty){
        try{
            wait();
        }catch (InterruptedException e) {Log.d(TAG,"Error getting");}
    }

    empty = true;
    notify();
    return buffer;

}

该程序始终运行到随机点。有时,在消费者从生产者读取数据的地方,程序会运行几分钟。但是,每次在某个时刻,程序都会在生产者和消费者处冻结在某个值(每次都是随机的)。有人发现上面的代码有问题吗?

I am testing a simple producer/ consumer example in android this is what i'm doing.

I have two EditText boxes, one being a producer and the other a consumer. The app also has a single button once this button is pressed two timers start and the producer produces while the consumer consumes. Here is my code:

   submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
  Timer producerTimer = new Timer();
  producerTimer .schedule(new TimerTask(){
    @Override
    public void run(){
    producer();
  }
  },100, 300);

  Timer consumerTimer = new Timer();
  consumerTimer .schedule(new TimerTask(){
    @Override
    public void run(){
    consumer();
  }
  },100, 300);
}
});

}

Now for the other methods:

public void producer(){
    this.runOnUiThread(producer_Tick);
}

public void consumer(){
    this.runOnUiThread(consumer_Tick2);
}

private Runnable producer_Tick = new Runnable(){
    public void run(){
        put(i++);

    }
};

private Runnable consumer_Tick= new Runnable(){
    public void run(){
        int result = get();
        consumerBox.append(Integer.toString(result) + "\n");

    }
};

Here are my Synchronized methods:

public synchronized void put(int val){
    if (!empty){
        try{
            wait();
        }catch (InterruptedException e) {Log.d(TAG,"Error Putting");}
    }

    producerBox.append(Integer.toString(val) + "\n");
    empty = false;
    buffer=val;
    notify();
}

public synchronized int get(){
    if (empty){
        try{
            wait();
        }catch (InterruptedException e) {Log.d(TAG,"Error getting");}
    }

    empty = true;
    notify();
    return buffer;

}

This program runs to random points all the time. Sometimes for a couple of minuites it runs fine where consumer reads from producer etc.. However, everytime, at some point, the program will just freeze at producer and consumer at a certain value (random each time). Does anyone see a problem with the above code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猥︴琐丶欲为 2024-12-06 11:44:50

您应该使用 阻塞队列 在生产者-消费者线程之间进行通信以更有效且易于理解的方式。

You should use a blocking queue to communicate between producer-consumer threads in more efficient and easy to understand way.

旧时浪漫 2024-12-06 11:44:50

你应该使用notifyAll()。通知不保证通知哪个线程。还要检查帽子是否空实际上是同步的(即由 get 和 put 所在的状态类拥有)。

you should use notifyAll(). notify does not gaurantee which thread is notified. Also check hat empty is actually synchronized (i.e. is owned by the status class where get and put reside.).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文