日期:2014-05-19 浏览次数:20644 次
class Share implements Runnable { List<String> list; int index = 0; int count = 0; public Share(List<String> list) { this.list = list; count = list.size(); } public void run() { while (true) { try { String url = null; synchronized(list) { url = list.get(index); index = (index+1)%count; } //do your process here Thread.sleep(1000*60*5); } catch (Exception e) { e.printStackTrace(); } } } } public class Test { public static void main(String[] args) { List<String> list = new ArrayList<String>(); for (int i=0; i<10; i++) { list.add("xxx.xxx.xxx:port"); } Runnable r = new Share(list); for (int i=0; i<20; i++) { Thread t = new Thread(r); t.start(); } } }
------解决方案--------------------