`

Callable Usage

阅读更多

The important thing need to look at is, use multiple threads(Callable), let them run together, and retrieve results from them.

package com.oreilly.tiger.ch10;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.Callable;
import static java.lang.System.out;

public class RandomPrimeSearch implements Callable {
private static final Random prng = new SecureRandom();

private int bitSize;

public RandomPrimeSearch(int bitSize) {
this.bitSize = bitSize;
}

public BigInteger call() {
return BigInteger.probablePrime(bitSize, prng);
}

public static void main(String args[])
{

//use ExecutorService to organize many Callables.
ExecutorService service = Executors.newFixedThreadPool(5);
Future prime1 = service.submit(new RandomPrimeSearch(512));
Future prime2 = service.submit(new RandomPrimeSearch(512));
Future prime3 = service.submit(new RandomPrimeSearch(512));

try {
BigInteger bigger = (prime1.get().multiply(prime2.get())).multiply(prime3.get());
out.println(bigger);
} catch (InterruptedException e) {
e.printStackTrace(out);
} catch (ExecutionException e) {
e.printStackTrace(out);
}

}
}

==========================================================

Uage of FutureTask
If you have only one Callable, you can just

1) use FutureTask, wrap your Callables.

2) use Thread, wrap your FutureTask, start the thread

3) retrieve results from FutureTask.



FutureTask<BigInteger> task = new FutureTask<BigInteger>(new RandomPrimeSearch(512));

new Thread(task).start( );

BigInteger result = task.get( );

 

分享到:
评论

相关推荐

    Seamless R and C++ Integration

    Rcpp attributes provide a high-level syntax for declaring C++ functions as callable from R and automatically generating the code required to invoke them. Attributes are intended to facilitate both ...

    Android代码-LifeCycleBinder

    The usage is simple, you just need to create a class that implements LifeCycleAware and modify your Activity/Fragment adding a field annotated with @BindLifeCycle and invoking LifeCycleBinder.bind...

    python 3000 and you(最新可编辑文档).ppt

    4. **Deprecated Functions and Keywords:** Several functions like `cmp()`, `reduce()`, `apply()`, `callable()`, and `reload()` were removed or replaced, and new keywords like `as`, `with`, and `...

    Lambda表达式最佳实践

    Usage usage = parameter -&gt; parameter; ``` 这比使用匿名内部类更加简洁和易于阅读。 5. **避免重写Functional Interface作为参数的方法**: 当一个方法接受Functional Interface作为参数时,应避免在实现中...

    python3.6.5参考手册 chm

    Performance and resource usage PEP 397: Python Launcher for Windows PEP 3151: Reworking the OS and IO exception hierarchy PEP 380: Syntax for Delegating to a Subgenerator PEP 409: Suppressing ...

    Professional C# 3rd Edition

    Usage Conventions 74 Summary 81 Chapter 3: Objects and Types 83 Classes and Structs 84 Class Members 85 Data Members 85 Function Members 85 xi Contents readonly Fields 99 Structs 101 Structs Are Value...

    google api php client

    [!... ... The Google API Client Library enables you to work with Google APIs such as Google+, Drive, or YouTube on your server. These client libraries are officially supported by Google....

    Python中装饰器兼容加括号和不加括号的写法详解

    if len(args) == 1 and len(kwargs) == 0 and callable(args[0]): # Actual decorated function return f(args[0]) else: # Decorator arguments return lambda realf: f(realf, *args, **kwargs) return new...

    Python Cookbook, 2nd Edition

    Simplifying Usage of Strings' translate Method Recipe 1.10. Filtering a String for a Set of Characters Recipe 1.11. Checking Whether a String Is Text or Binary Recipe 1.12. Controlling Case ...

    detour 2.1

    Detours preserves the un-instrumented target function (callable through a trampoline) as a subroutine for use by the instrumentation. Our trampoline design enables a large class of innovative ...

    Python 2.4 Quick Reference Card (Letter) (2007).pdf

    Callable Objects....................................2 Calling Functions...................................3 Functions Control..................................3 Decorators................................

Global site tag (gtag.js) - Google Analytics