我无法理解为什么这段代码不能编译
ExecutorService executor = new ScheduledThreadPoolExecutor(threads);
class DocFeeder implements Callable<Boolean> {....}
...
List<DocFeeder> list = new LinkedList<DocFeeder>();
list.add(new DocFeeder(1));
...
executor.invokeAll(list);
Run Code Online (Sandbox Code Playgroud)
错误消息是:
The method invokeAll(Collection<Callable<T>>) in the type ExecutorService is
not applicable for the arguments (List<DocFeeder>)
Run Code Online (Sandbox Code Playgroud)
list是Collection的DocFeeder,它实现了Callable<Boolean>-这是怎么回事?
我有三个问题.
为了解释,我正在审查某人的代码,并注意到BufferedReader有时候没有关闭.通常,Eclipse会发出警告,这是一个潜在的内存泄漏(我修复了它).但是,在Callable内部类中,没有警告.
class outerClass {
...
public void someMethod() {
Future<Integer> future = outputThreadPool.submit(new innerClass(this.myProcess.getInputStream(), threadName));
...
}
class innerClass implements Callable<Integer> {
private final InputStream stream;
private final String prepend;
innerClass(InputStream stream, String prepend) {
this.stream = stream;
this.prepend = prepend;
}
@Override
public Integer call() {
BufferedReader stdOut = new BufferedReader(new InputStreamReader(stream));
String output = null;
try {
while ((output = stdOut.readLine()) != null) {
log.info("[" + prepend + "] " + output);
}
} catch …Run Code Online (Sandbox Code Playgroud) 我正在理解细粒度util.concurrency.Java的实现在哪里Callable并Future位于JVM中?
我找到了Future类,它描述了Java lang中的高级未来,我试图找到它在较低级别描述的位置.
总而言之,找到Future和Callable的实际实现会很有意思,例如:处理Future.get()或Callable.call()的JVM部分,并规定它们应该如何工作.
期待您的回复,Akonkagva
本文从各种stackoverflow问题中多次链接,描述了带参数的装饰器在语法上与不带参数的装饰器的区别.
__init__()是执行装饰的唯一方法,__call__()每次调用装饰时都会调用它sayHello()."__call__(),只能接受一个参数(函数对象),并且必须返回替换原始的装饰函数对象.请注意,__call__()现在只调用一次,在装修期间,之后您返回的装饰功能将__call__()用于实际通话."文章中给出的解释并没有告诉我为什么语言是这样建立的:
虽然这种行为是有道理的 - 构造函数现在用于捕获装饰器参数,但该对象
__call__()不能再用作装饰函数调用,因此您必须使用它__call__()来执行装饰 - 它仍然是令人惊讶的第一次你看到了
此设置有两个相关功能让我感到不舒服:
__init__和__call__在这两个,但他们意味着不同的事情?__call__在具有参数的装饰器的情况下使用除了调用装饰函数之外的其他目的(顾名思义,至少来自无参数情况)?由于__call__只有后立即调用__init__,为什么不直接传递给装饰作为参数传递给函数__init__和处理,将在发生一切__call__的__init__呢?我有一个模型Project,我得到了以下instr的属性
attr = getattr(project, 'id', None)
Run Code Online (Sandbox Code Playgroud)
project是实例,id是字段,None是默认的返回类型.我的问题是如果我想用这个获得FK键怎么办?
project.customer.name
如何获得具有上述条件的客户名称?
if callable(attr):
context[node][field] = '%s' % attr()
Run Code Online (Sandbox Code Playgroud)
context = {'project': {}}
fields = ('id', 'name', 'category', 'created_by', customer)
for field in fields:
attr = getattr(project, field, None)
if callable(attr):
context['project'][field] = '%s' % attr()
else:
context['project'][field] = attr
Run Code Online (Sandbox Code Playgroud)
我需要在这里调整客户对象.我可以给类似customer__name或customer.name在我的领域,并逐渐加入顾客的名字,但我不知道.
我希望从一个方法得到一个结果可能需要一段时间才能完成并且实际上不会返回该对象,所以我想尽可能有效地处理它.这是我想要实现的一个例子:
public static void main (String[] args) {
Object obj = someMethod();
System.out.println("The object is" + obj + ", wooh!");
}
public void callObject() {
// Sends request for the object
}
public void receiveObject(Object object) {
// Received the object
}
public Object someMethod() {
callObject();
// delay whilst the object is being received
// return received object once received, but how?
}
Run Code Online (Sandbox Code Playgroud)
方法callObject将调用获取对象,但是在对象中调用另一个方法.我希望someMethod()能够调用该对象,然后返回它最终收到的内容,即使实际调用和接收是单独的方法.
我已经研究过使用FutureTasks和Callables,我认为这是前进的方向,我只是不太确定如何实现它.
对不起,如果我没有解释得太好,我会在必要时提供更多信息.
谢谢!
目前我正在为不同的调用约定(__stdcall,__ cdecl,__ fastcall等)构建仿函数(可调用类型).使用包装器,我将能够做到这样的事情:
void __stdcall foo(int arg)
{
std::printf("arg: %i\n", arg);
}
int main(int, char**)
{
Function<void, int> v{foo};
v(1337);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
目前,我为__stdcall调用约定构建了一个包装器,只要指定了正确的参数并传入了正确的参数,就可以调用任何__stdcall函数.该类如下所示:
template <typename ReturnT, typename... Args>
class Function
{
// NOTE: This version of my callable types
// only supports the __stdcall calling
// convention. I need support for __cdecl,
// __fastcall and also __thiscall.
using return_t = ReturnT;
using callable_t = return_t(__stdcall*)(Args...);
private:
callable_t mCallable;
public:
template <typename FuncT>
Function(FuncT const &func) :
mCallable(func)
{
; …Run Code Online (Sandbox Code Playgroud) 我以某种方式发现该call()方法适用于每个函数。使用这种方法,我可以将我的更改if (callback != null) callback()为callback?.call().
所以我试图找到 的实现和文档call(),但我找不到。它只是内置方法吗?这个方法的实现是什么?Function.apply()里面会叫吗?
假设我要实现一个简单的装饰器@notifyme,在调用装饰函数时打印一条消息。我希望装饰器接受一个参数来打印定制消息;参数(以及参数周围的括号)可以省略,在这种情况下将打印默认消息:
@notifyme('Foo is invoked!')
def foo():
pass
@notifyme # instead of @notifyme()
def bar():
pass
Run Code Online (Sandbox Code Playgroud)
为了允许省略括号,我必须提供以下两种实现@notifyme:
第一个实现允许用户自定义消息,因此它接受一个字符串作为参数并返回一个装饰器:
def notifyme_customized(message: str) -> Callable[[Callable], Callable]:
def decorator(func: Callable) -> Callable:
def decorated_func(*args, **kwargs):
print(str)
return func(*args, **kwargs)
return decorated_func
return decorator
Run Code Online (Sandbox Code Playgroud)
第二个实现本身是一个装饰器,并使用第一个实现来打印默认消息:
def notifyme_default(func: Callable) -> Callable:
return notifyme_customized('The function is invoked.')(func)
Run Code Online (Sandbox Code Playgroud)
为了使上面的两个实现使用相同的名称notifyme,我曾经functools.singledispatch动态地将调用分派notifyme到两个实现之一:
# This is a complete minimal reproducible example
from functools import singledispatch
from typing import Callable …Run Code Online (Sandbox Code Playgroud) 我正在尝试为类方法创建一个可调用变量。
class Person {
method walk(Str $direction) {
say "Walking $direction";
}
}
Run Code Online (Sandbox Code Playgroud)
我可以为“walk”方法创建一个可按预期工作的可调用变量。
my $person = Person.new;
my $callable = $person.^find_method('walk');
$person.$callable('up'); # OUTPUT: "Walking up"
Run Code Online (Sandbox Code Playgroud)
现在我想创建一个可调用函数,它将使用参数“up”调用方法“walk”。
my $callable = $person.^find_method('walk').assuming('up');
$person.$callable();
# Type check failed in binding to parameter '$direction'; expected Str but got Person (Person.new)
# in sub __PRIMED_ANON at EVAL_7 line 1
# in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
$person.$callable(不带括号)给出相同的错误
我尝试“直接”调用它,但出现了不同的错误。
$person.^find_method('walk')('up')
# Too few positionals passed; expected 2 arguments but got 1 …Run Code Online (Sandbox Code Playgroud) callable ×10
java ×4
python ×3
arguments ×1
asynchronous ×1
c++ ×1
concurrency ×1
currying ×1
dart ×1
decorator ×1
eclipse ×1
future ×1
futuretask ×1
getattr ×1
methods ×1
models ×1
python-3.8 ×1
raku ×1
runnable ×1
theory ×1