使用线程创建的三种方式实现j2me联网功能
在j2me联网时,在eclipse控制台下有一个警告:“若要避免潜在的死锁,应该在commandAction()处理程序之外的其他线程中执行可能会阻塞的,操作(如网络连接)。
为了避免潜在的死锁,我们经常把把联网的代码部分写到一个线程中去执行。
而实际应用中,网络连接的事务也都是使用一个单独的线程进行的。
线程的创建可以使用Runnable接口来实现,也可以使用Thread类实现,还可以以匿名内部类的方式创建。在本文中,分别用这三种方式给出了j2me联网的例子。
代码均测试通过。可以直接使用。
1.通过继承Thread类创建
/** * 测试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 secondsHttpURLConnection 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 secondsHttpURLConnection 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("超时错误,连接网络失败..."); } }}