2016-01-23 50 views
0

我在一个类中有两种方法 方法1:将BufferedImage数据写入目录B中的每个文件夹,以便在每个文件夹中有1个图像文件。如何在java多线程中实现上下文切换

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

方法2:写已经从每个文件夹的B中的目录的C创建到每个文件夹数据

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

如何实现多线程,使得图像数据可以在C被转移到文件夹之后的图像被写入到文件夹B中是这样的:

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. 

回答

0

我会在C使用队列由访问0。因此,只要B完成一个文件,它就会将它添加到队列中以便从中取出并执行其操作。这是最简单的答案。

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]; 
} 

就像这样。然后你需要实施一些终止C一次B完成