1

I have two methods in one class Method 1:Write BufferedImage data to a each folder inside directory B so that in each folder there are 1 image file.

BufferedImage Array = ...;
for(int a = 0;a < B.listfiles().length;a++)
   write Arrray[a] to B.listfiles()[a];      

Method 2: Write data already created from each folder in B to each folder in directory C

  for(int a = 0;a < C.listfiles().length;a++)
   write B.listfiles()[a] to C.listfiles()[a]

How do i implement Multithreading so that an image data can be transferred to a folder in C right after an image is written to a folder in B something like this:

write to folder 1 in B
write to folder 1 in C using data just created from folder 1.
write to folder 2 in B
write to folder 2 in C using data just created from folder 2.
loc tri ho
  • 11
  • 1

2 Answers2

1

I would use a queue in C that is accessed by B. So, as soon as B finishes with a file, it adds it to a queue for C to pull from and do its thing. That is the simplest answer.

for (int a = 0; a < B.listfiles().length; a++) {
   write Array[a] to B.listfiles()[a];
   boolean callNotify = C.queuedfiles().isEmpty();
   queue Array[a] to C.queuedfiles();
   if (callNotify) notify();
}

while(true) {
   if (C.queuedfiles().isEmpty()) wait();
   write C.queuedfiles().poll() to C.listfiles()[a];
}

Something like this. You then need to implement something for terminating C once B is finished

czifro
  • 784
  • 7
  • 24
0

This looks like a producer/consumer problem. Here are some examples of how you can implement that:

  1. Java BlockingQueue Example implementing Producer Consumer Problem
  2. Producer Consumer Design Pattern with Blocking Queue Example in Java
  3. Producer/Consumer threads using a Queue
Community
  • 1
  • 1
marcospereira
  • 12,045
  • 3
  • 46
  • 52