J2me多线程http数据从另一个线程泄漏



我需要在j2me设备上同时打开两个http连接。所以我在两个线程中打开了两个HttpConnection。但有时一个连接会接收到另一个连接的数据。我该如何解决这个问题
我在诺基亚N70上测试了该应用程序。

代码很大。我试着写一些简单的伪代码。

http.java:

public class Http
{
    public Http()
    {
    }
    public void start(String url) {
       new Thread() {
            public void run() {
                getHttp(url);
           }
       }.start();
    }
    private void getHttp(String url) {
        InputStream is = null;
        HttpConnection http=null ;
        try {
            http= (HttpConnection) Connector.open(url);
            httpCode = http.getResponseCode();
            is = http.openInputStream();
            int ic;
            byte[] tmp = new byte[1024];
            while (!cancel && (ic = is.read(tmp, 0, 1024)) != -1) {
                line.append((char) ic);
                bao.write(tmp, 0, ic);
                 }
              //httpnotify.receive(bao.toByteArray) ;
            } catch (Exception e) {
            }
        }
}

客户端1:

Http http=new Http() ;
http.setNotify(self) ;
http.start("http://....") ;

客户端2:

Http http2=new Http() ;
http2.setNotify(self) ;
http2.start("http://....") ;

我认为您需要在getHttp()方法上使用synchronized关键字

最新更新