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 8777235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:13:59+00:00 2026-06-13T19:13:59+00:00

I have 1 Interface called A that needs to be implemented by a class

  • 0

I have 1 Interface called A that needs to be implemented by a class I load dynamically after the program start. Lets call it B.

This interface provides x (more than 1) Methods. Lets call em from a() to z().
Now I have to wrap this class for some time measuring and control issues and run it in its own thread to be able to kill it if it takes too long.

Therefore I have invented class C that wraps B because Bdoesn’t implement runnable on its own.

The next part ist the Class the original Program should call. New Class D. D implements interface A as well to hide the whole controlling part from the model.

Now I have to wrap the methods of the interface in D and send them over and Callable to C who unwraps them and executes them on Object B.

Here some example code who I imagined it can be:

public class D implements A {

private C ai;

public D(String aiName) {
    ai = new C("trivialKi");
}

private void call(parameters, ORIGIN_METHOD origin) {
    AiTaskExecutor task = new AiTaskExecutor(parameters, origin, ai);
    FutureTask<Long> tsk = new FutureTask<Long>(task);

    Thread thread = new Thread(tsk);
    thread.start();
    if (abort) {
            tsk.cancel(true);
    }
}

@Override
public void a(g g, f f, t t) {
    call(g, f, t, ORIGIN_METHOD.a);
}

@Override
public void b(g g, t t, h h) {
    call(g, t, h, ORIGIN_METHOD.b);
}

@Override
public void c(g g, t t, f f) {
    call(g, t, f, ORIGIN_METHOD.c);
}
}

And in class C the obvious switch case with that enum to pass the parameters to the right method on the class B that is held in class C als private field.

Dou you have a better solution in mind?
I personally don’t like the enum thing and if the parameters are too different this does not work very well.

Is there a “standard” solution for things like that?

  • 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-13T19:14:00+00:00Added an answer on June 13, 2026 at 7:14 pm

    The standard solution for this is: Use a “Dynamic Proxy” (java.lang.reflect.Proxy) for A. This saves you almost all your boilerplate code.

    This site and Google contains enough usage examples for Proxy.

    Also: I propose not to use a new thread for each call – this is extremely expensive if the called methods are short. You can use the Callable interface instead Runnable and a threadpool Executor instead. This also allows you to have return values in your interface 🙂

    EDIT

    Just for fun I coded the dynamic proxy and the executor thing.

    Given the following interface A and a sample implementation B:

    interface A {
        int a(int g, int f, int h);
        int b(int x);
    }
    
    class B implements A {
        @Override
        public int a(int g, int f, int t) {
            System.out.println("called a in thread "+Thread.currentThread().getName());
            return 42;
        }
        @Override
        public int  b(int x) {
            System.out.println("called b in thread "+Thread.currentThread().getName());
            return 21;
        }
    }
    

    A proper Callable using reflection to call any java.lang.reflect.Method looks like this:

    class ReflectiveMethodCallable implements Callable<Object> {
        private Object target;
        private Method method;
        private Object[] args;
    
        public ReflectiveMethodCallable(Object target, Method method, Object[] args) {
            this.target = target;
            this.method = method;
            this.args = args;
        }
    
        @Override
        public Object call() throws Exception {
            return method.invoke(target, args);
        }
    }
    

    The part where such a ReflectiveMethodCallable is created and given to an ExecutorService is the InvocationHandler of java.lang.reflect.Proxy:

    class MyInvocationHandler implements InvocationHandler {
        private Object target;
        private ExecutorService executorService;
    
        public MyInvocationHandler(Object target, ExecutorService executorService) {
            this.target = target;
            this.executorService = executorService;
        }
    
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            try {
                Callable<Object> task = new ReflectiveMethodCallable(target, method, args);
                Future<Object> future = executorService.submit(task);
                return future.get();
            }
            catch(ExecutionException e1){
                try {
                    throw e1.getCause();
                } catch(InvocationTargetException e2){
                    throw e2.getCause();
                }
            }
        }
    }
    

    The InvocationHandler is used when creating a new Proxy in createProxyFor. The rest of the Main class is used for a SSCCE example:

    public class Main {
        public static void main(String[] args) throws InterruptedException {
            ExecutorService executorService = Executors.newCachedThreadPool();
    
            // get B somehow
            A a = new B();
    
            // get proxy for B 
            A proxy = createProxyFor(a, executorService);
    
            // call proxy
            System.out.println("current thread: "+Thread.currentThread().getName());
    
            int resultA = proxy.a(1,2,3);
            System.out.println("calling a returned "+resultA);
    
            int resultB = proxy.b(1);
            System.out.println("calling b returned "+resultB);
    
        }
    
        static A createProxyFor(A a, ExecutorService executorService){
            InvocationHandler h = new MyInvocationHandler(a, executorService);
            A proxy = (A)Proxy.newProxyInstance(A.class.getClassLoader(), new Class[]{A.class}, h);
            return proxy;
        }
    }
    

    The output:

    current thread: main
    called a in thread pool-1-thread-1
    calling a returned 42
    called b in thread pool-1-thread-1
    calling b returned 21
    

    To finish up:

    • Each method in A will be called in another thread.
    • Threads are reused by a thread pool.
    • Additional logic (e.g. time measurement, timeout control) can be done either in invoke or in call.
    • No need to code anything for each method.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a service class implemented in Java 6 / Spring 3 that needs
Let's say I have a class that implements the IDisposable interface. Something like this:
Let's say I have an interface called XMLControl, and I need every class that
I have an interface (called Subject ) that has the following method: public void
I have an interface that contains a single method called ListAll() using System.Collections.Generic; namespace
I have a scene, called testScene, it works like this: @interface testScene : myScene
So I have this strucutre - an interface called Animal : public interface Animal
I have a program that needs to read data from a number of sources.
I have an interface called Dictionary which has a method insert() . This interface
I have a class in my domain model called JobPlan this class is stored

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.