按日归档: 2010/06/03

[转]使用线程创建的三种方式实现j2me联网功能

src: http://www.javaeye.com/topic/420224

在j2me联网时,在eclipse控制台下有一个警告:“若要避免潜在的死锁,应该在commandAction()处理程序之外的其他线程中执行可能 会阻塞的,操作(如网络连接)。
为了避免潜在的死锁,我们经常把把联网的代码部分写到一个线程中去执行。
而实际应用中,网络连接的事务也都是使用一个单独的线程进行的。

线程的创建可以使用Runnable接口来实现,也可以使用Thread类实现,还可以以匿名内部类的方式创建。在本文中,分别用这三种方式给出 了j2me联网的例子。
代码均测试通过。可以直接使用。
1.通过继承Thread类创建

Java 代码
  1. /**  
  2. * 测 试Thread类线程  
  3. * @author mfcai  
  4. */   
  5.   import java.io.BufferedReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.io.Reader;  
  9. import java.net.HttpURLConnection;  
  10. import java.net.MalformedURLException;  
  11. import java.net.URL;  
  12.   
  13. public class OpenConn implements Runnable {  
  14.     private HttpURLConnection conn;  
  15.     private URL url;  
  16.   
  17.     // Thread thread1;  
  18.   
  19.     public OpenConn(String url) throws MalformedURLException {  
  20.         this.url = new URL(url);  
  21.     }  
  22.   
  23.     public void run() {  
  24.         try {  
  25.             conn = (HttpURLConnection) url.openConnection();  
  26.         } catch (IOException e) {  
  27.             e.printStackTrace();  
  28.             url = null;  
  29.         }  
  30.     }  
  31.   
  32.     public HttpURLConnection getConn() {  
  33.         return conn;  
  34.     }  
  35.   
  36.     public static void main(String[] args) throws Exception {  
  37.         // 创建Runnable类  
  38.         OpenConn openConn = new OpenConn("http://www.google.com");  
  39.         // 创建线程  
  40.         Thread thread = new Thread(openConn);  
  41.         // openConn.thread1=thread;  
  42.         thread.start();  
  43.         thread.join(10000);// wait 10 seconds  
  44.         HttpURLConnection c = openConn.getConn();  
  45.         if (c != null) {  
  46.             System.out.println("连接网络成功…");  
  47.             BufferedReader r = new BufferedReader(new InputStreamReader(c  
  48.                     .getInputStream()));  
  49.             String s = r.readLine();  
  50.             while (s != null) {  
  51.                 System.out.println(s);  
  52.                 s = r.readLine();  
  53.             }  
  54.         } else {  
  55.             System.out.println("超时错误,连接网络失败…");  
  56.         }  
  57.     }  
  58. }  
/**  * 测试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("超时错误,连接网络失败...");   }  } }

2.通过引用Runnable接口创建

Java 代码
  1. /**  
  2. * 测 试Runnable接口线程  
  3. * @author mfcai  
  4. */   
  5. import java.io.BufferedReader;  
  6. import java.io.IOException;  
  7. import java.io.InputStreamReader;  
  8. import java.io.Reader;  
  9. import java.net.HttpURLConnection;  
  10. import java.net.MalformedURLException;  
  11. import java.net.URL;  
  12.   
  13. public class OpenConn2 extends Thread {  
  14.     private HttpURLConnection conn;  
  15.     private URL url;  
  16.   
  17.     public OpenConn2(String url) throws MalformedURLException {  
  18.         this.url = new URL(url);  
  19.     }  
  20.   
  21.     public void run() {  
  22.         try {  
  23.             conn = (HttpURLConnection) url.openConnection();  
  24.         } catch (IOException e) {  
  25.             e.printStackTrace();  
  26.             url = null;  
  27.         }  
  28.     }  
  29.   
  30.     public HttpURLConnection getConn() {  
  31.         return conn;  
  32.     }  
  33.   
  34.     public static void main(String[] args) throws Exception {  
  35.         OpenConn2 openConn = new OpenConn2("http://www.google.com");  
  36.         openConn.start();  
  37.         openConn.join(10000);// wait 10 seconds  
  38.         HttpURLConnection c = openConn.getConn();  
  39.         if (c != null) {  
  40.             System.out.println("连接网络成功…");  
  41.             BufferedReader r = new BufferedReader(new InputStreamReader(c  
  42.                     .getInputStream()));  
  43.             String s = r.readLine();  
  44.             while (s != null) {  
  45.                 System.out.println(s);  
  46.                 s = r.readLine();  
  47.             }  
  48.         } else {  
  49.             System.out.println("超时错误,连接网络失败…");  
  50.         }  
  51.     }  
  52. }  
/**  * 测试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("超时错误,连接网络失败...");   }  } }

3.以匿名内部类的方式创建
即在一个方法中创建线程,当方法被调用时,线程即启动,如下:

Java 代码
  1. /**  
  2. * 测 试匿名线程线程创建  
  3. * @author mfcai  
  4. */   
  5.   
  6.   
  7. import java.io.BufferedReader;  
  8. import java.io.IOException;  
  9. import java.io.InputStreamReader;  
  10. import java.net.HttpURLConnection;  
  11. import java.net.URL;  
  12.   
  13. public class OpenConn3 {  
  14.     private HttpURLConnection conn;  
  15.     private URL url;  
  16.       
  17.     public static void main(String[] args){  
  18.         try{  
  19.             OpenConn3 open3= new OpenConn3();  
  20.             open3.strartMyThread();  
  21.         }catch(Exception ex){  
  22.             System.out.println(ex.toString());  
  23.         }  
  24.     }  
  25.      public void strartMyThread() throws Exception{   
  26.          this.url =new URL("http://www.google.com");  
  27.          java.lang.Runnable runner=new Runnable(){  
  28.              public void run(){   
  29.                  try {  
  30.                         conn = (HttpURLConnection) url.openConnection();  
  31.                     } catch (IOException e) {  
  32.                         e.printStackTrace();  
  33.                         url = null;  
  34.                     }  
  35.              }  
  36.   
  37.          };  
  38.          Thread openConn=new Thread(runner);   
  39.          openConn.start();   
  40.          openConn.join(10000);// wait 10 seconds  
  41.   
  42.         if (conn != null) {  
  43.             System.out.println("连接网络成功…");  
  44.             BufferedReader r = new BufferedReader(new InputStreamReader(conn  
  45.                     .getInputStream()));  
  46.             String s = r.readLine();  
  47.             while (s != null) {  
  48.                 System.out.println(s);  
  49.                 s = r.readLine();  
  50.             }  
  51.         } else {  
  52.             System.out.println("超时错误,连接网络失败…");  
  53.         }  
  54.   
  55.      }  
  56. }  
/**  * 测试匿名线程线程创建  * @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("超时错误,连接网络失败...");    }    } }

转载请注明作者和出处。

Read: 1072