且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java:参数化的可运行

更新时间:2023-09-29 23:24:16

通常,您可以实现 Runnable Callable 作为支持genertic输入参数的类;例如

  public class MyRunnable< T>实现Runnable {
private final T t;

public MyRunnable(T t){
this.t = t;
}

public void run(){
//引用t。
}
}


Standard Runnable interface has only non-parametrized run() method. There is also Callable<V> interface with call() method returning result of generic type. I need to pass generic parameter, something like this:

interface MyRunnable<E> {
  public abstract void run(E reference);
}

Is there any standard interface for this purpose or I must declare that basic one by myself?

Typically you would implement Runnable or Callable as a class that supported a genertic input parameter; e.g.

public class MyRunnable<T> implements Runnable {
  private final T t;

  public MyRunnable(T t) {
    this.t = t;
  }

  public void run() {
    // Reference t.
  }
}