J2ME中多线程网络连接编程的分析,与设计

关键字: J2ME   j2me 网络连接,多线程    

注意。本文引用http://www.j2medev.com网上一些内容,只是出于文章的连贯性,请见谅
引言
 
  J2ME(Java 2 Micro Edition)是Java 2的一个组成部分,它与J2SE、J2EE并称。J2ME是一种高度优化的Java运行环境,主要针对消费类电子设备的,例如蜂窝电话、可视电话、数字机 顶盒和汽车导航系统等等。即J2ME是为消费电子产品和手持设备量身定制的Java专用版本。
J2ME的出现使开发跨平台的消费类电子产品的应用软件成为可能。Java语言的与平台无关的特性移植到小型电子设备上,允许移动无线设备之间 共享应用程序。它提供了基于HTTP的高级Internet协议,使移动电话能以Client/Server方式直接访问Internet的全部信息,从 而使得不同的Client访问不同的资源。

  在将来的无线通信时代中,大量的网络应用程序将被开发出来去满足无线移动通讯的要求,而要充分的发挥无线移动通讯设备的通信能力,J2ME网络编程就变得尤为重要。那么为了高效地进行网络编程,就要利用Java语言的多线程编程机制。

下面我将给出代码来怎么才可以设计一个好的多线程连网。多线程在设计中的地位是很高的,也是比较难设计好的,没经验的人很容易就使程序产生死锁,崩溃等(在下现在还经常这样^_^)

我的例子是基于socket。
1。数据发送线程SenderTask,

代码
  1. package org.wuhua.net;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.OutputStream;  
  5.   
  6. /**  
  7.  * <b>类名:SenderTask.java</b> </br> 编写日期: 2006-7-16 <br/> 程序功能描述�  
  8.  * 发��命令线程��如果OputStream 出现异常,则断开�有连�<br/> 断开 Demo: <br/> Bug: <br/>  
  9.  *   
  10.  * 程序变更日期 �<br/> 变更作� �<br/> 变更说明 �<br/>  
  11.  *   
  12.  * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>  
  13.  */  
  14. public class SenderTask implements Runnable {  
  15.   
  16.     public OutputStream os;  
  17.   
  18.     private String message;  
  19.   
  20.     private boolean stop = false;  
  21.   
  22.     SenderTask(OutputStream os) {  
  23.         this.os = os;  
  24.   
  25.     }  
  26.   
  27.     /**  
  28.      * 发送指令,具体的指令格式,可以按照自己定义的规则。  
  29.      *   
  30.      * @param _msg  
  31.      */  
  32.     public synchronized void send(String _msg) {  
  33.         message = _msg;  
  34.         notify(); // 执行运行  
  35.     }  
  36.   
  37.     /**  
  38.      * 执行,监听客户发送指令,如果指令不为null则工作�, 否则暂停工作直到有客户发送指令为�止才工作,  
  39.      */  
  40.     public synchronized void run() {  
  41.         try {  
  42.             runImpl();  
  43.         } catch (Throwable tw) {  
  44.             tw.printStackTrace();  
  45.   
  46.         }  
  47.     }  
  48.   
  49.     private void runImpl() throws IOException {  
  50.         while (true) {  
  51.             if (stop)  
  52.                 break;  
  53.   
  54.             // If no client to deal, wait until one connects  
  55.             if (message == null) {  
  56.                 try {  
  57.                     wait();  
  58.                 } catch (Exception e) {  
  59.   
  60.                 }  
  61.             }  
  62.             wirte();  
  63.   
  64.         }  
  65.     }  
  66.   
  67.     /**  
  68.      * 具体发�送指令的实现  
  69.      *   
  70.      * @throws IOException  
  71.      *   
  72.      */  
  73.     private void wirte() throws IOException {  
  74.   
  75.         os.write(message.getBytes());  
  76.         os.flush();  
  77.   
  78.         message = null; // 指令为空,等待下一个指�,  
  79.     }  
  80.   
  81.     public synchronized void stop() {  
  82.         stop = true;  
  83.         if (os != null) {  
  84.             try {  
  85.                 os.close();  
  86.                 os = null;  
  87.             } catch (IOException e) {  
  88.   
  89.                 e.printStackTrace();  
  90.             }  
  91.         }  
  92.   
  93.         notify();  
  94.   
  95.     }  
  96.   
  97. }  
<script type="text/javascript">render_code();</script>

2。IncepterTask。负责接收从数据传送过来的数据,并且根据服务器要求,做自己的工作,代码:

