i writing code homework, not familiar writing multi-threaded applications. learned how open thread , start it. better show code.
(int = 0; < a.length; i++) { download(host, port, a[i]); scan.next(); }
my code above connects server opens a.length
multiple parallel requests. in other words, download opens a[i]
connections same content on each iteration. however, want server complete download method when i = 0
, start next iteration i = 1
, when the threads download has opened completes. did scan.next() stop hand not nice solution. how can that?
edit:
public static long download(string host, int port) { new java.io.file("folder_" + n).mkdir(); n--; int totallength = length(host, port); long result = 0; arraylist<httpthread> list = new arraylist<httpthread>(); (int = 0; < totallength; = + n + 1) { httpthread t; if (i + n > totallength) { t = (new httpthread(host, port, i, totallength - 1)); } else { t = new httpthread(host, port, i, + n); } list.add(t); } (httpthread t : list) { t.start(); } return result; }
and in httpthread;
public void run() { init(host, port); downloaddata(low, high); close(); }
note: our test web server modified web server, gets range: i-j
, in response, there contents of i-j
files.
you need call join()
method of thread doing downloading. cause current thread wait until download thread finished. this post on how use join.
if you'd post download method more complete solution
edit:
ok, after start threads need join them so:
for (httpthread t : list) { t.start(); } (httpthread t : list) { t.join(); }
this stop method returning until httpthreads have completed
Comments
Post a Comment