Java异常:ThreadPoolExecutor $ Worker与com.myco.TaskListEntry不兼容

时间:2013-02-27 22:12:08

标签: java multithreading naming

在Java中,我遇到了一个强制转换异常:

java.lang.ClassCastException: java.util.concurrent.ThreadPoolExecutor$Worker
incompatible with com.myco.TaskListEntry
at com.myco.JobThreadFactory.newThread(JobThreadFactory.java:17)

以下是代码:

public class JobThreadFactory implements ThreadFactory {
    @Override
    public Thread newThread(Runnable r) {
        TaskListEntry entry = (TaskListEntry)r;   //<== Cast exception!
        return new Thread(r, "Thread for " + entry.toString());
    }
}

@DisallowConcurrentExecution
public class WorksheetSessionJob implements Job {
    private List<TaskListEntry> taskListEntries;

    @Override
    public void execute(JobExecutionContext jobContext) throws JobExecutionException {
        ThreadFactory threadFactory = new JobThreadFactory();
        ExecutorService executor = Executors.newCachedThreadPool(threadFactory);
        for( TaskListEntry nextEntry : getWorkListEntries() ) {
            executor.execute(nextEntry);
        }
    }

    private List<TaskListEntry> getWorkListEntries() {
        List<TaskListEntry> entries = new ArrayList<TaskListEntry>();

        // TODO Get rows from DB - for now, make something up to test with
        //-- beginning of fake data creation ----------
        TaskListEntry entry = new TaskListEntry("0022", "04590150560", "DI_MODULAR", 1);
        entries.add(entry);
        entry = new TaskListEntry("0007", "04590150560", "DI_MODULAR", 2);
        entries.add(entry);
        //-- end of fake data creation ----------------

        return entries;
    }

}

public class TaskListEntry implements Runnable {
    private String  worksheetNumber;
    private String  specimenNumber;
    private String  instrumentName;
    private int     id;

    public TaskListEntry(String worksheetNumber, 
            String specimenNumber, String instrumentName, int id) {
        super();
        this.worksheetNumber = worksheetNumber;
        this.specimenNumber = specimenNumber;
        this.instrumentName = instrumentName;
        this.id = id;
    }

    @Override
    synchronized public void run() {
        // executor in my Job should have created a thread for me to run in
        //-- TESTING ONLY -------------
        try {
            Thread.sleep(10000);    //force execution to another thread?
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //-- END OF TESTING -----------
    }

}

为什么我的TaskListEntry没有被发送?我需要使用该类中的值来命名相应的线程。我究竟做错了什么?!

1 个答案:

答案 0 :(得分:4)

您遇到了强制转换问题,因为调用线程工厂时,RunnableThreadPoolExecutor$Worker类没有关联。 Worker Runnable是一个内部执行程序类,它从保存BlockingQueue个实例的内部TaskListEntry中取出任务。您的任务无法通过设计与特定线程相关联。如果你将100k任务提交到队列中,你就不希望它产生100k个线程 - 这就是使用线程池的全部意义。

如果您试图以某种方式跟踪哪些线程正在处理哪些任务,我建议在Thread.currentThread().getId()方法中记录thread-id(TaskListEntry.run())。但是如果你试图在线程名称中以某种方式查看每个任务,那么你就不了解线程池的工作原理。通常,我使用ThreadFactory在池线程上设置名称以标识池名称。像"TaskListEntry pool-1"之类的东西在这里是合适的线程名称。

如果您详细解释有关名称的内容,我们可能会提供更多帮助。