Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8235289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:45:24+00:00 2026-06-07T18:45:24+00:00

Please assume the following: public static void main(String[] args) { ThreadPoolExecutor pool = new

  • 0

Please assume the following:

public static void main(String[] args) {
    ThreadPoolExecutor pool = new ThreadPoolExecutor(0, 5, 1L, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>());
    DashboardHtmlExport d = new DashboardHtmlExport();
    for (int i = 0; i < 40; i++) {
        System.out.println("Submitting: " + i);
        try {
            Thread.sleep(cooldown);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        pool.submit(d.new A(i));
    }
}

private class A implements Runnable, Comparable<A> {
    private int order;

    public A(int order) {
        this.order = order;
    }

    public void run() {
        try {
            Thread.sleep(busyTime);
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
        System.out.println(new Date());
    }

    public int compareTo(A o) {
        return Integer.valueOf(o.order).compareTo(Integer.valueOf(order));
    }
}

This produced the following error:

Submitting: 0
Submitting: 1
Exception in thread "main" java.lang.ClassCastException: java.util.concurrent.FutureTask cannot be cast to java.lang.Comparable
    at java.util.PriorityQueue.siftUpComparable(PriorityQueue.java:578)
    at java.util.PriorityQueue.siftUp(PriorityQueue.java:574)
    at java.util.PriorityQueue.offer(PriorityQueue.java:274)
    at java.util.concurrent.PriorityBlockingQueue.offer(PriorityBlockingQueue.java:164)
    at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:653)
    at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:78)
    at com.skycomm.cth.pages.test.DashboardHtmlExport.main(DashboardHtmlExport.java:133)
Tue Jul 10 23:48:45 GMT+02:00 2012

Why is this happening ?!

This only works if the busyTime is less that the cooldown time. Meaning that the queue can’t handle more than 2 submitted tasks !!

Basically what I’m trying to do is to have a pool with UNBOUND size and sortable whether by comparable or a comparator. I can only achieve an UNBOUND queue using a LinkedBlockingQueue but that’s of course not sortable !

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:45:26+00:00Added an answer on June 7, 2026 at 6:45 pm

    The ThreadPoolExecutor transforms the Runnable you submit into a RunnableFuture using the newTaskFor() method, and adds this RunnableFuture to the queue. So if you want to use a PriorityQueue, you should override the newTaskFor() method to make sure the RunnableFuture instances it creates are comparable.

    Or you could also use the execute() method instead of the submit() method, to bypass the task creation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume a method with the following signature: public static void foo(String arg1, String args2,
I have a quick question about Java synchronization. Please assume the following code: public
I have a quick question about Java synchronization. Please assume the following code: public
Let's assume I have the following domain: public class Movie { public string Id
Lets assume we have the following array var arr = new string[] {foo,bar,jar,\r,a,b,c,\r,x,y,z,\r); Also
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please assume the following contrived JavaScript: function do_something() { var x = 5; function
Assume the following code (Please read my question in the code comments in the
Could somebody please provide the code to do the following: Assume there is a
assume the following class is given: class Base{ public: Base() {} Base( const Base&

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.