Multiple Thread Writer with Single Thread Reader using PipedOutputStream and PipedInputStream - java

I have a Multiple Writer Threads with Single Reader Thread model.
The ThreadMultipleDateReceiver class is designed to read from multiple Threads.
public class ThreadMultipleDateReceiver extends Thread {
private static final int MAX_CLIENT_THREADS = 4;
private byte[] incomingBytes;
private volatile boolean isRunning;
private volatile List<ThreadStreamDateWriter> lThrdDate;
private static PipedInputStream pipedInputStream;
public ThreadMultipleDateReceiver() {
lThrdDate = Collections.synchronizedList(new ArrayList<>(MAX_CLIENT_THREADS));
pipedInputStream = new PipedInputStream();
System.out.println("ThreadMultipleDateReceiver Created");
}
#Override public void run() {
isRunning = true;
while (isRunning) {
if (!lThrdDate.isEmpty()) {
System.out.println("ThreadMultipleDateReceiver has:" + lThrdDate.size());
for (int i = lThrdDate.size(); i > 0; i--) {
if (lThrdDate.get(i - 1).getState() == Thread.State.TERMINATED) {
lThrdDate.remove(i - 1);
} else {
System.out.println("I ThreadMultipleDateReceiver have:" + lThrdDate.get(i - 1).getNameDateWriter());
}
}
incomingBytes = new byte[1024];
try {
String str = "";
int iRd;
System.out.println("ThreadMultipleDateReceiver waiting:" + str);
while ((iRd = pipedInputStream.read(incomingBytes)) != -1) {
if (iRd > 0) {
str += new String(incomingBytes);
}
}
System.out.println("ThreadMultipleDateReceiver Received:\n\t:" + str);
} catch (IOException e) { }
} else {
System.out.println("ThreadMultipleDateReceiver Empty");
}
}
emptyDateWriters();
}
public void addDateWriter(ThreadStreamDateWriter threadDateWriter) {
if (lThrdDate.size() < MAX_CLIENT_THREADS) {
lThrdDate.add(threadDateWriter);
}
}
private void emptyDateWriters() {
if (!lThrdDate.isEmpty()) {
for (int i = lThrdDate.size(); i > 0; i--) {
ThreadStreamDateWriter threadDateWriter = lThrdDate.get(i - 1);
threadDateWriter.stopThread();
lThrdDate.remove(i - 1);
}
}
}
public PipedInputStream getPipedInputStream() {
return pipedInputStream;
}
public void stopThread() {
isRunning = false;
}
}
And the single Writer Thread
public class ThreadStreamDateWriter extends Thread {
String Self;
private byte[] outgoingBytes;
private volatile boolean isRunning;
private static PipedOutputStream pipedOutputStream;
ThreadStreamDateWriter(String name, PipedInputStream snk) {
Self = name;
pipedOutputStream = new PipedOutputStream();
try {
pipedOutputStream.connect(snk);
} catch (IOException e) { }
}
#Override public void run() {
isRunning = true;
while (isRunning) {
try {
outgoingBytes = getInfo().getBytes();
System.out.println("ThreadStreamDateWriter -> write to pipedOutputStream:" + new String(outgoingBytes));
pipedOutputStream.write(outgoingBytes);
System.out.println("ThreadStreamDateWriter -> wrote:" + new String(outgoingBytes));
try { Thread.sleep(4000); } catch (InterruptedException ex) { }
} catch (IOException | NegativeArraySizeException | IndexOutOfBoundsException e) {
isRunning = false;
}
}
}
String getInfo() {
String sDtTm = new SimpleDateFormat("yyyyMMdd-hhmmss").format(Calendar.getInstance().getTime());
return Self + " -> " + sDtTm;
}
public void stopThread() {
isRunning = false;
}
public String getNameDateWriter() {
return Self;
}
}
How launch (I'm using Netbeans)?
ThreadMultipleDateReceiver thrdMDateReceiver = null;
ThreadStreamDateWriter thrdSDateWriter0 = null;
ThreadStreamDateWriter thrdSDateWriter1 = null;
private void jtbDateExchangerActionPerformed(java.awt.event.ActionEvent evt) {
if (jtbDateExchanger.isSelected()) {
if (thrdMDateReceiver == null) {
thrdMDateReceiver = new ThreadMultipleDateReceiver();
thrdMDateReceiver.start();
}
if (thrdSDateWriter0 == null) {
thrdSDateWriter0 = new ThreadStreamDateWriter("-0-", thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter0.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter0);
}
if (thrdSDateWriter1 == null) {
thrdSDateWriter1 = new ThreadStreamDateWriter("-1-", thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter1.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter1);
}
} else {
if (thrdMDateReceiver != null) {
thrdMDateReceiver.stopThread();
}
}
}
The OUTPUT
run:
ThreadMultipleDateReceiver Created
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
.....
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver Empty
ThreadMultipleDateReceiver has:1
I ThreadMultipleDateReceiver have:-0-
ThreadMultipleDateReceiver waiting:
ThreadStreamDateWriter -> write to pipedOutputStream:-0- -> 20170608-090003
ThreadStreamDateWriter -> write to pipedOutputStream:-1- -> 20170608-090003
BUILD SUCCESSFUL (total time: 1 minute 3 seconds)
The ThreadMultipleDateReceiver is blocked, and is not printing:
ThreadMultipleDateReceiver Received:
-1- -> 20170608-090003
or
ThreadMultipleDateReceiver Received:
-0- -> 20170608-090003
How solve it?

looks like your piped output stream is static, so every time you construct a ThreadStreamDateWriter, you are stepping on the old value of piped output stream.
try making this an instance variable and pass it into the constructor. so you only have one of them.
edit 1: i made the pipes instance variables and added some printouts. seems to be running longer now (see below):
edit 2: you second pipedOutputStream.connect(snk); is throwing. you can only connect one thing at a time.
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
public class So44438086 {
public static class ThreadMultipleDateReceiver extends Thread {
private static final int MAX_CLIENT_THREADS=4;
private byte[] incomingBytes;
private volatile boolean isRunning;
private volatile List<ThreadStreamDateWriter> lThrdDate;
private /*static*/ PipedInputStream pipedInputStream;
public ThreadMultipleDateReceiver() {
lThrdDate=Collections.synchronizedList(new ArrayList<>(MAX_CLIENT_THREADS));
pipedInputStream=new PipedInputStream();
System.out.println("ctor setting pipedInputStream to: "+pipedInputStream);
System.out.println("ThreadMultipleDateReceiver Created");
}
#Override public void run() {
isRunning=true;
while(isRunning) {
if(!lThrdDate.isEmpty()) {
System.out.println("ThreadMultipleDateReceiver has:"+lThrdDate.size());
for(int i=lThrdDate.size();i>0;i--) {
if(lThrdDate.get(i-1).getState()==Thread.State.TERMINATED) {
lThrdDate.remove(i-1);
} else {
System.out.println("I ThreadMultipleDateReceiver have:"+lThrdDate.get(i-1).getNameDateWriter());
}
}
incomingBytes=new byte[1024];
try {
String str="";
int iRd;
System.out.println("ThreadMultipleDateReceiver waiting:"+str);
System.out.println("reading: "+pipedInputStream);
while((iRd=pipedInputStream.read(incomingBytes))!=-1) {
if(iRd>0) {
str+=new String(incomingBytes);
}
}
System.out.println("ThreadMultipleDateReceiver Received:\n\t:"+str);
} catch(IOException e) {}
} else {
System.out.println("ThreadMultipleDateReceiver Empty");
}
}
emptyDateWriters();
}
public void addDateWriter(ThreadStreamDateWriter threadDateWriter) {
if(lThrdDate.size()<MAX_CLIENT_THREADS) {
lThrdDate.add(threadDateWriter);
}
}
private void emptyDateWriters() {
if(!lThrdDate.isEmpty()) {
for(int i=lThrdDate.size();i>0;i--) {
ThreadStreamDateWriter threadDateWriter=lThrdDate.get(i-1);
threadDateWriter.stopThread();
lThrdDate.remove(i-1);
}
}
}
public PipedInputStream getPipedInputStream() {
return pipedInputStream;
}
public void stopThread() {
isRunning=false;
}
}
public static class ThreadStreamDateWriter extends Thread {
String Self;
private byte[] outgoingBytes;
private volatile boolean isRunning;
private /*static*/ PipedOutputStream pipedOutputStream;
ThreadStreamDateWriter(String name,PipedInputStream snk) {
Self=name;
pipedOutputStream=new PipedOutputStream();
System.out.println("ctor setting pipedOutputStream to: "+pipedOutputStream);
try {
pipedOutputStream.connect(snk);
System.out.println(pipedOutputStream+" connectd to: "+snk);
} catch(IOException e) {}
}
#Override public void run() {
isRunning=true;
while(isRunning) {
try {
outgoingBytes=getInfo().getBytes();
System.out.println("ThreadStreamDateWriter -> write to pipedOutputStream:"+new String(outgoingBytes));
System.out.println("writing to: "+pipedOutputStream);
pipedOutputStream.write(outgoingBytes);
System.out.println("ThreadStreamDateWriter -> wrote:"+new String(outgoingBytes));
try {
Thread.sleep(4000);
} catch(InterruptedException ex) {}
} catch(IOException|NegativeArraySizeException|IndexOutOfBoundsException e) {
isRunning=false;
}
}
}
String getInfo() {
String sDtTm=new SimpleDateFormat("yyyyMMdd-hhmmss").format(Calendar.getInstance().getTime());
return Self+" -> "+sDtTm;
}
public void stopThread() {
isRunning=false;
}
public String getNameDateWriter() {
return Self;
}
}
private void foo() {
if(thrdMDateReceiver==null) {
thrdMDateReceiver=new ThreadMultipleDateReceiver();
thrdMDateReceiver.start();
}
if(thrdSDateWriter0==null) {
thrdSDateWriter0=new ThreadStreamDateWriter("-0-",thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter0.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter0);
}
if(thrdSDateWriter1==null) {
thrdSDateWriter1=new ThreadStreamDateWriter("-1-",thrdMDateReceiver.getPipedInputStream());
thrdSDateWriter1.start();
thrdMDateReceiver.addDateWriter(thrdSDateWriter1);
}
}
void run() throws InterruptedException {
System.out.println(("running"));
foo();
System.out.println(("sleeping"));
Thread.sleep(10000);
System.out.println(("stopping"));
if(thrdMDateReceiver!=null) {
thrdMDateReceiver.stopThread();
}
}
public static void main(String[] args) throws InterruptedException {
new So44438086().run();
}
ThreadMultipleDateReceiver thrdMDateReceiver=null;
ThreadStreamDateWriter thrdSDateWriter0=null;
ThreadStreamDateWriter thrdSDateWriter1=null;
}

