What is the equivalent in Java for this C# declaration ?
public class AsyncThreadPool<T, K> where T : IAsyncThread<K> {
and IAsyncThread
is an interface
public interface IAsyncThread<T>
{
T GetAsyncUsedObject();
void StartAsyncRequest();
}
I have tried :
public class AsyncThreadPool<T extends IAsyncThread<K>, K >
But is not correct, as T implements IAsyncThread<K>
not extends it.
And I need in this class to use T.StartAsyncRequest()
or similar
In C# it is:
T asyncThread = default(T);
asyncThread.StartAsyncRequest()
Your <T extends IAsyncThread<K>>
is correct. Even though the class itself implements, not extends, the terminology for the generic definition is extends. If you wanted, you could use <T extends Object & IAsyncThread<K>>
or <T extends Object, IAsyncThread<K>>
but would be unnecessary.
For creating a member of type T, the only thing you really have at your disposal is to use a factory object.
public class AsyncThreadPool<T extends IAsyncThread<K>, K> {
private final AsyncThreadFactory<T> factory;
public ASyncThreadPool(AsyncThreadFactory<T> factory) {
this.factory = factory;
}
public void foo() {
T t = factory.createDefault();
t.startAsyncRequest();
}
}