使用線程創建的三種方法實現j2me聯網(PS:我想做android!NND!)

在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. 轉自javaeye mfcai
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章