Member Avatar

Hey there good folks! :)

I am working on a thread task called, Lab Alarm, and I am nearly done with it. We're suppoosed to work with the "producer-consumer" problem and It's just teacher code that one is supposed to modify and add some own to it. Now, I am almost done with this thing - but I keep getting some errors - which I don't fully comprehend. If anyone could shed a little light over it, and what next step I should take, I would be grateful. :)

The lab consists of 7 classes - they are not big ones though - and I will post the links to them right here. Any help is welcome. Best wishes to everyone!

The Task:








Recommended Answers

All 5 Replies

All fields inside classes private, non static.
My concept of view to the architecture of program.
I don't sure you happy, if I post whoole code.
I'm not sure if it would be correct in the learning process.

package ops023_lab01_uppgift_jc3;

import java.awt.Point;

public class LabAlarm {
    //make sure that they have a common buffer AlarmBuffer to write and read data

    static AlarmBuffer ab = new AlarmBuffer();
    static AlarmClient client1 = new AlarmClient("client1", new Point(600, 100));
    static AlarmClient client2 = new AlarmClient("client2", new Point(600, 200));
    static AlarmClient client3 = new AlarmClient("client3", new Point(600, 300));
    static AlarmClient client4 = new AlarmClient("client4", new Point(600, 400));
    static AlarmProducer producer1 = new AlarmProducer("producer1", ab, client1);
    static AlarmProducer producer2 = new AlarmProducer("producer2", ab, client2);
    static AlarmProducer producer3 = new AlarmProducer("producer3", ab, client3);
    //version with more then one registered clients
    static AlarmProducer producer4 = new AlarmProducer("producer4", ab, client1, client2, client3, client4);
    static AlarmConsumer consumer1 = new AlarmConsumer("consumer1", new Point(10, 0), ab);
    static AlarmConsumer consumer2 = new AlarmConsumer("consumer2", new Point(10, 400), ab);

    public LabAlarm() {

        Thread runnableProducer1 = new Thread(producer1);
        Thread runnableProducer2 = new Thread(producer2);
        Thread runnableProducer3 = new Thread(producer3);
        Thread runnableProducer4 = new Thread(producer4);
        Thread runnableConsumer1 = new Thread(consumer1);
        Thread runnableConsumer2 = new Thread(consumer2);


    public static void main(String[] args) {
        new LabAlarm();
package ops023_lab01_uppgift_jc3;

//class for a Buffer in the producer consumer problem
//look in lecture slides for hint on how to implement it
public class AlarmBuffer {

    private AlarmMessage message;
    private boolean empty;

    public void TheBuffer() {
        System.out.println("TheBuffer ");
        this.empty = true;

    public synchronized void insert(AlarmMessage message) {
        System.out.println("i1");//for test
        while (!empty) {
            try {
            } catch (InterruptedException e) {
        }//end of while-loop
        this.message = message;
        this.empty = false;
    }//end of insert-method

    // who uses this ?
    public synchronized AlarmMessage remove() {
        while (empty) {
            try {
            } catch (InterruptedException e) {

        }//end of while-loop
        this.empty = true;
        return message;

    }//end of remove-method

    public synchronized boolean isEmpty(String who) {//3.
        System.out.println(who + " ask-->AlarmBuffer: isEmpty() " + empty + "\n");
        return empty;

    public synchronized boolean isEmpty() {
        return empty;
}//end of class
package ops023_lab01_uppgift_jc3;

public class AlarmMessage {

    private String fromClient;
    private String message;
    private long alarmTime;

    AlarmMessage(String name, String messageText) {
        fromClient = name;
        message = messageText;
        alarmTime = System.currentTimeMillis();

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[fromClient: ");
        sb.append(",message: ");
        sb.append(",alarmTime: ");
        return sb.toString();

I keep getting some errors

We need more information than this. Exactly what errors?

I keep getting some errors

We need more information than this. Exactly what errors?

Member Avatar

We need more information than this. Exactly what errors?


I redid the coding and I got it working. The problems I encounter now is that on the AlarmServer GUI - it doesn't show the correct client number when printed on the GUI. And, in AlarmConsumer (Line 42) - I don't know how I would make it so that the values in the buffer is correctly compared to an AlarmClient's message. Those two are the things that annoy me at the time, and I would like to know what I need to do?

Here's the new code:








Human --> AlarmClient
Human: (enter text, press button)
AlarmClient: on press button creates message, setups flag
AlarmProducer --> AlarmClient
AlarmProducer: checks AlarmClient is flag on? , if Yes - gets the message
AlarmProducer --> AlarmBuffer
AlarmProducer: inserts message into AlarmBuffer
AlarmBuffer: stores message, setups flag , sends signal notify()
AlarmConsumer --> AlarmBuffer
AlarmConsumer: checks AlarmBuffer isEmpty , if not empty - gets the message
AlarmBuffer: setups flag , sends signal notify(), returns message
AlarmConsumer --> AlarmServer
AlarmConsumer: sends message to AlarmServer
AlarmServer --> Human
AlarmServer: displays the message
Human: reads this message

No - AlarmProducer producerX = new AlarmProducer(ab, as);
but - AlarmProducer producerX = new AlarmProducer(ab);

Make all fields private, for access to the fields, use the set() , get() , is() methods (public)

Compiler will show some errors, that you remove yourself, or you asking a question.

In first phase of develop let's only AlarmClient generate the message (instance of AlarmMessage). AlarmProducer,AlarmBuffer,AlarmConsumer,AlarmServer only transport this one to the another.
Later you can extend the message-format , to store for example: time , where the message is inside paricular parts of the chain.

Do not extend program if basic things not run properly.
Even if that part is over, you will need to carefully look at threads.(maybe use threadpool).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.