[英]ExecutorService - How to set values in a Runnable/Callable and reuse
I want to use the ExecutorService to run a series of the same Runnable/Callable tasks. 我想使用ExecutorService运行一系列相同的Runnable / Callable任务。 I've searched around for a tutorial or an example, but nothing that involves actually setting the value of an existing Runnable/Callable object and then using submit() to send that object back into the ExecutorService.
我到处搜索了教程或示例,但没有涉及实际设置现有Runnable / Callable对象的值,然后使用Submit()将对象发送回ExecutorService的内容。
Basically, here's what I want to do: 基本上,这就是我想要做的:
InetAddress.getByName(host)
to grab data on each host. InetAddress.getByName(host)
以获取每个主机上的数据。 So, right now, with 10,000(+) servers, it takes forever. 因此,现在,如果有10,000(+)个服务器,那将永远花光。 So, my thought was to use the ExecutorService to manage a pool of threads.
因此,我的想法是使用ExecutorService管理线程池。 What I can't seem to figure out is how to detect when one thread is finished so I can grab the data.
我似乎无法弄清楚的是如何检测一个线程何时结束,以便我可以获取数据。 Then I need to get the next server in the list, place it into the Task and then submit() back to the ExecutorService.
然后,我需要获取列表中的下一个服务器,将其放入Task中,然后将commit()返回到ExecutorService。
That said, what I've read so far seems to point to the following, ExecutorService, submit(), Callable, Future. 也就是说,到目前为止,我所读的内容似乎指向以下内容:ExecutorService,submit(),Callable,Future。
So, as a psuedo-process: 因此,作为伪过程:
So, a tutorial or example of this would be great...especially an example of placing a new value into a WorkerTask(). 因此,这方面的教程或示例将非常有用...尤其是将新值放入WorkerTask()的示例。 Also, I'd appreciate any thoughts on this proposed solution?
另外,我希望对这个提议的解决方案有什么想法? Is this bad, good, or if there is another way, I'm open.
这是好事还是好事,或者如果我有其他选择,我很开放。
02/09/2014 - Edit and Add 2014年2月9日-编辑并添加
Hi, So the following is a first cut at this. 嗨,因此,以下是第一个切入点。 But to answer some question being posed:
但是要回答一些提出的问题:
- I've solved the issue of placing new data in a Worker and resubmitting to the ExecutorService...see the code snippet. -我已经解决了将新数据放入Worker并重新提交给ExecutorService的问题...请参见代码段。
- I've also solved the issue of "get the stuff"...I simply cast the Future() results to the Worker class...see the code snippet. -我也解决了“拿东西”的问题...我只是将Future()结果强制转换为Worker类...请参见代码段。
- Finally, while I could just allocate every server to a Worker() and a Future(), I'm concerned that the current 10,000 will grow and memory will become an issue. -最后,虽然我可以将每个服务器分配给Worker()和Future(),但我担心当前的10,000个服务器会增长,并且内存将成为问题。
That said, here's a first attempt, and this works pretty well. 就是说,这是第一次尝试,并且效果很好。 Runs much faster and only uses the getNumbnerThreads() Worker and Future objects:
运行速度更快,并且仅使用getNumbnerThreads()Worker和Future对象:
public List<ServerBean> lookupHostIps ( List<ServerBean> theServerList ) {
//ServerBean serverDto = null;
ServerBean ipDto = null;
List<ServerBean> theResults = new ArrayList<ServerBean>();
List<HostLookupWorker> theWorkers = new ArrayList<HostLookupWorker>( getNumberThreads() );
List<Future<HostLookupWorker>> theFutures = new ArrayList<Future<HostLookupWorker>>( getNumberThreads() );
ExecutorService executor = Executors.newFixedThreadPool ( getNumberThreads() );
// WORKERS : Create the workers...prime them with a server
// bean...
//
for (int j = 0; j < getNumberThreads(); j++) {
//for (int j = 0; j < theServerList.size(); j++) {
theWorkers.add ( new HostLookupWorker( theServerList.get(j) ) );
Future<HostLookupWorker> theFuture = executor.submit ( theWorkers.get ( j ) );
theFutures.add ( j, theFuture );
}
int lloopItems = getNumberThreads(); /* loops thru all servers */
//int lloopThreads = 0; /* loops thru threads */
int lidxThread = 0; /* what thread is ready */
//int lidxFuture = 0; /* what future is ready */
boolean lblnNext = false; /* is a thread done/ready */
int lidxWorkers = 0; /* tracks the futures */
while ( lloopItems < theServerList.size() ) {
// READY : Is one of the threads ready for more work?
if ( lblnNext ) {
// VALUE : Grab the thread by index and set the next
// server value.
theWorkers.get ( lidxThread ).setBean ( theServerList.get(lloopItems) );
getLog().debug ( "Thread [" + lidxThread + "] Assigned Host ["+theServerList.get(lloopItems).getServerName ()+"] " );
// FUTURE : Package a new Future<HostLookupWorker>
// and submit it to the thread pool.
Future<HostLookupWorker> theFuture = executor.submit ( theWorkers.get ( lidxThread ) );
theFutures.add ( lidxThread, theFuture );
lblnNext = false; /* reset to allow for another thread */
lloopItems++; /* increment the main loop counter */
}
while ( !(lblnNext) ) {
try {
if ( theFutures.get(lidxWorkers).get() != null ) {
// GET THE STUFF : Grab the results from the Future...
HostLookupWorker ltheItem = theFutures.get(lidxWorkers).get();
if ( ltheItem.getValue () != null ) {
if (!ltheItem.getValue ().contains("Cannot find host")){
ipDto = new ServerBean ();
ipDto.setServerId ( ltheItem.getBean ().getServerId() );
ipDto.setServerName ( ltheItem.getBean ().getServerName() );
ipDto.setIpAddress ( ltheItem.getValue () );
theResults.add(ipDto);
}
lidxThread = lidxWorkers; /* this thread is ready for more work */
lblnNext = true; /* flag the upper condition to assign new work */
getLog().debug ( "Thread [" + lidxThread + "] Host ["+ltheItem.getHost ()+"] has IP ["+ltheItem.getValue()+"]" );
}
}
else {
getLog().debug ( "Thread [" + lidxThread + "] NULL" );
}
lidxWorkers++; /* next worker/future */
if ( lidxWorkers >= getNumberThreads() ) {
lidxWorkers = 0;
}
}
catch(ExecutionException e){
getLog().error ( e );
}
catch(InterruptedException e){
getLog().error ( e );
}
}
}
executor.shutdown ();
return theResults;
}
Here's the Worker/Thread class : 这是Worker / Thread类:
import java.net.*;
import java.util.concurrent.Callable;
import com.lmig.cdbatch.dto.ServerBean;
public class HostLookupWorker implements Callable {
private InetAddress node = null;
private String value = null;
private boolean busy = false;
private ServerBean bean = null;
public HostLookupWorker () {
this.busy = false;
}
// public HostLookupWorker ( String theHost ) {
// this.busy = false;
// this.host = theHost;
// }
public HostLookupWorker ( ServerBean theItem ) {
this.busy = false;
this.bean = theItem;
//this.host = theItem.getServerName ().trim ();
}
public String lookup ( String host ) {
if ( host != null ) {
// get the bytes of the IP address
try {
this.node = InetAddress.getByName ( host );
}
catch ( UnknownHostException ex ) {
this.value = "Not Found [" + getHost() + "]";
return "Not Found [" + host + "]";
}
if ( isHostname(host) ) {
getBean().setIpAddress ( node.getHostAddress() );
return node.getHostAddress();
}
else { // this is an IP address
//return node.getHostName();
return host;
}
}
return host;
} // end lookup
public boolean isHostname(String host) {
// Is this an IPv6 address?
if (host.indexOf(':') != -1)
return false;
char[] ca = host.toCharArray();
// if we see a character that is neither a digit nor a period
// then host is probably a hostname
for (int i = 0; i < ca.length; i++) {
if (!Character.isDigit(ca[i])) {
if (ca[i] != '.')
return true;
}
}
// Everything was either a digit or a period
// so host looks like an IPv4 address in dotted quad format
return false;
} // end isHostName
// public void run() {
// value = lookup ( getHost() );
//
// }
public Object call() throws Exception {
Thread.sleep ( 10000 );
this.busy = true;
this.value = lookup ( getHost() );
this.busy = false;
return this;
}
public String getHost() {
return getBean().getServerName ().trim ();
}
public void setHost(String host) {
getBean().setServerName ( host.trim () );
}
public InetAddress getNode() {
return node;
}
public void setNode(InetAddress node) {
this.node = node;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public boolean isBusy() {
return busy;
}
public void setBusy(boolean busy) {
this.busy = busy;
}
public ServerBean getBean() {
return bean;
}
public void setBean(ServerBean bean) {
this.bean = bean;
}
}
So, to summarize: 因此,总结一下:
- The process does work, and works fast. -该过程确实有效,并且工作迅速。
- I need to fix the code a little, as there are getNumberThreads() - 1 Futures left unprocessed when the larger while () loop finally finishes... -我需要修复代码,因为有getNumberThreads()-1当更大的while()循环最终结束时,未处理的期货...
So, what I'm struggling with now is how to detect when a thread is finished...I've seen multiple examples, one testing for Future() != null, others testing for Future() == null. 所以,我现在正在努力的是如何检测线程何时完成...我已经看到了多个示例,一个示例测试Future()!= null,另一个示例测试Future()== null。 So which one is right?
那么哪一个是对的?
我认为在这种情况下,最好的办法是为每个服务器创建一个任务,因为它们将由拉中的线程执行,然后使用tge future对象检索任务返回的服务器信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.