| 129 |
underlying <code>NativeImpl</code>. </p> |
underlying <code>NativeImpl</code>. </p> |
| 130 |
|
|
| 131 |
<pre> |
<pre> |
| 132 |
class SingleThreadAdapter implements InvocationHandler { |
class SingleThreadAdapter { |
| 133 |
|
|
| 134 |
public SingleThreadAdapter (String library) { |
public SingleThreadAdapter (String library) { |
| 135 |
this.executor = Executors.newSingleThreadExecutor(); |
this.executor = Executors.newSingleThreadExecutor(); |
| 184 |
public Object invoke (Object proxy, |
public Object invoke (Object proxy, |
| 185 |
final Method method, |
final Method method, |
| 186 |
final Object[] args) throws Throwable { |
final Object[] args) throws Throwable { |
| 187 |
|
try { |
| 188 |
Callable task = new Callable () { |
Callable task = new Callable () { |
| 189 |
public Object call () throws Exception { |
public Object call () throws Exception { |
| 190 |
method.invoke(instance, args); |
try { |
| 191 |
|
return method.invoke(instance, args); |
| 192 |
|
} |
| 193 |
|
catch (Exception e) { |
| 194 |
|
throw e; |
| 195 |
|
} |
| 196 |
|
catch (Throwable e) { |
| 197 |
|
throw RuntimeException(e); |
| 198 |
|
} |
| 199 |
} |
} |
| 200 |
}; |
}; |
|
try { |
|
| 201 |
FutureTask ftask = new FutureTask(task) |
FutureTask ftask = new FutureTask(task) |
| 202 |
executor.execute(ftask); |
executor.execute(ftask); |
| 203 |
return ftask.get(); |
return ftask.get(); |
| 227 |
NativeInterface ni = (NativeInterface) |
NativeInterface ni = (NativeInterface) |
| 228 |
adapter.newInstance(new Callable<NativeInterface> () { |
adapter.newInstance(new Callable<NativeInterface> () { |
| 229 |
public NativeInterface call () { |
public NativeInterface call () { |
| 230 |
return new NativeImpl(); |
return new NativeImpl(); // may use native calls |
| 231 |
} |
} |
| 232 |
}); |
}); |
| 233 |
int i = ni.nativeIntMethod(); |
int i = ni.nativeIntMethod(); |