I have the following multi thread scenario and the codes. It looks kind of messy and want to know if I can make it better or if there is any flaw to fix.
When a request arrives, one thread downloads it and the remaining operations are dependent on download so it needs to wait for download to finish. But there can be many request coming and download tasks must run at the same time but each process operation need to wait for its own download task. So I did the following.
ProcessCompletedListener
public interface ProcessCompletedListener {
void onComplete(Object object);
}
RequestListener
public interface RequestListener {
void onRequest(Object request);
}
Receiver class where I send request for testing purpose.
public class Receiver {
private RequestListener requestListener;
public void setRequestListener(RequestListener requestListener) {
this.requestListener = requestListener;
}
public void requestBomb() {
String[] names = new String[]{"a", "b", "c"};
int i = 0;
while (i < 3) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
requestListener.onRequest(names[i]);
i++;
}
}
}
Custom blocking queue
public class CustomBlockingQueue {
private BlockingQueue<Object> blockingQueue = new LinkedBlockingDeque<>();
private ProcessCompletedListener processCompletedListener;
public boolean offerAndProcess(Object object) {
if (blockingQueue.offer(object)) {
process(object);
return true;
}
return false;
}
public void setProcessCompletedListener(ProcessCompletedListener processCompletedListener) {
this.processCompletedListener = processCompletedListener;
}
private void process(Object object) {
new Thread(() -> {
System.err.println(object + ": Request process started.");
try {
Thread.sleep(6000); // mock for real operation
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(object + ": Request process completed.");
if (processCompletedListener != null) {
processCompletedListener.onComplete(object);
}
}).start();
}
}
Demo class
public class Demo {
public CustomBlockingQueue customBlockingQueue;
public void setCustomBlockingQueue(CustomBlockingQueue customBlockingQueue) {
this.customBlockingQueue = customBlockingQueue;
}
public CustomBlockingQueue getCustomBlockingQueue() {
return customBlockingQueue;
}
public static void main(String[] args) {
Demo demo = new Demo();
demo.setCustomBlockingQueue(new CustomBlockingQueue());
Receiver receiver = new Receiver();
Thread t = new Thread(() -> {
receiver.setRequestListener((request) -> {
new Thread(() ->
demo.getCustomBlockingQueue().setProcessCompletedListener((Object object) -> {
System.err.println(object + ": post process started");
try {
Thread.sleep(1500); // mock for real operation
} catch (InterruptedException e) {
e.printStackTrace();
}
System.err.println(object + ": post process completed");
})).start();
demo.getCustomBlockingQueue().offerAndProcess(request);
});
});
t.start();
receiver.requestBomb();
}
}
a
could be asked while, it has been started already? So the process/callback should be queued? \$\endgroup\$