在Java中设计并发线程时使用Runnable
和Callable
接口有什么区别,为什么要选择一个而不是另一个呢?
现在很清楚元类是什么,有一个相关的概念,我一直在使用,而不知道它的真正含义.
我想每个人都用括号做错了,导致"对象不可调用"异常.更重要的是,使用__init__
并__new__
导致想知道这种血腥__call__
可以用于什么.
你能给我一些解释,包括魔术方法的例子吗?
我正在开发一个项目,其中我有多个接口和两个实现类,需要实现这两个接口.
假设我的第一个接口是 -
public Interface interfaceA {
public void abc() throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
它的实施是 -
public class TestA implements interfaceA {
// abc method
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼它 -
TestA testA = new TestA();
testA.abc();
Run Code Online (Sandbox Code Playgroud)
现在我的第二个界面是 -
public Interface interfaceB {
public void xyz() throws Exception;
}
Run Code Online (Sandbox Code Playgroud)
它的实施是 -
public class TestB implements interfaceB {
// xyz method
}
Run Code Online (Sandbox Code Playgroud)
我这样称呼它 -
TestB testB = new TestB();
testB.xyz();
Run Code Online (Sandbox Code Playgroud)
问题陈述:-
现在我的问题是 - 有什么办法,我可以并行执行这两个实现类吗?我不想按顺序运行它.
意思是,我想并行运行TestA
和TestB
实现?这可能吗?
我想在这里使用Callable但不知道如何在这里使用带有void返回类型的Callable -
我们以TestB类为例:
public interface interfaceB …
Run Code Online (Sandbox Code Playgroud) 我正在尝试运行此代码,我有一个列表列表.我需要添加到内部列表,但我得到错误
TypeError: 'list' object is not callable.
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我这里我做错了什么.
def createlists():
global maxchar
global minchar
global worddict
global wordlists
for i in range(minchar, maxchar + 1):
wordlists.insert(i, list())
#add data to list now
for words in worddict.keys():
print words
print wordlists(len(words)) # <--- Error here.
(wordlists(len(words))).append(words) # <-- Error here too
print "adding word " + words + " at " + str(wordlists(len(words)))
print wordlists(5)
Run Code Online (Sandbox Code Playgroud) 由于使用ExecutorService
可submit
一个Callable
任务并返回Future
,为什么需要使用FutureTask
包装Callable
的任务和使用的方法execute
?我觉得他们都做同样的事情.
我已经创建了一段代码,它接受一个IP地址(来自另一个类中的main方法),然后循环遍历一系列IP地址,并在每个IP地址上执行.我有一个GUI前端,它正在崩溃(因此我为什么要完成多线程.我的问题是我不能再将IP地址作为我的ping代码中的参数作为其可调用的.我已经搜遍了所有为此,似乎无法找到解决这个问题的方法.有一种方法可以让一个可调用的方法来获取参数吗?如果没有,有没有其他方法可以实现我想要做的事情?
我的代码示例:
public class doPing implements Callable<String>{
public String call() throws Exception{
String pingOutput = null;
//gets IP address and places into new IP object
InetAddress IPAddress = InetAddress.getByName(IPtoPing);
//finds if IP is reachable or not. a timeout timer of 3000 milliseconds is set.
//Results can vary depending on permissions so cmd method of doing this has also been added as backup
boolean reachable = IPAddress.isReachable(1400);
if (reachable){
pingOutput = IPtoPing + " is reachable.\n";
}else{
//runs ping command once …
Run Code Online (Sandbox Code Playgroud) 我遇到了一些高级的java代码(对我来说很先进:))我需要帮助理解.
在类中有一个嵌套类,如下所示:
private final class CoverageCRUDaoCallable implements
Callable<List<ClientCoverageCRU>>
{
private final long oid;
private final long sourceContextId;
private CoverageCRUDaoCallable(long oid, long sourceContextId)
{
this.oid = oid;
this.sourceContextId = sourceContextId;
}
@Override
public List<ClientCoverageCRU> call() throws Exception
{
return coverageCRUDao.getCoverageCRUData(oid, sourceContextId);
}
}
Run Code Online (Sandbox Code Playgroud)
稍后在外部类中,创建了一个可调用类的实例.我不知道这是什么:
ConnectionHelper.<List<ClientCoverageCRU>> tryExecute(coverageCRUDaoCallable);
Run Code Online (Sandbox Code Playgroud)
它对我来说看起来不像java语法.你能详细说明这种神秘的语法会发生什么吗?您可以在代码摘录中看到它在下面使用.
CoverageCRUDaoCallable coverageCRUDaoCallable = new CoverageCRUDaoCallable(
dalClient.getOid(), sourceContextId);
// use Connection helper to make coverageCRUDao call.
List<ClientCoverageCRU> coverageCRUList = ConnectionHelper
.<List<ClientCoverageCRU>> tryExecute(coverageCRUDaoCallable);
Run Code Online (Sandbox Code Playgroud)
EDITED 添加了ConnectionHelper类.
public class ConnectionHelper<T>
{
private static final Logger logger =
LoggerFactory.getLogger(ConnectionHelper.class); …
Run Code Online (Sandbox Code Playgroud) 我在Spring服务中定义了一个长期运行的任务.它由Spring MVC控制器启动.我想HttpResponse
在服务结束之前启动服务并返回给调用者.该服务最后将文件保存在文件系统上.在javascript中,我创建了一个轮询工作来检查服务状态.
在Spring 3.2中我找到了@Async
注释,但是我不明白它DeferredResult
与它的区别Callable
.我何时必须使用@Async
,何时使用DeferredResult
?
我发现这个方法在python中链接,但即使有了它我也无法理解Python中的方法链接.
这里的目标是两个:解决编码问题并理解方法链(考虑到我仍然没有100%对callables有信心).
直到问题定义.
我想要一个有两个方法的类:一个设置object ='line'的参数,另一个设置覆盖'bar'.
这是我到目前为止所得到的:
class foo():
def __init__(self, kind=None):
self.kind = kind
def __call__(self, kind=None):
return foo(kind=kind)
def my_print(self):
print (self.kind)
def line(self):
return self(kind='line')
def bar(self):
return self(kind='bar')
Run Code Online (Sandbox Code Playgroud)
可悲的是,通过这段代码,我可以实现我的目标
a = foo()
a.bar().line().bar().bar().line().my_print()
Run Code Online (Sandbox Code Playgroud)
但是我希望通过编写这段代码来获得相同的结果
a = foo()
a.bar.line.bar.bar.line.my_print()
Run Code Online (Sandbox Code Playgroud)
我该如何实现这一目标?我想我定义__call__
方法的方法有点不对劲.在此先感谢您的帮助.
我想知道使用Callable在Spring中使用@Async和Servlet 3异步请求实现的一般用例.
据我了解,@ Async用于使任何方法(特别是任何服务方法)异步执行.
@Async
void doSomething(String s) {
// this will be executed asynchronously
}
Run Code Online (Sandbox Code Playgroud)
以及任何返回Callable的控制器
@RequestMapping("/view")
public Callable<String> callableWithView(final Model model) {
return new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(2000);
model.addAttribute("foo", "bar");
model.addAttribute("fruit", "apple");
return "views/html";
}
};
}
Run Code Online (Sandbox Code Playgroud)
我很困惑,什么时候使用什么.使用异步servlet/controller和spring @Async会产生什么影响?
callable ×10
java ×5
python ×3
asynchronous ×2
arguments ×1
deferred ×1
futuretask ×1
generics ×1
interface ×1
ping ×1
runnable ×1
spring ×1
spring-mvc ×1