HashSet双层多线程遍历 本帖最后由 u011532105 于 2014-07-10 14:45:07 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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()); } }} @shixitong 不是这个意思.... 第一个 是开个线程池只是获取第二个Set里面的数据.结果应该是... pool-1-thread-1pool-1-thread-2pool-1-thread-3pool-1-thread-4pool-1-thread-5pool-1-thread-6第二个Set执行结束第一个Set取出一条数据生成第二个Setpool-1-thread-1pool-1-thread-2pool-1-thread-3pool-1-thread-4pool-1-thread-5pool-1-thread-6第二个Set执行结束第一个Set取出一条数据生成第二个Set大概我想要的是这样的... 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 一下就可以了。 这么个情况...第一层HashSet里面就是有几百个关键字...取出每个关键字..然后去google查询前十条或者是前二十条结果名称或者URL存入到HashSet中.然后再把结果遍历出来写入到数据库里面或者是执行其他操作..写入数据库用多线程实现..同时要保证执行结束才能进行第一层的再次next() ...大概是这么个意思...只能用HashSet.... 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); }} 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); }} 求一个正则表达式 com.heavenlake.wordapi.Document属于那个jar包啊 懂hibernate hql查询的进来,count查询出来的数据不正确,只有58分了,只求高手来秒杀 tomcat 怎样管理界面 在WebRoot下没有indexjsp页面 在manage里面直接点击项目就能出来 webRoot下面的文件夹 spring的问题 小弟请教一个另一个排序问题 请问关于Web Service的书籍 入门求教 关于jive(J道版),安装正常,编译出错! 求助EJB错误的解决方法,多谢! 使用什么定时器能够定时对前台页面发送消息? FreeMarker配置使用问题
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());
}
}
}
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大概我想要的是这样的...
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 一下就可以了。
第一层HashSet里面就是有几百个关键字...
取出每个关键字..然后去google查询前十条或者是前二十条结果名称或者URL存入到HashSet中.然后再把结果遍历出来写入到数据库里面或者是执行其他操作..写入数据库用多线程实现..同时要保证执行结束才能进行第一层的再次next() ...大概是这么个意思...只能用HashSet....
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);
}
}
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);
}
}