Related

Java: Publisher-Observer pattern: High CPU Usage

I have a ServerSocket Thread that accepts a connection and starts a socket handler. This part seems to be working fine with no memory leaks or high cpu usage. I added a Publisher thread and an observer thread and now, my java program is reporting high CPU usage.
Subject.java:
public interface Subject {
public void attach(Observer o);
public void detach(Observer o);
public void notifyUpdate(Message m);
}
MessagePublisher.java:
public class MessagePublisher extends Thread implements Subject{
private List<Observer> observers = new ArrayList<>();
private boolean readyToPublish = false;
private ConcurrentLinkedQueue<Message> msgHolder;
public MessagePublisher(ConcurrentLinkedQueue<Message> _queue){
this.msgHolder = _queue;
}
#Override
public void attach(Observer o) {
observers.add(o);
}
#Override
public void detach(Observer o) {
observers.remove(o);
}
#Override
public void notifyUpdate(Message m) {
for(Observer o: observers) {
o.update(m);
}
}
public void run(){
this.readyToPublish = true;
while (readyToPublish)
{
try
{
Message _m = (Message)this.msgHolder.poll();
if(!_m.equals(null)){
System.out.println("Polled message: " + _m.getMessage());
System.out.println("Number of subscribers: " + observers.size());
notifyUpdate(_m);
}
}
catch(Exception j) { }
try { sleep(9); }
catch(Exception e) { }
}
EndWork();
}
public void EndWork(){
this.readyToPublish = false;
this.observers.clear();
}
}
Main.java:
public static void main(String[] args) {
// TODO code application logic here
ConcurrentLinkedQueue<Message> msgHolder = new ConcurrentLinkedQueue<Message>();
ServerSocketThread _socketThread = new ServerSocketThread(msgHolder);
_socketThread.start();
MessagePublisher _publisher = new MessagePublisher(msgHolder);
_publisher.start();
UIServerSocketThread _uiSocketThread = new UIServerSocketThread(_publisher);
_uiSocketThread.start();
}
UIServerSocketThread.java:
public class UIServerSocketThread extends Thread{
private ServerSocket objServerSocket;
private Socket objSocket;
private int iPort = 21001;
private FileHandler obj_LogFileHandler;
private Logger obj_Logger;
private int file_size = 8000000;
private int numLogFiles = 20;
private UIClientSocketThread objCltSocket;
private MessagePublisher objPublisher;
private boolean running = false;
public UIServerSocketThread(MessagePublisher _publisher){
this.running = true;
try {
this.obj_LogFileHandler = new FileHandler("uiserver.log.%g", file_size, numLogFiles);
this.obj_LogFileHandler.setFormatter(new SimpleFormatter());
}
catch ( IOException obj_Exception ) {
this.obj_LogFileHandler = null;
}
catch ( SecurityException obj_Exception ) {
this.obj_LogFileHandler = null;
}
this.obj_Logger = null;
if ( this.obj_LogFileHandler != null ) {
this.obj_Logger = Logger.getLogger("eti.logger.uiserver");
this.obj_Logger.addHandler(this.obj_LogFileHandler);
}
try {
this.objServerSocket = new ServerSocket(this.iPort);
} catch(IOException i){
//i.printStackTrace();
this.obj_Logger.info(i.getMessage());
}
this.objPublisher = _publisher;
}
public void run() {
StringBuffer sMsg;
sMsg = new StringBuffer();
while ( this.running ) {
try {
this.objSocket = this.objServerSocket.accept();
} catch(Exception e){
sMsg.append("Error accepting ui socket connection\n");
sMsg.append(e.getMessage());
this.obj_Logger.info(sMsg.toString());
}
try {
this.objCltSocket = new UIClientSocketThread(this.objSocket, this.obj_Logger);
if(!this.objPublisher.equals(null)){
this.obj_Logger.info("Attacing clientObserver");
this.objPublisher.attach(this.objCltSocket);
}
this.objCltSocket.start();
} catch(Exception r) {
sMsg.append("Error \n");
sMsg.append(r.getMessage());
this.obj_Logger.info(sMsg.toString());
}
}
this.objPublisher.EndWork();
stopServerSocketThread();
} // end run
public void stopServerSocketThread() {
try {
this.running = false;
this.objServerSocket.close();
this.objServerSocket = null;
this.obj_Logger.info("Server NOT ACCEPTING Connections!!");
} catch(Exception e ) {
this.obj_Logger.info(e.getMessage());
}
}
}
I am not sure where the issue is. Any help is appreciated.
You have two infinite loops in your application. One is in class MessagePublisher with function run(). And another one is in class UIServerSocketThread. This will cause high CPU usage.

