我寫了一段存取靜態變數的code:
public class SetConnectionsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
......
public static List<Connection> list = new ArrayList<>();
}
然後我透過多執行緒,取得connection物件,並寫入上面那個arrayList:
SetConnectionForMultiThread[] smtArray = new SetConnectionForMultiThread[200];
Thread[] thrArray = new Thread[200];
int j = 0;
for(int i = 0;i<200;i++){
smtArray[j] =
new SetConnectionsForMultiThread();
thrArray[j] = new Thread(smtArray[j]);
thrArray[j].start();
j++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
然後在SetConnectionForMultiThread這個class裡:
public class SetConnectionForMultiThread implements Runnable{
public void run(){
......
SetConnectionsServlet.list.add(connection);
}
}