java - Forcing server to wait because image haven't finished writing to disk -
i have website when user uploads pdf file, gets sent server, pdf rendering, , creates .png file display pdf preview thumbnail.
the problem when .png created, write disk slower whole execution, causes image show on site error because tried access image isn't there (because code finished before write disk finished).
i added thread.sleep(2000)
after .png creation , seems fix problem. question is, situation thread.sleep(2000)
best code use? don't know if code effect multiple users accessing site , uploading files.
let's usera uploads file while userb browses normally. after usera uploads file, userb experience 2 second delay?
is there better way me pause code execution in situation this?
edit: added code. using pdf-renderer library: https://java.net/projects/pdf-renderer
public static void convertpdftoimage () throws ioexception { randomaccessfile raf = new randomaccessfile (new file (rep.pathsamplepdf), "r"); filechannel fc = raf.getchannel (); bytebuffer buf = fc.map (filechannel.mapmode.read_only, 0, fc.size ()); pdffile pdffile = new pdffile (buf); pdfpage firstpage = pdffile.getpage(1); rectangle2d r2d = firstpage.getbbox (); int width = (int) r2d.getwidth (); int height = (int) r2d.getheight (); image pdfimage = firstpage.getimage(width, height, r2d, null, true, true); bufferedimage imagetosave = (bufferedimage) pdfimage; file outputfile = new file("public/pdfpreview/" + somename + ".png"); imageio.write(imagetosave, "png", outputfile); //trying delay part can finish writing disk thread.sleep(2000) //if put code here, fixes problem. }
let's usera uploads file while userb browses normally. after usera uploads file, userb experience 2 second delay?
userb experience delay if userb's , usera's requests being handled same thread, not case since web servers use separate thread handle each connection.
thread.sleep(2000) best code use?
no, not way of doing things. aside tying cpu, explicitly having thread wait 2 seconds before attempting access file when have no guarentee file ready @ point. better try leverage java's wait() , notify() object methods concurrency. however, not know if necessariy since should know when write disk complete (a java method return).
Comments
Post a Comment