簡體   English   中英

ExecutorService-如何在Runnable / Callable中設置值並重用

[英]ExecutorService - How to set values in a Runnable/Callable and reuse

我想使用ExecutorService運行一系列相同的Runnable / Callable任務。 我到處搜索了教程或示例,但沒有涉及實際設置現有Runnable / Callable對象的值,然后使用Submit()將對象發送回ExecutorService的內容。

基本上,這就是我想要做的:

  • 獲取服務器列表。
  • 遍歷服務器列表,調用InetAddress.getByName(host)以獲取每個主機上的數據。
  • 將該數據收集到Server Bean中以存儲在數據庫中。

因此,現在,如果有10,000(+)個服務器,那將永遠花光。 因此,我的想法是使用ExecutorService管理線程池。 我似乎無法弄清楚的是如何檢測一個線程何時結束,以便我可以獲取數據。 然后,我需要獲取列表中的下一個服務器,將其放入Task中,然后將commit()返回到ExecutorService。

也就是說,到目前為止,我所讀的內容似乎指向以下內容:ExecutorService,submit(),Callable,Future。

因此,作為偽過程:

  • 獲取服務器列表。
  • 使用numThreads個線程數設置ExecutorService
  • 迭代numThreads並創建numThreads WorkerTask()對象。
  • Submit()WorkerTask()到ExecutorService進行處理。
  • 檢測到WorkerTask()完成時,獲取Callable(Future)結果。
  • 獲取下一個服務器。
  • 將服務器值設置到WorkerTask()<-怎么做? 這難以捉摸...
  • 將擁有新值的WorkerTask()提交回ExecutorService。
  • 再次迭代。
  • ExecutorService.shutdown()...

因此,這方面的教程或示例將非常有用...尤其是將新值放入WorkerTask()的示例。 另外,我希望對這個提議的解決方案有什么想法? 這是好事還是好事,或者如果我有其他選擇,我很開放。

2014年2月9日-編輯並添加

嗨,因此,以下是第一個切入點。 但是要回答一些提出的問題:
-我已經解決了將新數據放入Worker並重新提交給ExecutorService的問題...請參見代碼段。
-我也解決了“拿東西”的問題...我只是將Future()結果強制轉換為Worker類...請參見代碼段。
-最后,雖然我可以將每個服務器分配給Worker()和Future(),但我擔心當前的10,000個服務器會增長,並且內存將成為問題。

就是說,這是第一次嘗試,並且效果很好。 運行速度更快,並且僅使用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;
}

這是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;
    }

}

因此,總結一下:
-該過程確實有效,並且工作迅速。
-我需要修復代碼,因為有getNumberThreads()-1當更大的while()循環最終結束時,未處理的期貨...

所以,我現在正在努力的是如何檢測線程何時完成...我已經看到了多個示例,一個示例測試Future()!= null,另一個示例測試Future()== null。 那么哪一個是對的?

我認為在這種情況下,最好的辦法是為每個服務器創建一個任務,因為它們將由拉中的線程執行,然后使用tge future對象檢索任務返回的服務器信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM