每当我运行实现可调用的程序时,我都会以顺序形式获得输出。
就像,这是我的程序:
package com.handson;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class WorkSheet_1 implements Callable<String> {
/**
* @param args
*/
private int id;
static int count = 0;
public static String test[] = { "a1" , "a2" , "a3" , "a4" , "a5" , "a6" , "a7" , "a8" ,
"b1" , "b2" , "b3" , "b4" , "b5" , "b6" , "b7" , "b8" ,
"c1" , "c2" …Run Code Online (Sandbox Code Playgroud) 没有太多文档可以了解 runInTransaction() 方法到底如何工作。在不同的 DAO 上执行多个操作时,如果不返回任何值,我可以使用runInTransaction(Runnable body)ORrunInTransaction(Callable<V> body)如果要返回任何结果,
我的查询: 如果事务中的所有查询都成功,那么我想返回一个图像对象,需要在事务成功时将其上传到服务器如果发生任何异常或事务不成功,我需要返回一个布尔值false 指示用户发生了某些错误。
方法如下:
public boolean userCheckedIn(final User user) {
try {
appDatabase.runInTransaction(new Callable<Object>() {
@Override
public Object call() throws Exception {
if (user != null) {
//Add entry in table A
appDatabase.UserDao().add(user);
//Update entry in table B
//Delete an entry from table C
Event image = updateUserAction(action);
return image;
}
return null;
}
});
} catch (Exception e) {
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
在上面的方法中,我打算做的是,如果所有数据库执行都成功,我需要返回一个将上传到服务器的图像。如果在执行数据库事务时发生任何异常或发生任何错误,我需要返回false以让用户知道发生了错误。不确定我是否做对了。另外,我应该将 …
在 C++ 鸭子类型中,有可调用的概念,例如可以像函数一样调用的符号。我想用 Delphi 泛型来模拟这一点。
我遇到一个问题,我需要一个可以接受普通过程、类方法或 lambda 作为参数的过程。现在我有 3 个几乎相同的重载实现。有没有办法使用 Delphi 泛型来避免这种重复?
我需要在一天的特定时间运行一个可调用的.一种方法是计算now和所需时间之间的timediff,并使用executor.scheduleAtFixedRate.
有更好的主意吗?
executor.scheduleAtFixedRate(command, TIMEDIFF(now,run_time), period, TimeUnit.SECONDS))
class Thing():
xyz = "I'm a string"
class Truc():
def xyz(self):
return "I'm a function"
def valueOrCalledValue(input):
if callable(input):
return input()
else:
return input
thing = Thing()
print valueOrCalledValue(thing.xyx)
>>> "I'm a string"
truc = Truc()
print valueOrCalledValue(truc.xyz)
>>> "I'm a function"
Run Code Online (Sandbox Code Playgroud)
是否有内置功能可以完成我的valueOrCalledValue工作?
我正在尝试使用callables实现Fibonacci序列,并将我的Fibonacci的初始值调用为3,4,5,6和2000.我得到的输出如下:
3 5 8 13 -820905900187520670
问题是当我试图在我的可调用中计算fib(2000)时.有人可以看看下面提供的代码,看看我的方法出错了:
import java.util.concurrent.*;
import java.util.*;
class FibonacciGen implements Callable<Long>{
private Long fib;
public FibonacciGen(long num){
this.fib = num;
}
public Long call(){
return calculateFibonacci(fib);
}
private long calculateFibonacci(long someNum){
long firstNum = 0L;
long secondNum = 1L;
long counter = 0L;
while(counter<someNum){
long fibCalc = secondNum+firstNum;
firstNum = secondNum;
secondNum = fibCalc;
counter= counter+1L;
}
return secondNum;
}
}
public class FibonacciCallable{
public static void main(String[] args){
ExecutorService exec = Executors.newCachedThreadPool();
ArrayList<Callable<Long>> results = …Run Code Online (Sandbox Code Playgroud) 我有一个非常奇怪和令人沮丧的错误.
问题很简单:
>>> mylist
[70.71, 67.23, 60.1, 62.52, 64.14, 65.4, 68.84, 61.04, 66.95, 62.22, 63.73, 62.04, 57.12, 61.3, 65.48, 61.49, 66.94, 62.68, 60.31, 64.38, 62.84, 63.03, 67.12, 60.65, 61.68, 64.0, 62.91, 61.36, 60.65, 62.45, 64.22, 66.4, 59.96, 57.03, 66.4, 60.43, 64.05, 64.09, 50.94, 39.84, 45.12, 63.39, 55.62, 55.58, 58.04, 59.91, 60.05, 57.3, 61.83, 63.87, 50.58, 62.56, 60.75, 58.9, 62.99, 61.65, 59.09, 59.91, 64.66, 61.1, 61.31, 59.62, 56.65, 60.1, 66.04, 60.57, 59.77, 53.0, 60.84, 61.75, 64.53, 52.0, 62.08, 67.69, 60.62, 55.04, …Run Code Online (Sandbox Code Playgroud) public class myService {
@Autowired
ExecutorService executor;
public Result getServiceResult(Token token, Profile profile){
//validate token and profile
return getResult(token, profile).get();
}
private getResult (Token token, Profile profile){
Future<Result> = threadPoolExecutor.submit(
() -> myManager.createAccount(token, profile));
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码在我目前的工作中运行良好.我无法理解如何threadPoolExecutor.submit通过"功能/方法"但不能通过callable?
我正在使用Java 8和Spring框架.
我正在将LU分解matlab代码转换为python.
但是当我这样做时,我遇到了这个错误
'numpy.ndarray' object is not callable
当我尝试测试我的代码时会发生此错误.这是我的代码,谁能帮助解决这个问题?我在等你的帮忙.
import numpy as np
def LU(a):
[m,m]=a.shape
for k in range(0,m-1,1):
a[k+1:m-1,k]=a[k+1:m-1,k]/a(k,k)
a[k+1:m-1,k+1:m-1]=a[k+1:m-1,k+1:m-1]-a[k+1:m-1,k]*a[k,k+1:m-1]
L=np.eye(m,m)+np.tril(a,-1)
U=np.triu(a)
return [L,U]
b=np.array([[1,0,0],[0,1,0],[0,0,1]])
LU(b)
Run Code Online (Sandbox Code Playgroud) 我试图理解Python中的“可调用对象”是什么,以及类可调用的含义。我在玩以下代码:
class A(object):
def __init__(self):
pass
print("Is A callable? " + str(callable(A)))
a=A()
print("created a")
a()
Run Code Online (Sandbox Code Playgroud)
得到以下结果:
Is A callable? True
created a
Traceback (most recent call last):
File "test2.py", line 11, in <module>
a()
TypeError: 'A' object is not callable
Run Code Online (Sandbox Code Playgroud)
此外,
print(type(A.__call__()))
Run Code Online (Sandbox Code Playgroud)
给出:
<class '__main__.A'>
Run Code Online (Sandbox Code Playgroud)
这是否意味着A类具有__call__方法?为什么是class类?
A.__call__()每次使用A()实例化时都会被调用吗?
callable ×10
python ×4
java ×3
android ×1
android-room ×1
concurrency ×1
database ×1
delphi ×1
fibonacci ×1
future ×1
generics ×1
java-8 ×1
long-integer ×1
min ×1
numpy ×1
runnable ×1
scheduling ×1