2012-12-07 61 views
0

多线程同步的请求我的问题是,既然我创建多个线程发送唯一请求到相同的OutputStream,哪些变量我只需要创建1个参考(或同步)。我的教授没有包括URLConnections。不知道该怎么通过的URLConnection

当我只运行1个线程,它工作正常,但多个线程只能产生输出为先。

非常感谢帮助。

============================================== ===========================

URLTest的构造函数中:

/*URL*/ link = new URL("url removed"); 
/*URLConnection*/ connect = link.openConnection(); 
connect.setDoOutput(true); 

我这段代码执行内部URLTest:

for (int i = 0; i < 2; i++) { 
    Thread t = new Thread(new ThreadTest()); 
    /*ArrayList<Thread>*/ a.add(t); 
    t.start(); 
} 

run()的内部ThreadTest实现Runnable:

PrintWriter osw = new PrintWriter(connect.getOutputStream()); 
osw.write("query removed"); 
osw.close(); 
BufferedReader in = new BufferedReader(new InputStreamReader(
     connect.getInputStream())); 

String s; 
while ((s = in.readLine()) != null) 
    System.out.println(s); 

回答

0

的问题是,一个URLConnection相依为命一个新线程的创建时间被实例化。

相关问题