2012-12-07 111 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相依爲命一個新線程的創建時間被實例化。

相關問題