Why isn't my client socket inputstream receiving message sent from server socket outputstream

This is the SocketServer code that generates a server thread
public class ProcessorCorresponder {
protected final static Logger logger = LogManager.getLogger( ProcessorCorresponder.class );
private static int port = Integer.parseInt(PropertiesLoader.getProperty("appserver.port") == null ? "666" : PropertiesLoader.getProperty("appserver.port"));
private static int maxConnections = Integer.parseInt(PropertiesLoader.getProperty("appserver.maxconnections") == null ? "666" : PropertiesLoader.getProperty("appserver.maxconnections"));
public static void main(String[] args) {
logger.info("Starting server .. "
+ "[port->" + port
+ ",databaseName->" + databaseName + "]");
try (ServerSocket listener = new ServerSocket();) {
listener.setReuseAddress(true);
listener.bind(new InetSocketAddress(port));
Socket server;
int i = 0;
while((i++ < maxConnections) || (maxConnections == 0)) {
server = listener.accept();
logger.debug(
"New Thread listening on " + server.getLocalAddress().toString() + ":" + server.getLocalPort()
+ ", initiated from IP => " + server.getInetAddress().toString() + ":" + server.getPort()
);
MySocketServer socSrv = new MySocketServer (server);
Thread t = new Thread( socSrv );
t.start();
}
} catch (Exception ex) {
logger.error("Error in ProcessorInterface", ex);
}
}
}
Server code: This is a thread to handle one connection, there is a program that monitors a serversocket and spins off request threads as needed.
public class MySocketServer implements Runnable {
protected final static Logger logger = LogManager.getLogger(MySocketServer.class);
private final Socket server;
// because we are using threads, we must make this volatile, or the class will
// never exit.
private volatile boolean shouldContinue = true;
private StringBuffer buffHeartbeatMessage = new StringBuffer().append((char) 0).append((char) 0).append((char) 0)
.append((char) 0).append((char) 0).append((char) 0);
private Heartbeat heartbeat = new Heartbeat(/* 60 */3000, buffHeartbeatMessage.toString());
public MySocketServer(Socket server) {
this.server = server;
}
#Override
public void run() {
try (BufferedReader in = new BufferedReader(new InputStreamReader(this.server.getInputStream()));
BufferedOutputStream out = new HeartbeatBufferedOutputStream(this.server.getOutputStream(),
heartbeat)) {
final StreamListener listener = new StreamListener(in);
listener.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getID() == ActionEvent.ACTION_PERFORMED) {
if (event.getActionCommand().equals(StreamListener.ERROR)) {
logger.error("Problem listening to stream.");
listener.setShouldContinue(false);
stopRunning();
} else {
String messageIn = event.getActionCommand();
if (messageIn == null) { // End of Stream;
stopRunning();
} else { // hey, we can do what we were meant for
logger.debug("Request received from client");
// doing stuff here
...
// done doing stuff
logger.debug("Sending Client Response");
try {
sendResponse(opResponse, out);
} catch (Exception ex) {
logger.error("Error sending response to OP.", ex);
}
}
}
}
}
});
listener.start();
while (shouldContinue) {
// loop here until shouldContinue = false;
// this should be set to false in the ActionListener above
}
heartbeat.setShouldStop(true);
return;
} catch (Exception ex) {
logger.error("Error in ESPSocketServer", ex);
return;
}
}
private void stopRunning() {
shouldContinue = false;
}
private void sendResponse(ClientResponse opResponse, BufferedOutputStream out) throws Exception {
logger.debug("Before write");
out.write(opResponse.getResponse().getBytes());
logger.debug("After write. Before flush");
out.flush();
logger.debug("After flush");
// this log message is in my logs, so I know the message was sent
}
}
My StreamListener class.
public class StreamListener extends Thread {
protected final static Logger logger = LogManager.getLogger(StreamListener.class);
public final static String ERROR = "ERROR";
private BufferedReader reader = null;
private List<ActionListener> actionListeners = new ArrayList<>();
private boolean shouldContinue = true;
public StreamListener(BufferedReader reader) {
this.reader = reader;
}
#Override
public void run() {
while (shouldContinue) {
String message;
try {
// client blocks here and never receives message
message = reader.readLine();
ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, message);
fireActionPerformed(event);
} catch (IOException e) {
e.printStackTrace();
ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, ERROR);
fireActionPerformed(event);
}
}
}
public void setShouldContinue(boolean shouldContinue) {
this.shouldContinue = shouldContinue;
}
public boolean getShouldContinue() {
return shouldContinue;
}
public boolean addActionListener(ActionListener listener) {
return actionListeners.add(listener);
}
public boolean removeActionListener(ActionListener listener) {
return actionListeners.remove(listener);
}
private void fireActionPerformed(ActionEvent event) {
for (ActionListener listener : actionListeners) {
listener.actionPerformed(event);
}
}
}
My Heartbeat class
public class Heartbeat extends Thread {
private BufferedOutputStream bos = null;
private int beatDelayMS = 0;
private String message = null;
private boolean shouldStop = false;
public Heartbeat(int beatDelayMS, String message) {
this.beatDelayMS = beatDelayMS;
this.message = message;
setDaemon(true);
}
#Override
public void run() {
if (bos == null) { return; }
while(!shouldStop) {
try {
sleep(beatDelayMS);
try {
bos.write(message.getBytes());
bos.flush();
} catch (IOException ex) {
// fall thru
}
} catch (InterruptedException ex) {
if (shouldStop) {
return;
}
}
}
}
public void setBufferedOutputStream(BufferedOutputStream bos) {
this.bos = bos;
}
public BufferedOutputStream getBufferedOutputStream() {
return bos;
}
public void setShouldStop(boolean shouldStop) {
this.shouldStop = shouldStop;
}
public boolean getShouldStop() {
return shouldStop;
}
}
My HeartbeatBufferedOutputStream
public class HeartbeatBufferedOutputStream extends BufferedOutputStream {
private Heartbeat heartbeat = null;
public HeartbeatBufferedOutputStream(OutputStream out, Heartbeat heartbeat) {
super(out);
this.heartbeat = heartbeat;
this.heartbeat.setBufferedOutputStream(this);
heartbeat.start();
}
#Override
public synchronized void flush() throws IOException {
super.flush();
heartbeat.interrupt();
}
}
And finally here is the "Client" class
public class Mockup extends Thread {
protected final static Logger logger = LogManager.getLogger(Mockup.class);
// because we are using threads, we must make this volatile, or the class will
// never exit.
private volatile boolean shouldContinue = true;
public static void main(String[] args) {
new Mockup().start();
}
#Override
public void run() {
try (Socket socket = new Socket("localhost", 16100);
BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {
final StreamListener listener = new StreamListener(in);
listener.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
if (event.getID() == ActionEvent.ACTION_PERFORMED) {
if (event.getActionCommand().equals(StreamListener.ERROR)) {
logger.error("Problem listening to stream.");
listener.setShouldContinue(false);
stopRunning();
} else {
String messageIn = event.getActionCommand();
if (messageIn == null) { // End of Stream;
stopRunning();
} else { // hey, we can do what we were meant for
// convert the messageIn to an OrderPower request, this parses the information
logger.info("Received message from server. [" + messageIn + "].");
}
}
}
}
});
listener.start();
StringBuffer buff = new StringBuffer("Some message to send to server");
logger.info("Sending message to server [" + buff.toString() + "]");
out.write(buff.toString().getBytes());
out.flush();
boolean started = false;
while (shouldContinue) {
if (!started) {
logger.debug("In loop");
started = true;
}
// loop here until shouldContinue = false;
// this should be set to false in the ActionListener above
}
logger.info("Exiting Mockup");
return;
} catch (Exception ex) {
logger.error("Error running MockupRunner", ex);
}
}
private void stopRunning() {
shouldContinue = false;
}
}
I have confirmed from logging messages that the Server sends a message to the BufferedOutputStream, and is flushed, but the Client logs indicate that it is blocked on the reader.readLine() and never gets the message.
You are reading lines but you are never writing lines. Add a line terminator to what you send.

