callable

Java终结任务:Callable和Future

佐手、 提交于 2019-12-02 14:36:37
在这里首先介绍下Callable和Future,我们知道通常创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口,但是这两种方式创建的线程不返回结果,而Callable是和Runnable类似的接口定义,但是通过实现Callable接口创建的线程可以有返回值,返回值类型可以任意定义。 Callable接口 public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; } 可以看到,这是一个泛型接口,call()函数返回的类型就是传递进来的V类型。 那么怎么使用Callable呢?一般情况下是配合ExecutorService来使用的,在ExecutorService接口中声明了若干个submit方法的重载版本: <T> Future<T> submit(Callable<T> task); <T> Future<T> submit(Runnable task, T result); Future<?>

Callable和Future

我是研究僧i 提交于 2019-12-02 14:36:26
Callable与 Future 两功能是Java 5版本中加入的,Callable是类似于Runnable的接口,实现Callable接口的类和实现Runnable的类 。 Callable 的接口定义如下: public interface Callable<V> { /** * Computes a result, or throws an exception if unable to do so. * * @return computed result * @throws Exception if unable to compute a result */ V call() throws Exception; } Callable 和 Runnable 的区别如下: Callable 定义的方法是 call ,而 Runnable 定义的方法是 run 。 Callable 的 call 方法可以有返回值, 而 Runnable 的 run 方法不能有返回值 。 Callable 的 call 方法可抛出异常,而 Runnable 的 run 方法不能抛出异常。 Future 定义 Future 表示异步计算的结果,它提供了检查计算是否完成的方法,以等待计算的完成,并检索计算的结果。 Future 的 cancel 方法可以取消任务的执行,它有一布尔参数,参数为 true

line = line.strip() TypeError: 'NoneType' object is not callable

柔情痞子 提交于 2019-12-02 11:20:54
I am trying to find all num's in a list from an html using beautifulsoup : import urllib from BeautifulSoup import * import re line = None url = raw_input('Enter - ') html = urllib.urlopen(url).read() soup = BeautifulSoup(html) # Retrieve all of the anchor tags tags = soup('span') for line in tags: line = line.strip() numlist = re.findall('[0-9]+' , tags) print numlist` I'm getting a traceback: Traceback (most recent call last): File "C:\Documents and Settings\mea388\Desktop\PythonSchool\new 12.py", line 14, in line = line.strip() TypeError: 'NoneType' object is not callable I cannot

Call function with part of variadic arguments

做~自己de王妃 提交于 2019-12-02 05:28:08
问题 Consider I have the following: void bar(int a, int b) { } template<typename F, typename... Args> void foo(F function, Args... args> { function(args...); } I would like to have some kind of way to only pass the necessary amount of arguments to the function, so that I would be able to do the following, which should result in a call to bar with 1, 2 as arguments discarding the 3. Without knowing how many arguments the passed in function type F needs. foo(bar, 1, 2, 3); foo([](int a, int b){}, 1,

Call function with part of variadic arguments

谁说我不能喝 提交于 2019-12-02 00:42:50
Consider I have the following: void bar(int a, int b) { } template<typename F, typename... Args> void foo(F function, Args... args> { function(args...); } I would like to have some kind of way to only pass the necessary amount of arguments to the function, so that I would be able to do the following, which should result in a call to bar with 1, 2 as arguments discarding the 3. Without knowing how many arguments the passed in function type F needs. foo(bar, 1, 2, 3); foo([](int a, int b){}, 1, 2, 3); When I try to use the below function traits: namespace detail { template<typename F, std::size

class variable functions

╄→尐↘猪︶ㄣ 提交于 2019-12-01 20:30:54
问题 Say $this->varname is equal to a string for which is_callable() returns true. To call it I'd have to do $temp = $this->varname; $temp(); or... is there another way I could call it without having to create two lines? The problem with doing just $temp = $this->varname() is that that'll try to call a method within the class called varname() - it won't call the function at $this->varname. Thanks! 回答1: You have to do so or use call_user_func(_array) . Or as Anthony has suggested to me on the

PHP Callable Object as Object Member

时光总嘲笑我的痴心妄想 提交于 2019-12-01 17:50:48
I have a class Logger which, among other things has a method Log . As Log is the most common use of the Logger instance, I have wired __invoke to call Log Another class, "Site" contains a member "Log", an instance of Logger. Why would this work: $Log = $this->Log; $Log("Message"); But not this: $this->Log("Message"); The former fails with "PHP Fatal error: Call to undefined method Site::Log()" Is this a limitation of the callable object implementation, or am I misunderstanding something? Same reasons you can't do this: $value = $this->getArray()["key"]; or even this $value = getArray()["key"];

PHP Callable Object as Object Member

时光怂恿深爱的人放手 提交于 2019-12-01 17:14:04
问题 I have a class Logger which, among other things has a method Log . As Log is the most common use of the Logger instance, I have wired __invoke to call Log Another class, "Site" contains a member "Log", an instance of Logger. Why would this work: $Log = $this->Log; $Log("Message"); But not this: $this->Log("Message"); The former fails with "PHP Fatal error: Call to undefined method Site::Log()" Is this a limitation of the callable object implementation, or am I misunderstanding something? 回答1:

java schedule a callable at pseudo 'fixed' rate (bell curve distribution for example)

点点圈 提交于 2019-12-01 12:09:41
Currently I have a ScheduledThreadPoolExecutor which can execute a callable at a fixed rate. scheduledThreadPoolExecutor.scheduleAtFixedRate(callable, delay, waitNano, TimeUnit.NANOSECONDS); My team wants to add some randomess into the waitNano. Currently it is 1000. But we want the wait second to be 900, 1100, 800, 1200, for example. The average wait time is still 1000 but the wait second is randomly distributed, (for example like a Bell curve). I am wondering is this functionality already in java? If I have to write my own "scheduler" which has this random-waiting behavior, what do you guys

java schedule a callable at pseudo 'fixed' rate (bell curve distribution for example)

你说的曾经没有我的故事 提交于 2019-12-01 09:57:52
问题 Currently I have a ScheduledThreadPoolExecutor which can execute a callable at a fixed rate. scheduledThreadPoolExecutor.scheduleAtFixedRate(callable, delay, waitNano, TimeUnit.NANOSECONDS); My team wants to add some randomess into the waitNano. Currently it is 1000. But we want the wait second to be 900, 1100, 800, 1200, for example. The average wait time is still 1000 but the wait second is randomly distributed, (for example like a Bell curve). I am wondering is this functionality already