Java - Pass a method to a class and execute it in a new Thread
Asked Answered
K

1

7

[Check the bottom of the question for updates]

As in the title, I'd like to write a class which takes in a method and executes it in a new Thread. I lurked around SO and came up with something like:

import java.util.concurrent.Callable;

public class MyExecutor<T> implements Runnable{

    private Callable<T> method;


    public <T> MyExecutor(Callable<T> pMethod){
        this.method = pMethod;
    }

    @Override
    public void run() {
        try {
            // start a new Thread, then
            method.call();
        } catch (Exception e) {
            System.err.println("Failed calling method "+method.getClass());
            e.printStackTrace();
        }

    }

}

However Eclipse warns me that, inside the constructor, in

this.method = pMethod;

I cannot convert from Callable <T> to Callable <T>.

It smells like I'm doing something entirely wrong, but I can't grasp it.

UPDATE

Turns out I was reinventing the wheel, what I wanted to do can be attained like this:

public class MyExecutor implements Executor{


    @Override
    public void execute(Runnable command) {
        new Thread(command).start();
    }

}

In the main flow a method can be executed in a new thread like this:

    MyExecutor myExec = new MyExecutor();

    myExec.execute(new Runnable() {

        @Override
        public void run() {
            myMethod();
        }
    });
Keil answered 9/8, 2016 at 10:51 Comment(3)
you can't pass methods to other classes. you can pass instances of methods on which you can call methodsParlance
Did you try removing <T> in front of the constructor?Lengthen
The <T> before your constructor is declaring a second generic type, also called T. If you remove it, your code should be ok.Historiographer
S
6

Since you added a type parameter <T> to your constructor, it shadows the type parameter for the class. Therefore, the T for the constructor argument pMethod is a different T than the class parameter, which is what Eclipse is warning you about. Just change the signature for the constructor to public MyExecutor(...).

Sonde answered 9/8, 2016 at 10:55 Comment(5)
Thank you, you're right. Since we're here, does the idea itself (passing a method to the class, creating a thread inside my generic class and executing my method) make sense? Is it decent practice? Marked as the solution anyway.Keil
@Keil You're reinventing the wheel basically. The JDK already has executors, that provide you with the functionality (and more, with Future and other such things). At least you know that your idea is valid enough to include in the runtime ;)Wheelhouse
As Kayaman says - not only is it valid practice, it's already done. You might want to look into the java.util.concurrent.Executor hierarchy.Sonde
Thank you all, every day is a school day :)Keil
@Tom, you aren't passing a method. You are passing an object that implements the Callable interface. Java does not have functional types. Java8 fakes them with @Functional interfaces.Scallop

© 2022 - 2024 — McMap. All rights reserved.