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

  • Home
  • SEARCH
  • 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 8335265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:37:27+00:00 2026-06-09T03:37:27+00:00

I have an interface like this: public interface BatchSynchronisedPool<R extends Runnable> { void execute(R

  • 0

I have an interface like this:

public interface BatchSynchronisedPool<R extends Runnable> {
    void execute(R runnable, Object batchIdentifier);
    public <T> Future<T> submit(Callable<T> callable, Object batchIdentifier);
}

I want to infer an upper bound for Callable, but still want to be able to keep the <T> argument on the method:

public interface BatchSynchronisedPool<R extends Runnable, C extends Callable> {
    void execute(R runnable, Object batchIdentifier);
    public <T> Future<T> submit(C<T> callable, Object batchIdentifier);
}

Obviously that doesn’t work, because the type of C I specify may only take a specific range of T arguments. However, now you have the general just of what I am trying to do, is there a possible solution, or am I stuck always having to submit a callable? (Or remove generics altogether and perform an unsafe cast)

  • 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-09T03:37:30+00:00Added an answer on June 9, 2026 at 3:37 am

    I’m not 100% certain, but don’t think that you can do what you’re trying to do here. Since C is not generic, you can’t use C<T>. Lots of code below, but tl;dr take option 3. All that really changes in the end is how many BatchSynchronisedPool objects you’ll need to create, which is not really a considerable overhead…


    1. Keep the <T> generic type parameter on the method, submit a Callable<T> and perform a runtime check of the types, like your original solution, in the class that implements this interface.

    public interface BatchSynchronisedPool<R extends Runnable> {
        void execute(R runnable, Object batchIdentifier);
        public <T> Future<T> submit(Callable<T> callable, Object batchIdentifier);
    }
    
    public class MyBSP<R, C> implements BatchSynchronisedPool<R, C> {
        void execute(R runnable, Object batchIdentifier) { ... }
        public <T> Future<T> submit(Callable<T> callable, Object batchIdentifier) {
            // Check types.
            if (!(callable instanceof MyDesiredCallableClass)) {
                throw new IllegalArgumentException("Types don't match.");
            }
    
            // Do work.
            T result = callable.call();
    
            ...
        }
    }
    
    public class MyUsageClass {
         public static void main(String[] args) {
             // Submit string.
             MyBSP<Runnable> bsp = new MyBSP<Runnable>();
             bsp.submit(new StringCallable(), someObject1);
    
             // Submit integer.
             bsp.submit(new IntegerCallable(), someObject2);
         }
    }
    

    2. Keep the <T> generic type parameter on the method, submit a C and perform a cast, like you’ve suggested, in the class that implements this interface.

    public interface BatchSynchronisedPool<R extends Runnable, C extends Callable> {
        void execute(R runnable, Object batchIdentifier);
        public <T> Future<T> submit(Class<T> cls, C callable, Object batchIdentifier);
    }
    
    public class MyBSP<R, C> implements BatchSynchronisedPool<R, C> {
        void execute(R runnable, Object batchIdentifier) { ... }
        public <T> Future<T> submit(Class<T> cls, C callable, Object batchIdentifier) {
            // Do work... with a cast.
            T result = cls.cast(callable.call());
    
            ...
        }
    }
    
    public class MyUsageClass {
         public static void main(String[] args) {
             // Submit string.
             MyBSP<Runnable, Callable> bsp = new MyBSP<Runnable, Callable>();
             bsp.submit(new StringCallable(), someObject1);
    
             // Submit integer.
             bsp.submit(new IntegerCallable(), someObject2);
         }
    }
    

    3. Create a new BatchSynchronisedPool for each type T that you’re trying to submit by specifying T as a generic type parameter for the class. Then each time you want to call submit on a different type, you’d need to generate a new instance of BatchSynchronisedPool.

    public interface BatchSynchronisedPool<T, R extends Runnable, C extends Callable<T>> {
        void execute(R runnable, Object batchIdentifier);
        public Future<T> submit(C callable, Object batchIdentifier);
    }
    
    public class MyBSP<T, R, C> implements BatchSynchronisedPool<T, R, C> {
        void execute(R runnable, Object batchIdentifier) { ... }
        public Future<T> submit(C callable, Object batchIdentifier) {
            // Do work.  Types are okay; no checking or casting needed!
            T result = callable.call();
    
            ...
        }
    }
    
    public class MyUsageClass {
         public static void main(String[] args) {
             // Submit string.
             MyBSP<String, Runnable, Callable<String>> stringBsp = new MyBSP<String, Runnable, Callable<String>>();
             stringBsp.submit(new StringCallable(), someObject1);
    
             // Submit integer.
             MyBSP<Integer, Runnable, Callable<Integer>> integerBsp = new MyBSP<Integer, Runnable, Callable<Integer>>();
             integerBsp.submit(new IntegerCallable(), someObject2);
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface like this public interface IField<T> extends IsSerializable { public String
I have crated an interface like this public interface NetworkInterface { public void onReceive();
Let's say I have an interface, like this: public interface ILoggable { void Log(Func<string>
I have an interface definded like this: public interface IDatabase{ void Get<TTypeToFetch> ();} and
I have an interface like this: public interface DataObject { ... public void copyFrom(DataObject
Suppose you have an interface like this: public interface IDoSomething<out T> { T DoSomething(object
I have an interface like this: public interface IUser{ //some properties here T ToDerived(User
I have an interface and a class implementing the interface like this: public interface
I have an Interface like this: namespace QuickRoutes.Model.Utilities { public interface IRoutesManager { bool
I have a proxied method in a MongoRepository extender class like this: public interface

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.