本帖最后由 u011532105 于 2014-07-10 14:45:07 编辑

解决方案 »

  1.   

    package shi;import java.util.Collections;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;/**
     * 代码如上. 第一层HashSet获取到内容之后,根据 next()内容再次封装一个HashSet 第二层的HashSet使用线程池读取...
     * 但是好像这个线程池并没有关闭。 求能够保证Set只读取一次。并且第一层每次循环之前都要保证第二层循环已经执行结束!
     * 
     * */
    public class Test { public static void main(String[] args) throws InterruptedException,
    ExecutionException {
    HashSet<String> hsSet = new HashSet<String>();
    for (int i = 0; i < 1000; i++) {
    hsSet.add("" + i);
    }
    Iterator<String> itter = hsSet.iterator();
    while (itter.hasNext()) {
    String a = itter.next();
    System.out.println("当前的值为:" + a);
    HashSet<String> endHashSet = addHashSet(a);
    Set<String> syHashSet = Collections.synchronizedSet(endHashSet);
    synchronized (syHashSet) {
    Iterator<String> endItter = syHashSet.iterator();
    ExecutorService pool = Executors.newFixedThreadPool(100);
    GetEnd t = new GetEnd(endItter);
    Future future = pool.submit(t);
    // 当循环syHashSet循环结束
    if (future.get() == null && !pool.isTerminated()) {
    pool.shutdownNow();
    }
    }
    }
    } 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;
    }
    }class GetEnd implements Runnable { public Iterator<String> endItter; public GetEnd() { } public GetEnd(Iterator<String> endItter) {
    this.endItter = endItter;
    } public void run() {
    System.out.println(Thread.currentThread().getName());
    while (endItter.hasNext()) {
    System.out.println(endItter.next());
    }
    }
    }
      

  2.   

    @shixitong   不是这个意思.... 第一个  是开个线程池只是获取第二个Set里面的数据.结果应该是... 
      

  3.   

    pool-1-thread-1
    pool-1-thread-2
    pool-1-thread-3
    pool-1-thread-4
    pool-1-thread-5
    pool-1-thread-6
    第二个Set执行结束第一个Set取出一条数据
    生成第二个Set
    pool-1-thread-1
    pool-1-thread-2
    pool-1-thread-3
    pool-1-thread-4
    pool-1-thread-5
    pool-1-thread-6
    第二个Set执行结束第一个Set取出一条数据
    生成第二个Set大概我想要的是这样的...
      

  4.   


    ExecutorService pool = Executors.newFixedThreadPool(100); 
    GetEnd t = new GetEnd(); 
    pool.execute(t); 
    pool.shutdown();这里需要100个吗?1 你就执行一个task  pool.execute(t);线程池需要这么多线程。
    2 每次循环一次开辟 线程池 浪费。
    3 其实等价代码public static void main(String args) throws InterruptedException {
    HashSet<String> hsSet = new HashSet<String>();
    for (int i = 0; i < 1000; i++) {
    hsSet.add("" + i);
    }
    Iterator<String> itter = hsSet.iterator();
    while (itter.hasNext()) {
    String a = itter.next();
    HashSet<String> endHashSet = addHashSet(a);
    Set<String> syHashSet = Collections.synchronizedSet(endHashSet);
    final Iterator<String> endItter = syHashSet.iterator(); Thread thread = new Thread(new Runnable() { @Override
    public void run() {
    // TODO Auto-generated method stub
    System.out.println(Thread.currentThread().getName());
    while (endItter.hasNext()) {
    System.out.println(endItter.next());
    }
    }
    });
    thread.start();
    thread.join();
    }
    }
    并且第一层每次循环之前都要保证第二层循环已经执行结束! 只要join 一下就可以了。
     
      

  5.   

    这么个情况...
    第一层HashSet里面就是有几百个关键字...
    取出每个关键字..然后去google查询前十条或者是前二十条结果名称或者URL存入到HashSet中.然后再把结果遍历出来写入到数据库里面或者是执行其他操作..写入数据库用多线程实现..同时要保证执行结束才能进行第一层的再次next() ...大概是这么个意思...只能用HashSet....
      

  6.   

    import java.util.Collections;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;public class Test { public static void main(String[] args) throws InterruptedException,
    ExecutionException {
    HashSet<String> hsSet = new HashSet<String>();
    for (int i = 0; i < 1000; i++) {
    hsSet.add("" + i);
    }
    Iterator<String> itter = hsSet.iterator();
    while (itter.hasNext()) {
    String a = itter.next();
    System.out.println("当前的值为:" + a);
    HashSet<String> endHashSet = addHashSet(a);
    Set<String> syHashSet = Collections.synchronizedSet(endHashSet);
    synchronized (syHashSet) {
    Iterator<String> endItter = syHashSet.iterator();
    ExecutorService pool = Executors.newFixedThreadPool(10);
    while (endItter.hasNext()) {
    GetEnd t = new GetEnd(endItter.next());
    pool.submit(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;
    }
    }
    class GetEnd implements Runnable { public String value;
    public GetEnd() {
    }
    public GetEnd(String value) {
    this.value = value;
    } public void run() {
    System.out.println(Thread.currentThread().getName());
    System.out.println(value);
    }
    }
      

  7.   

    import java.util.Collections;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import java.util.concurrent.ExecutionException;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;public class Test { public static void main(String[] args) throws InterruptedException,
    ExecutionException {
    HashSet<String> hsSet = new HashSet<String>();
    for (int i = 0; i < 1000; i++) {
    hsSet.add("" + i);
    }
    Iterator<String> itter = hsSet.iterator();
    while (itter.hasNext()) {
    String a = itter.next();
    System.out.println("当前的值为:" + a);
    HashSet<String> endHashSet = addHashSet(a);
    Set<String> syHashSet = Collections.synchronizedSet(endHashSet);
    synchronized (syHashSet) {
    Iterator<String> endItter = syHashSet.iterator();
    ExecutorService pool = Executors.newFixedThreadPool(10);
    while (endItter.hasNext()) {
    GetEnd t = new GetEnd(endItter.next());
    pool.submit(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;
    }
    }
    class GetEnd implements Runnable { public String value;
    public GetEnd() {
    }
    public GetEnd(String value) {
    this.value = value;
    } public void run() {
    System.out.println(Thread.currentThread().getName());
    System.out.println(value);
    }
    }