我写了一段存取静态变量的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);
}
}