代码
  1. package org.wuhua.net;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.DataInputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;   
  9. /**  
  10.  * <b>类名:CheckForm.java</b> </br> 编写日期: 2006-7-18 <br/>  
  11.  * 程序功能描述:检测从服务器上传过来的数据,并判断是否需要弹出窗口显示给用户,并更新相关的信息 <br/>  
  12.  * 此程序的操作网络的核心类,负责网络,跟一些消息��辑处理<br/> Demo: <br/>  
  13.  *   
  14.  * Bug: <br/>  
  15.  *   
  16.  * 程序变更日期 �<br/> 变更作� �<br/> 变更说明 �<br/>  
  17.  *   
  18.  * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>  
  19.  */  
  20. public class IncepterTask implements Runnable  {  
  21.     private DataInputStream in;  
  22.     private boolean stop;  
  23.     private byte[] content = null;  
  24.     IncepterTask(InputStream _in) {  
  25.         in = new DataInputStream(_in);    
  26.     }  
  27.     /**  
  28.      * 程序启动后不停的获取数据,并根据数据的类型做相应的工作  
  29.      */  
  30.     public synchronized void run() {  
  31.         try{       
  32.             runImpl();  
  33.         }catch(Throwable tw){  
  34.             tw.printStackTrace();  
  35.                
  36.         }  
  37.           
  38.     }  
  39.   
  40.     private void runImpl() throws IOException {  
  41.         while (true) {  
  42.             if (stop)  
  43.                 break;  
  44.               
  45.             while (true) {  
  46.                 readMessage();  
  47.   
  48.                 if (content == null)  
  49.                     continue;             
  50.                   
  51.                 executeIncepter( );  
  52.    
  53.             }  
  54.         }  
  55.     }  
  56.       
  57.     //获取数据后的具体工作  
  58.     private void executeIncepter() {  
  59.         // TODO Auto-generated method stub  
  60.           
  61.     }  
  62.     /**  
  63.      * 读取数据  
  64.      *   
  65.      * @throws IOException  
  66.      */  
  67.     private void readMessage() throws IOException {  
  68.   
  69.         content = null;  
  70.         int length = 0;  
  71.         int offer = 0;  
  72.   
  73.         // 如果没数据则阻塞在这�,等啊等啊等啊.  
  74.         length = in.readShort();  
  75.   
  76.         if (length < 0)  
  77.             lengthlength = length + 65536;  
  78.         content = new byte[length];  
  79.         while (true) {  
  80.             int i = in.read(content, offer, length - offer);  
  81.             offer += i;  
  82.             if (offer == length)  
  83.                 break;  
  84.         }  
  85.     }  
  86.    
  87.     public synchronized void stop() {  
  88.         stop = true;  
  89.         if(in != null){  
  90.             try {  
  91.                 in.close();  
  92.                 in = null;  
  93.             } catch (IOException e) {  
  94.                    
  95.                 e.printStackTrace();  
  96.             }  
  97.         }  
  98.               
  99.         notify();  
  100.            
  101.     }  
  102. }  
<script type="text/javascript">render_code();</script>

1。Client类,负责打开连接以及管理,接收数据,与发送数据线程。

代码
  1. package org.wuhua.net;  
  2. import java.io.IOException;  
  3.   
  4. import javax.microedition.io.Connector;  
  5. import javax.microedition.io.SocketConnection;  
  6. public class Client {  
  7.     static SocketConnection sc;   
  8.     static IncepterTask it;  
  9.     static SenderTask st;  
  10.       
  11.     static int restart;  
  12.     public static boolean IS_START = false;  
  13.    
  14.     private Client() {  
  15.         try {  
  16.             sc = (SocketConnection) Connector.open(  
  17.                     "socket://127。0。0。1:1232"3false);   
  18.               
  19.               
  20.             it = new IncepterTask(sc.openInputStream());  
  21.             new Thread(it).start(); //启动拦截数据线程  
  22.               
  23.             st = new SenderTask(sc.openOutputStream());  
  24.             new Thread(st).start();//启动数据发送线程  
  25.             IS_START = true;  
  26.               
  27.         } catch (Exception e) {  
  28.                
  29.             IS_START = false;  
  30.                
  31.         }  
  32.     }  
  33.       
  34.     public  static void start(){  
  35.         new Client();         
  36.     }  
  37.       
  38.     public static void stop() {  
  39.         try {  
  40.             if (sc != null)  
  41.                 sc.close();  
  42.             if(it != null)  
  43.                 it.stop();  
  44.             if(st != null)  
  45.                 st.stop();  
  46.         } catch (IOException e) {  
  47.                
  48.         }  
  49.         sc = null;    
  50.         st = null;  
  51.         it = null;  
  52.     }  
  53.   
  54.     /** 
  55.      * 发送指令 
  56.      */  
  57.   
  58.     public static void sendDictate(String type) {  
  59.   
  60.         try {  
  61.             st.send(type);  
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.         }  
  65.   
  66.     }  
  67.   
  68. }  
<script type="text/javascript">render_code();</script>

以上是连接网络的基本框架,如果需要加些功能可以很好的利用这些代码

[code]

最后更新:2006-11-15 14:14 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值