使用线程创建的三种方式实现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("超时错误,连接网络失败...");
}

}
}


转载请注明作者和出处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值