wait()/notify() not working properly

I have a ConsumerProducer object on which I want to acquire lock from two different threads. The class is as below:
public class ConsumerProducer {
public String stringPool = null;
public void put(String s){
stringPool = s;
}
public String get(){
String ret = stringPool;
stringPool = null;
return ret;
}
}
The thread impl class is as below:
public class WaitNotifyTest implements Runnable {
private String threadType;
public ConsumerProducer cp;
public static volatile int i = 1;
public WaitNotifyTest(String threadType, ConsumerProducer cp) {
this.threadType = threadType;
this.cp = cp;
}
public static void main(String[] args) throws InterruptedException {
ConsumerProducer cp = new ConsumerProducer();
WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp);
WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp);
Thread t1 = new Thread(test1);
Thread t2 = new Thread(test2);
t1.start();
t2.start();
t1.join();
t2.join();
}
#Override
public void run() {
while (true) {
if (threadType.equalsIgnoreCase("Consumer")) {
synchronized (cp) {
try {
if (null != cp.get()) {
cp.wait();
}
consume();
System.out.println("notify from Consumer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
synchronized (cp) {
try {
if (null == cp.get()) {
cp.wait();
}
produce();
System.out.println("notify from Producer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (i == 5) {
break;
}
i++;
}
}
public void consume() {
System.out.println("Putting: Counter" + i);
cp.put("Counter" + i);
}
public void produce() {
System.out.println("getting: " + cp.get());
}
}
But when I run the code it is facing some kind of deadlock and it is stuck printing like
Putting: Counter3
notify from Consumer
Something is going terribly wrong but I am not able to identify. Please help.
Your consumer is doing producer's job and producer is doing consumer's job.
Exchange their responsibility and modify the condition to wait. Please refer to the code below.
Consumer will wait when there is nothing to get and he will release the lock of cp. So that producer has chance to go into the synchronized block.
Producer only produces when there is nothing or he will wait. After that, he will release the lock of cp. So that consumer has chance to go into the synchronized block.
Consumer is who get things away.
Producer is who put things to table.
According to your comment. You want to put Counter from 1 to 5, so you should add i++ only in Producer thread. How can you control its increase in both threads?
You don't judge whether it's consumer or producer calling the get() from cp object but assign null to stringPool. It's obvious wrong and will make consumer get null from public space. I add a new method clearString() which will set public space to null only when consumer has comsumed the product.
public class WaitNotifyTest implements Runnable {
private String threadType;
public ConsumerProducer cp;
public static volatile int i = 0;
public WaitNotifyTest(String threadType, ConsumerProducer cp) {
this.threadType = threadType;
this.cp = cp;
}
public static void main(String[] args) throws InterruptedException {
ConsumerProducer cp = new ConsumerProducer();
WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp);
WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp);
Thread t1 = new Thread(test1);
Thread t2 = new Thread(test2);
t1.start();
t2.start();
t1.join();
t2.join();
}
#Override
public void run() {
while (true) {
if (threadType.equalsIgnoreCase("Consumer")) {
synchronized (cp) {
try {
/*
* Consumer will wait when there is nothing to get and he will release the lock of cp.
* So that producer has change to go into the synchronized block.
*/
if (null == cp.get()) {
cp.wait();
}
consume();
System.out.println("notify from Consumer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
synchronized (cp) {
try {
/*
* Producer only produce when there is nothing or he will wait. At the same time, he will release the lock of cp.
* So that consumer has chance to go into the synchronized block.
*/
if (null != cp.get()) {
cp.wait();
}
i++;
produce();
System.out.println("notify from Producer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (i == 5) {
break;
}
}
}
public void consume() {
System.out.println("getting: " + cp.get());
cp.clearString();
}
public void produce() {
System.out.println("Putting: Counter" + i);
cp.put("Counter" + i);
}}
Also see the ConsumerProducer class.
public class ConsumerProducer {
public String stringPool = null;
public void put(String s){
stringPool = s;
}
public String get(){
return stringPool;
}
public void clearString(){
stringPool = null;
}
}
Updated code is here:
ConsumerProducer.java:
public class ConsumerProducer {
public volatile String stringPool = null;
public void put(String s){
this.stringPool = s;
}
public String get(){
String ret = this.stringPool;
//this.stringPool = null;
return ret;
}
//added
public void clearString(){
this.stringPool = null;
}
}
WaitNotifyTest.java
public class WaitNotifyTest implements Runnable {
private String threadType;
public ConsumerProducer cp;
public static volatile int i = 0;
public WaitNotifyTest(String threadType, ConsumerProducer cp) {
this.threadType = threadType;
this.cp = cp;
}
public static void main(String[] args) throws InterruptedException {
ConsumerProducer cp = new ConsumerProducer();
WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp);
WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp);
Thread t1 = new Thread(test1);
Thread t2 = new Thread(test2);
t1.start();
t2.start();
t1.join();
t2.join();
}
#Override
public void run() {
while (true) {
if (threadType.equalsIgnoreCase("Consumer")) {
synchronized (cp) {
try {
if (null == cp.get()) {
cp.wait();
}
consume();
System.out.println("notify from Consumer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} else {
synchronized (cp) {
try {
if (null != cp.get()) {
cp.wait();
}
i++;
produce();
System.out.println("notify from Producer");
cp.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (i == 5) {
break;
}
}
}
public void produce() {
System.out.println("Putting: Counter" + i);
cp.put("Counter" + i);
}
public void consume() {
System.out.println("getting: " + cp.get());
cp.clearString();
}
}

Directory watch, locked files

I´m try to build a application which, by threads, are listening to directory for changes. Everything works fine, but there is a little bug. I´m very new to the whole threads-thing... So, this problem probably based on my ignorance...
The program can se all the changes in the picked directory, BUT, when the threads are running, i cant modify the files inside the directory... Those are locked in the process...
I will be very happy if someone perhaps can give me some tips in how i can solve this.
Thank you in advance
DirWatch
public class DirWatch implements Runnable{
Path dirPath;
private boolean run = true;
private boolean created = false;
private boolean modified = false;
private boolean compressed = false;
private boolean isJSON = false;
/**
*
* #param path
* #throws IOException
*/
public void setWatchPath(String path) throws IOException {
dirPath = Paths.get(path);
try {
Boolean isFolder = (Boolean) Files.getAttribute(dirPath, "basic:isDirectory", new LinkOption[]{NOFOLLOW_LINKS});
if (!isFolder) {
throw new IllegalArgumentException("Path: " + dirPath + " is not a folder");
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
System.out.println("Watching path: " + path);
}
public void setDirPathWatchRun(boolean run){
this.run = run;
}
public boolean isCreated() {
return created;
}
public void setCreated(boolean created) {
this.created = created;
}
public boolean isModified() {
return modified;
}
public void setModified(boolean modified) {
this.modified = modified;
}
public boolean isCompressed() {
return compressed;
}
public void setCompressed(boolean compressed) {
this.compressed = compressed;
}
public boolean isJSON() {
return isJSON;
}
public void setJSON(boolean JSON) {
isJSON = JSON;
}
private void checkFileType(String fileName){
String extension = fileName.substring(fileName.length() - 4);
if(extension.equalsIgnoreCase(FILE_TYPE.TAR.getFileType())){
setCompressed(true);
System.out.println(extension);
}
if(extension.equalsIgnoreCase(".json")){
setJSON(true);
}
}
#Override
public void run() {
FileSystem fs = dirPath.getFileSystem ();
try(WatchService service = fs.newWatchService()) {
dirPath.register(service, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
WatchKey key = null;
while(run) {
key = service.take();
WatchEvent.Kind<?> kind = null;
for(WatchEvent<?> watchEvent : key.pollEvents()) {
kind = watchEvent.kind();
if (OVERFLOW == kind) {
System.out.println("OVERFLOW");
continue;
} else if (ENTRY_CREATE == kind) {
System.out.println("New path created: " + watchEvent.context().toString());
setCreated(true);
checkFileType(watchEvent.context().toString());
} else if (ENTRY_DELETE == kind){
System.out.println("Path deleted: " + watchEvent.context().toString());
setModified(true);
checkFileType(watchEvent.context().toString());
} else if (ENTRY_MODIFY == kind) {
System.out.println("Path modified: " + watchEvent.context().toString());
setModified(true);
checkFileType(watchEvent.context().toString());
}
}
if(!key.reset()) {
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Method Watch in another class
private void watch() throws IOException {
watchJSON = new DirWatch();
watchTAR = new DirWatch();
watchTAR.setWatchPath(serverArgs.getCompressedPath());
watchJSON.setWatchPath(serverArgs.getExtractedPath());
Runnable checkSourceActions = new Runnable() {
#Override
public void run() {
while(true) {
if (watchJSON.isCreated() || (watchJSON.isModified())) {
server();
}
if(watchTAR.isCreated() || (watchTAR.isModified())) {
extractFiles(fileHandler);
createManifest(fileHandler);
server();
}
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread thread1 = new Thread(watchJSON);
Thread thread3 = new Thread(watchTAR);
Thread thread2 = new Thread(checkSourceActions);
thread1.start();
thread2.start();
thread3.start();
}
When I try to change the file while the program is running

java semaphore idiom

I have a thread which does an action only when it gets exclusive access to 2 semaphores.
public void run(){
boolean a1=false;
boolean a2=false;
boolean a3=false;
while(true){
try{
if(res[1].tryAcquire()==true){
a1=true;
if((res[2].tryAcquire()==true){
a2=true;
if(res[3].tryAcquire()==true)){
a3=true;
System.out.println("Rolled the tobacco");
}
}
}
}
finally{
if(a1){
a1=false;
res[1].release();
}
if(a2){
a2=false;
res[2].release();
}
if(a3){
a3=false;
res[3].release();
}
}
}
}
}
Is there a better way to write this to make sure we do not upset the semaphore acquired count?
Is there a way to check if a semaphore is acquired by the current thread?
In Java 7 a try with Closeable is possible. There certainly must be nicer solutions.
public class Region implements Closeable {
private final Semaphore semaphore;
public Region(Semaphore semaphore) {
this.semaphore = semaphore;
if (!semaphore.tryAcquire()) {
throw NotAcquiredException(semaphore);
}
}
#Override
public void close() {
semaphore.release();
}
}
public class NotAcquiredException extends Exception { ... }
Usage:
public void run() {
boolean a1 = false;
boolean a2 = false;
boolean a3 = false;
while (true) {
try (Closeable r1 = new Region(res[1])) {
a1 = true;
try (Closeable r2 = new Region(res[2])) {
a2 = true;
try (Closeable r3 = new Region(res[3])) {
a3 = true;
System.out.println("Rolled the tobacco");
} catch (IOException e) {
}
} catch (IOException e) {
}
} catch (IOException e) {
}
}
You could separate each acquire into a try...finally, not shorter, but gets rid of some variables and makes it fairly obvious what should happen for each lock. (I changed the array to zero based)
public void run(){
while(true){
if(res[0].tryAcquire()){
try {
if(res[1].tryAcquire()) {
try {
if(res[2].tryAcquire()){
try {
System.out.println("Rolled the tobacco");
} finally {
res[3].release();
}
}
} finally {
res[2].release();
}
}
} finally{
res[1].release();
}
}
}
}
If you need to acquire a lot of locks or do this in several places, then maybe a helper class would be nice. At least hides the boilerplate code of acquire and releasing the semaphores.
public void run() {
SemaphoreHelper semaphoreHelper = new SemaphoreHelper(res);
while (true) {
try {
if (semaphoreHelper.aquireAll()) {
System.out.println("Rolled the tobacco");
}
} finally {
semaphoreHelper.releaseAquired();
}
}
}
private static class SemaphoreHelper {
private final Semaphore[] semaphores;
private int semaphoreIndex;
public SemaphoreHelper(Semaphore[] semaphores) {
this.semaphores = semaphores;
}
public void releaseAquired() {
while (semaphoreIndex > 0) {
semaphoreIndex--;
semaphores[semaphoreIndex].release();
}
}
public boolean aquireAll() {
while (semaphoreIndex < semaphores.length) {
if (!semaphores[semaphoreIndex].tryAcquire()) {
return false;
}
semaphoreIndex++;
}
return true;
}
}

Categories

Resources