public static void main(String[] args) {
HashSet hsSet = new HashSet();
for (int i = 0; i < 1000; i++) {
hsSet.add("" + i);
}
Iterator itter = hsSet.iterator();
while (itter.hasNext()) {
String a = itter.next();
HashSet endHashSet = addHashSet(a);
Set syHashSet = Collections.synchronizedSet(endHashSet);
final Iterator endItter = syHashSet.iterator();
class GetEnd implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
while (endItter.hasNext()) {
System.out.println(endItter.next());
}
}
}
ExecutorService pool = Executors.newFixedThreadPool(100);
GetEnd t = new GetEnd();
pool.execute(t);
pool.shutdown();
}
}
public static HashSet addHashSet(String a){
HashSet hashSet = new HashSet();
for (int i = 0; i < 10; i++) {
hashSet.add(i + a);
}
return hashSet;
}
代码入上. 第一层HashSet获取到内容之后,根据 next()内容再次封装一个HashSet 第二层的HashSet使用线程池读取...
但是好像这个线程池并没有关闭。求指点...这个应该怎么写..谢谢!
[code="java"]
线程池你每个循环, 都创建一个, 然后关闭只是当前,
下次又重新创建了
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class App {
public static void main(String[] args) {
HashSet<String> hsSet = new HashSet<String>();
for (int i = 0; i < 10; i++) {
hsSet.add("" + i);
}
Iterator<String> itter = hsSet.iterator();
ExecutorService pool = Executors.newFixedThreadPool(1);
while (itter.hasNext()) {
String a = itter.next();
HashSet<String> endHashSet = addHashSet(a);
Set<String> syHashSet = Collections.synchronizedSet(endHashSet);
final Iterator<String> endItter = syHashSet.iterator();
class GetEnd implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
while (endItter.hasNext()) {
System.out.println(endItter.next());
}
}
}
if(!pool.isShutdown()) {
GetEnd t = new GetEnd();
pool.execute(t);
pool.shutdown();
}
}
}
public static HashSet<String> addHashSet(String a) {
HashSet<String> hashSet = new HashSet<String>();
for (int i = 0; i < 10; i++) {
hashSet.add(i + a);
}
return hashSet;
}
}
[/code]