java - Start two threads in the same time but avoid using join after, this is for android program -
i have 2 threads starting @ same time in android program downloading packets.
serverip = wifiserver; thread thread22 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread22.start(); serverip = mobileserver; thread thread23 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread23.start();
i want download files 2 servers @ same time. if use under every thread thread.joim();
serverip = wifiserver; thread thread22 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread22.start(); thread22.join(); serverip = mobileserver; thread thread23 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread23.start(); thread23.join();
the code not parallel serialized. , don't want this. know join waiting finish first , after continue other.
when take out thread.join(); downloading whatever wants , not want download. , using first thread , isn't going second.
how can avoid join? because need thread call connection. if need more details tell me put.
here main code.
queue<string> fileparts = new linkedlist<string>(); time = systemclock.currentthreadtimemillis(); long time2 = system.currenttimemillis(); starttime = time2; int intnumbofchunks = integer.parseint(numbofchunks); intnumbofchunks = 250; (int = 0; < intnumbofchunks; i++) { filetoclaim = ""; filetoclaim = "bigbang.mp4"; filetoclaim = string.format("%s.part%06d", filetoclaim, i); fileparts.add(filetoclaim); } int i=0; while (!fileparts.isempty()) { filetoreceive = fileparts.peek(); serverip = wifiserver; thread thread22 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread22.start(); thread22.start(); try { thread22.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } serverip = mobileserver; thread thread23 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread23.start(); try { thread22.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } thread thread3 = new thread(new join(filetoreceive)); thread3.start(); try { thread3.join(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } fileparts.remove();}
the downloader thread
public class downloader { final string filetoreceive; boolean filedownloaded = false; socket clientsocket; public downloader(string filetoreceive) { this.filetoreceive = filetoreceive; } public void download(string serverip) { //socket clientsocket = null; try { system.out.println("kanw connect me ti porta 3248"); clientsocket = new socket(serverip, 3248); system.out.println("egine sundesi me ti porta 3248"); system.out.println("stelnw " + filetoreceive); bufferedwriter buffwriter = new bufferedwriter( new outputstreamwriter(clientsocket.getoutputstream())); buffwriter.write(filetoreceive + "\n"); buffwriter.flush(); system.out.println("esteila " + filetoreceive); inputstream = clientsocket.getinputstream(); file root = environment.getexternalstoragedirectory(); system.out.println("dimiourgo fakelo vid "); file dir = new file(root.getabsolutepath() + "/vid/"); // file dir = new file (root.getabsolutepath()); dir.mkdirs(); system.out.println("arxizw tin lipsei tou chunk"); byte[] longbytes = new byte[8]; readfully(clientsocket, longbytes); bytebuffer bb = bytebuffer.wrap(longbytes); long filelength = bb.getlong(); system.out.println("apothikeusi arxeiou"); // save @suppresswarnings("resource") fileoutputstream f = new fileoutputstream(new file(dir, filetoreceive)); byte[] buffer = new byte[1024]; int len1 = 0; int readbytes = 0; while ((len1 = is.read(buffer)) > 0) { f.write(buffer, 0, len1); readbytes += len1; } if (readbytes == filelength) { this.filedownloaded = true; } system.out.println("receiving file " + filetoreceive); system.out.println("file recieved"); } catch (ioexception ex) { system.out.println("client stopped"); ex.printstacktrace(); } { try { if (clientsocket != null && !clientsocket.isclosed()) { clientsocket.close(); } } catch (ioexception e) { e.printstacktrace(); } } } private void readfully(socket clientsocket, byte[] buffer) throws ioexception { system.out.println("readfully"); inputstream inputstream = clientsocket.getinputstream(); int remaining = buffer.length; int read = 0; while (remaining > 0) { read = inputstream.read(buffer, buffer.length - remaining, remaining); if (read < 0) { throw new ioexception(); } remaining -= read; } system.out.println("exit"); } public boolean isdownloaded() { return this.filedownloaded; } }
calldownloader class thread
public void run() { system.out.println("connecting tou "+serverip); downloader d = new downloader(filetoreceive); //downloader1 d1 = new downloader1(filetoreceive); d.download(serverip); }
if threads running concurrently, there no way sure files received in specific order... received. once of threads end, safe assume of files present.
//calling start() on of threads before join() causes them work concurrently. serverip = wifiserver; thread thread22 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread22.start(); serverip = mobileserver; thread thread23 = new thread (new calldownloader(filetoreceive, serverip, wifiserver, mobileserver)); thread23.start(); //the files downloading concurrently in threads now. //this code waits of threads finish. thread23.join(); thread22.join(); //at point safe assume of files have been downloaded. append each chunk file entire file.
Comments
Post a Comment