在ExecutorService.submit

时间:2019-05-15 20:10:03

标签: java runnable executorservice

我有一个程序,其中使用ExecutorService初始化了线程池。在主应用程序线程中,我创建了一个带有一些数据的可运行实例,并使用executorservice.submit()将其传递给线程池,池中的一个线程获取了数据并将其发布到Kafka。相同的代码在其他机器上也可以正常工作(机器1:AIX-在WebSphere Application Server中,机器2:Windows 10,独立的Java JVM)。 当我在AIX上的独立JVM中运行相同的代码时,它不起作用(Runnable的日志都没有,数据也不会发布到Kafka。

课程:


import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;



public class CustomExecutorServiceWrapper {



private static ExecutorService[] executorArr =  {Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1), Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1),Executors.newFixedThreadPool(1) } ;

public static void submitToSpecificThread(Runnable r) {
        int i = r.hashCode()%10;
    ExecutorService executorService = executorArr[Math.abs(i)];
    ThreadPoolExecutor tPoolExecutor = (ThreadPoolExecutor)executorService;
     tPoolExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {

        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("Task rejected" );

        }
    });
    System.out.println("Thread Name - "+Thread.currentThread().getId() +" " +  Thread.currentThread().getName() + " queue size " +  tPoolExecutor.getQueue().size() );
    Future fObj = executorService.submit(r);
    System.out.println("FutureObject Cancelled" + fObj.isCancelled());
    System.out.println("FutureObject isDone" + fObj.isDone());

    System.out.println("Thread Name - "+Thread.currentThread().getId() +" " +  Thread.currentThread().getName() + " after submit queue size " +
            tPoolExecutor.getQueue().size() );

}
}

Class-2:


import com.common.kafka.StaticKafkaBaseSenderImpl;

public class CustomRunnable implements Runnable{

    @Override
    public int hashCode() {
        return strWorkLoadIdentifier.hashCode();
    }

    String strWorkLoadIdentifier ;

    String strLoadMessage ;

    private void log(String str) {
        System.out.println("CustomRunnable - "+ Thread.currentThread().getId()+" - "+Thread.currentThread().getName() + " - " + str);
        }   
    public CustomRunnable(String str) {
        this.strWorkLoadIdentifier=str;
    }
    public CustomRunnable(String str, String strMessage) {
        this.strWorkLoadIdentifier=str;
        this.strLoadMessage=strMessage;
    }

    @Override
    public void run() {
        log("Starting work Load" + strWorkLoadIdentifier);      
        try {
            long t1=System.currentTimeMillis();
            StaticKafkaBaseSenderImpl.doBackgroundStream(strLoadMessage);
            log("Time taken to post message - " + (System.currentTimeMillis() - t1));
        } catch (Exception e) {
            log("Error Sending Message to Kafka");
            e.printStackTrace();
        }
    }
}

第3类:StaticKafkaBaseSenderImpl->向Kafka主题发送消息。

申请流程:

  1. 主应用程序线程收集一些数据。
  2. 主应用程序线程执行以下语句:

    CustomExecutorServiceWrapper.submitToSpecificThread(new CustomRunnable(“ Random MessageId”,“收集的数据”);

  3. 主应用程序线程继续执行。

在方法commitToSpecificThread的后台,识别出特定的ExecutorService实例并将任务提交给它。

应用运行记录:

2019-05-15 14:30:58,595:INFO   :Thread-31_ApplicationProcess: Thread Name - 69 Thread-31_ApplicationProcess queue size 0 [system]: CustomExecutorServiceWrapper
2019-05-15 14:30:58,595:INFO   :Thread-31_ApplicationProcess: FutureObject Cancelledfalse                                  [system]: CustomExecutorServiceWrapper
2019-05-15 14:30:58,595:INFO   :Thread-31_ApplicationProcess: FutureObject isDonefalse                                     [system]: CustomExecutorServiceWrapper
2019-05-15 14:30:58,595:INFO   :Thread-31_ApplicationProcess: Thread Name - 69 Thread-31_ApplicationProcess after submit queue size 0 [system]: CustomExecutorServiceWrapper

我在Future Object上添加了检查,以查看它是否被拒绝或取消,但是我看不到RejectedExecutionHandlerisCancelled()isDone()的日志都返回false。

0 个答案:

没有答案