Question: Using Java, is there a way to reference a method and have spawn off into a thread dynamically?

For example, if you have a method called `spawn()` which would take a parameter of another method reference and it would then spawn off a thread which would run the referenced method. I imagine the code would look something like:

```
void spawn(**wayToReferenceANumberOfDifferentMethods currentMethodToLaunch**) {
    new Thread(){
        public void run() {
            // **way to launch the currentMethodToLaunch**
        }
    }.start();
}

```

In order for the above to work, I think I would need to be able to reference a type of `method`. If this isn't possible, why is that so?

Answer: 
You can pass an object that implements Runnable to the spawn method. 

```
void spawn(Runnable myRunnable) {
    Thread thread = new Thread(myRunnable); 
    thread.start();
}

```

You can have multiple classes that implement the Runnable interface, each doing something different. The object you pass to the spawn method would determine which one of them would actually run. 

Here is what the class that implements Runnable would look like:

```
public class MyClass implements Runnable 
{   
  public void run()     
  {
     // do what you need here
  }
} 

```

Until Java 8, interfaces were used to "pass methods around", like described above. With Java 8, you have Lambda expressions, which allow you to simulate this behavior better.
As other answers have indicated you probably just want to pass in `Runnable`s or `Callable`s and use those as the basis for executing the method.

However, if you really want to pass around methods than you can use the [Reflection API](http://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html) to accomplish this. It's somewhat cumbersome, but it is definitely doable.

```
public void spawn(final Method callme, final Object objectThatMethodIsAMemberOf, final Object...args) {
  new Thread() {
    @Override
    public void run() {
      try {
        callme.invoke(objectThatMethodIsAMemberOf, args);
      } catch(Exception e) {
        // A bunch of things can go wrong for example
        // you call the method on the wrong type of object
        // you supply the wrong number and/or type of arguments
        // the method is private and you don't have permission to call it
        // the method runs, but throws an exception
        // probably some more that I'm forgetting
      }
    }
  }.start()
}

```