日期:2014-05-20  浏览次数:20768 次

使用线程创建的三种方式实现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 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接口创建
/** 
* 测试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.以匿名内部类的方式创建
即在一个方法中创建线程,当方法被调用时,线程即启动,如下:
/** 
* 测试匿名线程线程创建 
* @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("超时错误,连接网络失败...");
 		}

	 }
}


转载请注明作者和出处。

1 楼 hemin108 2009-07-06  
呵呵 的确遇到过这类问题,谢谢分享
另外想问下 怎么实现联网后取得返回的信息呢,例如我做个手机归属地查询,怎么取得返回的归属地信息?
2 楼 wkklun 2010-01-28  
很好的代码,线程的三种实现方式都实现了