java - How to do multiple tasks at once? -
how make download 2 3 files @ once , when 1 finishes picks another? right needs do, takes while if you're downloading 30 videos @ once, want download 2 or 3 @ time.
try { url url; byte[] buf; int byteread, bytewritten = 0; url = new url(getfinallocation(faddress));; outstream = new bufferedoutputstream(new fileoutputstream(destinationdir + "\\" + localfilename)); ucon = url.openconnection(); = ucon.getinputstream(); buf = new byte[size]; while ((byteread = is.read(buf)) != -1) { outstream.write(buf, 0, byteread); bytewritten += byteread; } system.out.println("downloaded successfully."); //system.out.println("file name:\"" + localfilename + "\"\nno ofbytes :" + bytewritten); } catch (exception e) { e.printstacktrace(); } { try { is.close(); outstream.close(); } catch (ioexception e) { e.printstacktrace(); } } }
you add code class implements runnable. code go in method run(). (you need implement method per runnable interface.)
you create new threads , start them passing runnable.
thread thread = new thread(new runnableclass()); thread.start(); you'll need implement logic passing faddress string runnableclass. (via constructor, or method gets called before thread.start().)
does started?
edit - added example
public class main { public static void main(string[] args) { thread thread1 = new thread(new myrunnable("http://someaddress")); thread1.start(); thread thread2 = new thread(new myrunnable("http://otheraddress")); thread2.start(); } public static class myrunnable implements runnable { string address; public myrunnable(string address) { this.address = address; } @override public void run() { // code here can access address } } }
Comments
Post a Comment