java - Why is multithreading slowing down -


i'm trying save byte array of rgb values png image follows:

byte[] imgarray = ...; int canvassize = 512;  colormodel c = new componentcolormodel(colorspace.getinstance(colorspace.cs_gray), null, false, false, transparency.opaque, databuffer.type_byte);  image image = toolkit.getdefaulttoolkit().createimage(             new memoryimagesource(canvassize, canvassize, c, imgarray, 0, canvassize));  bufferedimage bimage = new bufferedimage(canvassize, canvassize, bufferedimage.type_byte_gray);  // draw image on buffered image graphics2d bgr = bimage.creategraphics(); bgr.drawimage(image, 0, 0, null); //this takes time bgr.dispose();  imageio.write(bimage, "png", new file(uniquefilename)); 

i'm using fixedthreadpool save multiple images simultaneously. more threads use (up number of free cores have on computer), longer saving process takes. running on 6 threads takes twice long running on 1 thread.

why taking longer multiple threads? memory swaps? can avoid problem?

also, if there better way me saving png array, please let me know.

edited show pictures being saved distinct images, not overwriting each other.

i think caused different type of optimization. trying save multiple images @ once in 1 path - means requirement of queuing saving operations - io-bound task, not cpu-bound. multiple saving threads may not helpful here. within small (in terms of cpu power requirements) operations delegating threads job may give overhead resulting in extending time needed finish task, not shortening. hope helps :)


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -