我正在做一個並行的圖像處理程序,我有4個內核,所以當我在少於5個的線程上運行程序時,應該比僅在1個線程上運行時有所改進,我想那裏沒有任何改進,因爲圖片可能太小,所以由於線程而導致開銷。所以我用8142x2175圖片仍然沒有任何改進。問題來自Eclipse嗎?當我添加更多的線程處理時間增加?
這是我的主要圖像處理程序,我包括類之一,你可以看到我使用Java和Eclipse的運行它。
public class Main {
public static void main(String[] args) throws IOException {
final File imageFile= new File ("C:\\Users\\user\\Desktop\\Parallel\\pic8142x2175.jpg");
BufferedImage orgImg= ImageIO.read(imageFile);
// the destination buffered image
BufferedImage destImg = new BufferedImage(orgImg.getWidth(), orgImg.getHeight(),
BufferedImage.TYPE_INT_RGB);
// let the user enter number of thread
System.out.println(" please enter number of threads");
Scanner in = new Scanner(System.in);
int nThreads=in.nextInt();
// create the threads
ExecutorService executor = Executors.newFixedThreadPool(nThreads);
// assign each thread with its own runnable
for (int i=0; i<nThreads;i++){
//Runnable gsr = new GreyscaleRunnable(orgImg, i, nThreads,destImg);
//Runnable pr = new PixelateRunnable(orgImg,20,i,nThreads,destImg);
Runnable fr= new FlipRunnable(orgImg,i,nThreads,destImg);
executor.execute(fr);
}// end of for loop
// This will make the executor accept no new threads
// and finish all existing threads in the queue
executor.shutdown();
// Wait until all threads are finish
}
}
FlipRunnable類
public class FlipRunnable implements Runnable{
private BufferedImage img;
private BufferedImage dest;
private int my_rank, thread_count;
// constructor of the class
public FlipRunnable(BufferedImage img,int my_rank,int thread_count,BufferedImage dest) {
this.img=img;
this.my_rank=my_rank;
this.thread_count=thread_count;
this.dest=dest;
}
long startTime = System.currentTimeMillis();
@Override
public void run() {
int localRows = img.getHeight()/thread_count;
int myFirstRow = my_rank * localRows;
int myLastRow = (my_rank + 1) * localRows - 1;
for (int i = myFirstRow; i <= myLastRow; i++) {
for (int j = 0; j < img.getWidth(); j++){
int px = img.getRGB(j, i);
dest.setRGB(img.getWidth() - j - 1, i, px);
}// end of j loop
}// end of i loop
String dstName = "C:\\Users\\user\\Desktop\\Parallel\\funFlipped.jpg";
File dstFile = new File(dstName);
try {
ImageIO.write(dest, "jpg", dstFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Output image: " + dstName);
// the total time
long endTime = System.currentTimeMillis();
long totalTime = endTime - startTime;
System.out.println("time is:"+totalTime);
}
}
首先,每個工人似乎覆蓋目標圖像... –
另外,'setRGB()''是synchronized',所以必須有大量爭的工人之間。看起來你更好地將圖像複製到普通數組並在那裏工作。 –
是的,我希望工作人員互相覆蓋,這樣我就可以得到整個圖像,而不僅僅是線程工作的部分。對不起我的英語,我試圖說的是,當我沒有讓工人覆蓋,我的輸出只是圖像的一部分,這樣我就完成了整個圖像 – user3099142