callable

并发编程之Callable和Future接口、FutureTask类

末鹿安然 提交于 2020-03-17 12:29:30
某厂面试归来,发现自己落伍了!>>> Callable接口代表一段可以调用并返回结果的代码;Future接口表示异步任务,是还没有完成的任务给出的未来结果。所以说Callable用于产生结果,Future用于获取结果。 Java 5在concurrency包中引入了java.util.concurrent.Callable 接口,它和Runnable接口很相似,但它可以返回一个对象或者抛出一个异常。 其中Runnable可以提交给Thread来包装下,直接启动一个线程来执行,而Callable则一般都是提交给ExecuteService来执行。Executor就是Runnable和Callable的调度容器,Future就是对于具体的调度任务的执行结果进行查看,最为关键的是Future可以检查对应的任务是否已经完成,也可以阻塞在get方法上一直等待任务返回结果。 Runnable和Callable的差别 就是Runnable是没有结果可以返回的,并且Runnable无法抛出返回结果的异常,就算是通过Future也看不到任务调度的结果的。 Callable接口使用泛型去定义它的返回类型。Executors类提供了一些有用的方法在线程池中执行Callable内的任务。由于 Callable任务是并行的(并行就是整体看上去是并行的,其实在某个时间点只有一个线程在执行)

java并发callable,runnable,future和futureTask

假如想象 提交于 2020-02-28 21:10:10
java多线程 java实现多线程有两种方式:一个是直接继承Thread类,一种是实现Runnable接口。但这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果。于是后面又增加了Callable和Future,通过它们可以在任务执行完毕之后得到任务执行结果。 callable与runnable java.lang.Runnable是一个接口,在它里面只声明了一个run()方法: public interface Runnable { public abstract void run(); } 由于run()方法返回值为void类型,所以在执行完任务之后无法返回任何结果。   Callable位于java.util.concurrent包下,它也是一个接口,在它里面也只声明了一个方法,只不过这个方法叫做call(): 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(

Python property callable

我的未来我决定 提交于 2020-02-03 04:24:31
问题 Is there any way to have a property and a method with the same name? I mean a property that can be used the usual way and to be callable at the same time? Like this: >>> b = Book() >>> b.pages 123 >>> b.pages() 123 >>> b.pages(including_toc=False) 123 >>> b.pages(including_toc=True) 127 回答1: No, you can't. () always calls an object from the expression on its left-hand side. What this means is, that b.pages() can be read as follows: _tmp = b.pages _tmp() As you can see, methods are attributes.

invokeAll() is not willing to accept a Collection<Callable<T>>

别来无恙 提交于 2020-01-22 18:51:47
问题 I fail to understand why this code won't compile ExecutorService executor = new ScheduledThreadPoolExecutor(threads); class DocFeeder implements Callable<Boolean> {....} ... List<DocFeeder> list = new LinkedList<DocFeeder>(); list.add(new DocFeeder(1)); ... executor.invokeAll(list); The error msg is: The method invokeAll(Collection<Callable<T>>) in the type ExecutorService is not applicable for the arguments (List<DocFeeder>) list is a Collection of DocFeeder , which implements Callable

How do I fix this “TypeError: 'str' object is not callable” error?

我的梦境 提交于 2020-01-21 02:57:27
问题 I'm creating a basic program that will use a GUI to get a price of an item, then take 10% off of the price if the initial price is less than 10, or take 20% off of the price if the initial price is greater than ten: import easygui price=easygui.enterbox("What is the price of the item?") if float(price) < 10: easygui.msgbox("Your new price is: $"(float(price) * 0.1)) elif float(price) > 10: easygui.msgbox("Your new price is: $"(float(price) * 0.2)) I keep getting this error though: easygui

How do I fix this “TypeError: 'str' object is not callable” error?

泪湿孤枕 提交于 2020-01-21 02:57:17
问题 I'm creating a basic program that will use a GUI to get a price of an item, then take 10% off of the price if the initial price is less than 10, or take 20% off of the price if the initial price is greater than ten: import easygui price=easygui.enterbox("What is the price of the item?") if float(price) < 10: easygui.msgbox("Your new price is: $"(float(price) * 0.1)) elif float(price) > 10: easygui.msgbox("Your new price is: $"(float(price) * 0.2)) I keep getting this error though: easygui

Performing a long calculation that returns after a timeout

半城伤御伤魂 提交于 2020-01-06 01:06:07
问题 I want to perform a search using iterative deepening, meaning every time I do it, I go deeper and it takes longer. There is a time limit (2 seconds) to get the best result possible. From what I've researched, the best way to do this is using an ExecutorService, a Future and interrupting it when the time runs out. This is what I have at the moment: In my main function: ExecutorService service = Executors.newSingleThreadExecutor(); ab = new AB(); Future<Integer> f = service.submit(ab); Integer

How to run two classes in parallel using multithreading?

时光怂恿深爱的人放手 提交于 2020-01-03 15:34:38
问题 I am working on a project in which I have multiple interface and two Implementations classes which needs to implement these two interfaces. Suppose my first Interface is - public Interface interfaceA { public String abc() throws Exception; } And its implementation is - public class TestA implements interfaceA { // abc method } I am calling it like this - TestA testA = new TestA(); testA.abc(); Now my second interface is - public Interface interfaceB { public String xyz() throws Exception; }

Why is 'module' object not callable? [duplicate]

心不动则不痛 提交于 2020-01-02 20:17:40
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: TypeError: ‘module’ object is not callable This is my very first Python attempt, just trying to regain basic programming knowledge after a 10 year silence in a, for me, new language, Python. The basic idea is a tiny battly engine which decides the better hit. The bugging code is next. self.__power = self.__att*random(2,4)/dier.__defn As my python knowledge is extremely basic, I'm rather scared of saying the

How to schedule a Callable to run on a specific time?

断了今生、忘了曾经 提交于 2020-01-01 17:51:08
问题 I need to run a callable at a specific time of day. One way to do it is to calculate the timediff between now and the desired time , and to use the executor.scheduleAtFixedRate . Have a better idea? executor.scheduleAtFixedRate(command, TIMEDIFF(now,run_time), period, TimeUnit.SECONDS)) 回答1: For this kind of thing, just go ahead and install Quartz. EJB has some support for this kind of thing but really you just want Quartz for scheduled tasks. That being said, if you insist on doing it