2016年11月9日水曜日

Thread 練習

Thread 練習

https://paiza.io/projects/xrf-cg825CeFPxNDEjnKlg


import java.io.*;
import java.net.*;

public class Main {
public static void main(String[] args) throws Exception {
int i = 0;
for (i = 0; i < 90; i++) {
new Thread(new Runnable() {
public void run() {
long x = Thread.currentThread().getId();
new D().Down(x);
}
} ).start();
}
}
}

class D {
public D() {
}
public void Down(Long x) {
try {
URL u = new URL("http://www.yahoo.co.jp");
HttpURLConnection c = (HttpURLConnection)u.openConnection();
c.setConnectTimeout(3000);
c.setReadTimeout(3000);
c.setDoInput(true);
InputStream is = null;
c.setRequestMethod("GET");
if (c.getResponseCode() == 200) {
is = c.getInputStream();
BufferedReader br = new BufferedReader( new InputStreamReader(is));
System.out.println(Thread.currentThread().getName() + "  :  " + br.readLine());
// byte[] buf = new byte[1024];
// int len = 0;
// while((len = is.read(buf)) != -1 ){
//     System.out.println("Thread:"+ x +"->"+ new String(buf,0,len));
//     System.out.println(x);
// }
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

0 件のコメント: