#Need help, Monitors as synchronization , SharedBuffer as shared resource for communication

1 messages · Page 1 of 1 (latest)

barren bridge
#

I don't know how to make multiple writer/modifiers threads to run concurrently.
Heres the requirement:
Multiple Writer threads should perform the action of writing, with each thread picking up a
string from the source. The source is a list of strings loaded from a file and each thread
should store its string in the shared buffer. Multiple Modifier threads should each collect a
string from the shared buffer, modify its content, and save it back in the same position of
the shared buffer. The Modifier thread that processes a string should mark it for the Reader
thread

fickle oysterBOT
#

<@&987246399047479336> please have a look, thanks.

fickle oysterBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

barren bridge
#
package textEditors.model;

import textEditors.main.Controller;

import java.util.concurrent.locks.Condition;

public class SharedBuffer {
    private int bufferSize;
    private Controller controller;
    private String[] buffer;
    private BufferStatus[] status;
    private int writePos;
    private int readPos;
    private int modifyPos;
    
    public SharedBuffer(int bufferSize, Controller controller) {
        this.bufferSize = bufferSize;
        this.controller = controller;
        buffer = new String[bufferSize];
        status = new BufferStatus[bufferSize];
        writePos = 0;
        readPos = 0;
        modifyPos = 0;

        for (int i = 0; i < bufferSize; i++) {
            buffer[i] = "";
            status[i] = BufferStatus.Empty;
        }
    }

    public synchronized void writeData(String line, String threadName) {
        while ( status[writePos] != BufferStatus.Empty) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        buffer[writePos] = line;
        status[writePos] = BufferStatus.New;
        System.out.println(threadName + " - has written \"" + line + "\", position " + writePos);
        String logEntry = threadName + " - has written \"" + line + "\", position " + writePos;
        controller.logbookStatus(logEntry);
        writePos = (writePos + 1) % bufferSize;
        notifyAll();
    }
    
    public synchronized void modifyData(String find, String replace, String threadName) {
        while (status[modifyPos] != BufferStatus.New) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (buffer[modifyPos].contains(find)) {
            buffer[modifyPos] = buffer[modifyPos].replace(find, replace);
            System.out.println(threadName + " - has modified " + find + " to " + replace + " at position " + modifyPos);
            String logEntry = threadName + " - has modified " + find + " to " + replace + " at position " + modifyPos;
            controller.logbookStatus(logEntry);
        }
        status[modifyPos] = BufferStatus.Checked;
        modifyPos = (modifyPos + 1) % bufferSize;
        notifyAll();
    }

    public synchronized String readData(String threadName) {
        String line = "";

        while (status[readPos] != BufferStatus.Checked) {
            try {
                wait();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        line = buffer[readPos];
        status[readPos] = BufferStatus.Empty;
        System.out.println(threadName + " - has read " + line + " position " + readPos);
        String logEntry = threadName + " - has read, position " + readPos;
        readPos = (readPos + 1) % bufferSize;

        notifyAll();
        return line;
    }

    public enum BufferStatus {
        Empty,
        Checked,
        New
    }
}


fickle oysterBOT
# barren bridge ```java package textEditors.model; import textEditors.main.Controller; import ...

Detected code, here are some useful tools:

Formatted code
package textEditors.model;

import textEditors.main.Controller;
import java.util.concurrent.locks.Condition;

public class SharedBuffer {
  private int bufferSize;
  private Controller controller;
  private String[] buffer;
  private BufferStatus[] status;
  private int writePos;
  private int readPos;
  private int modifyPos;
  public SharedBuffer(int bufferSize, Controller controller) {
    this .bufferSize = bufferSize;
    this .controller = controller;
    buffer = new String[bufferSize] ;
    status = new BufferStatus[bufferSize] ;
    writePos = 0;
    readPos = 0;
    modifyPos = 0;
    for (int i = 0; i < bufferSize; i++) {
      buffer[i]  = "";
      status[i]  = BufferStatus.Empty;
    }
  }
  public synchronized void writeData(String line, String threadName) {
    while (status[writePos]  != BufferStatus.Empty) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    buffer[writePos]  = line;
    status[writePos]  = BufferStatus.New;
    System.out.println(threadName + " - has written \"" + line + "\", position " + writePos);
    String logEntry = threadName + " - has written \"" + line + "\", position " + writePos;
    controller.logbookStatus(logEntry);
    writePos = (writePos + 1) % bufferSize;
    notifyAll();
  }
  public synchronized void modifyData(String find, String replace, String threadName) {
    while (status[modifyPos]  != BufferStatus.New) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    if (buffer[modifyPos] .contains(find)) {
      buffer[modifyPos]  = buffer[modifyPos] .replace(find, replace);
      System.out.println(threadName + " - has modified " + find + " to " + replace + " at position " + modifyPos);
      String logEntry = threadName + " - has modified " + find + " to " + replace + " at position " + modifyPos;
      controller.logbookStatus(logEntry);
    }
    status[modifyPos]  = BufferStatus.Checked;
    modifyPos = (modifyPos + 1) % bufferSize;
    notifyAll();
  }
  public synchronized String readData(String threadName) {
    String line = "";
    while (status[readPos]  != BufferStatus.Checked) {
      try {
        wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    line = buffer[readPos] ;
    status[readPos]  = BufferStatus.Empty;
    System.out.println(threadName + " - has read " + line + " position " + readPos);
    String logEntry = threadName + " - has read, position " + readPos;
    readPos = (readPos + 1) % bufferSize;
    notifyAll();
    return line;
  }
  public enum BufferStatus {
    Empty, Checked, New}
}
rotund bramble
#

It is always horrible/a challenge to deal with basic sync-patterns; therefor Java provides a higher abstraction for such tasks. One of them is - for example - a ForkJoinPool in combination with RecursiveAction/Task. The jaadoc for RecursiveAction shows the basic usage

foggy sparrow
#

Java also have atomic classes like AtomicInt and AtomicReference, these are thread safe wrappers e.g. AtomicInt is a thread safe Integer, you could probably do that to start with and see how it performs

barren bridge
#

Ok i will check that out thanks for the response! HYPERS