日期:2014-05-20 浏览次数:20768 次
/** * 测试Thread类线程 * @author mfcai */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class OpenConn implements Runnable { private HttpURLConnection conn; private URL url; // Thread thread1; public OpenConn(String url) throws MalformedURLException { this.url = new URL(url); } public void run() { try { conn = (HttpURLConnection) url.openConnection(); } catch (IOException e) { e.printStackTrace(); url = null; } } public HttpURLConnection getConn() { return conn; } public static void main(String[] args) throws Exception { // 创建Runnable类 OpenConn openConn = new OpenConn("http://www.google.com"); // 创建线程 Thread thread = new Thread(openConn); // openConn.thread1=thread; thread.start(); thread.join(10000);// wait 10 seconds HttpURLConnection c = openConn.getConn(); if (c != null) { System.out.println("连接网络成功..."); BufferedReader r = new BufferedReader(new InputStreamReader(c .getInputStream())); String s = r.readLine(); while (s != null) { System.out.println(s); s = r.readLine(); } } else { System.out.println("超时错误,连接网络失败..."); } } }
/** * 测试Runnable接口线程 * @author mfcai */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class OpenConn2 extends Thread { private HttpURLConnection conn; private URL url; public OpenConn2(String url) throws MalformedURLException { this.url = new URL(url); } public void run() { try { conn = (HttpURLConnection) url.openConnection(); } catch (IOException e) { e.printStackTrace(); url = null; } } public HttpURLConnection getConn() { return conn; } public static void main(String[] args) throws Exception { OpenConn2 openConn = new OpenConn2("http://www.google.com"); openConn.start(); openConn.join(10000);// wait 10 seconds HttpURLConnection c = openConn.getConn(); if (c != null) { System.out.println("连接网络成功..."); BufferedReader r = new BufferedReader(new InputStreamReader(c .getInputStream())); String s = r.readLine(); while (s != null) { System.out.println(s); s = r.readLine(); } } else { System.out.println("超时错误,连接网络失败..."); } } }
/** * 测试匿名线程线程创建 * @author mfcai */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class OpenConn3 { private HttpURLConnection conn; private URL url; public static void main(String[] args){ try{ OpenConn3 open3= new OpenConn3(); open3.strartMyThread(); }catch(Exception ex){ System.out.println(ex.toString()); } } public void strartMyThread() throws Exception{ this.url =new URL("http://www.google.com"); java.lang.Runnable runner=new Runnable(){ public void run(){ try { conn = (HttpURLConnection) url.openConnection(); } catch (IOException e) { e.printStackTrace(); url = null; } } }; Thread openConn=new Thread(runner); openConn.start(); openConn.join(10000);// wait 10 seconds if (conn != null) { System.out.println("连接网络成功..."); BufferedReader r = new BufferedReader(new InputStreamReader(conn .getInputStream())); String s = r.readLine(); while (s != null) { System.out.println(s); s = r.readLine(); } } else { System.out.println("超时错误,连接网络失败..."); } } }