runnable

java多线程编程之Future/FutureTask和Callable

夙愿已清 提交于 2019-12-04 20:25:32
有这样一种场景,用多线程发送数据到某个服务器,需要知道各个线程是否都发送成功,等所有线程都发送完成才能继续下一轮计算和发送。如果用传统的多线程方式,就需要启动多个线程,然后在每个线程中分别发送数据,外部通过某种方式等待各个线程全部都发送完成,再进行后面的计算等流程。这种实现方式的代码会比较臃肿,在java中提供了一种Callable+Future的方法,可以将异步的多线程调用变为同步方式。 Callable 在java的多线程编程中,有Thread和Runnable两种方式来新建线程,其中Runnable封装了一个异步运行的任务,可以认为是一个没有任何参数和返回值的异步方法。Callable接口类似于Runnable,两者都是为那些其实例可能被另一个线程执行的类设计的,不同之处在于: Runnable不会返回结果,并且无法抛出经过检查的异常。而Callable是有返回结果并且可能抛出异常的。 Runnable定义了run方法,而Callable定义了一个不带任何参数的叫做call的方法。 此外,Callable接口的类型参数也是返回值的类型。 public interface Callable { /** * Computes a result, or throws an exception if unable to do so. * * @return computed

How to stop runnable when the app goes to background?

六眼飞鱼酱① 提交于 2019-12-04 18:30:36
问题 I am trying to establish a runnable which can load ads by every 5 sec interval (of course 5 sec is too fast, it's just for testing purpose) Here is my code: package com.admobsdk_dfp_handler; import com.google.ads.*; import com.google.ads.doubleclick.*; import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.view.Menu; import android.widget.RelativeLayout; public class AdMobSDK_DFP_Handler extends Activity { private DfpAdView adView; private Handler

Scenario of extending Thread class and implementing Runnable interface [duplicate]

独自空忆成欢 提交于 2019-12-04 17:48:26
问题 This question already has answers here : “implements Runnable” vs “extends Thread” in Java (42 answers) The difference between the Runnable and Callable interfaces in Java (12 answers) subclass of thread implementing Runnable interface (2 answers) Closed 2 years ago . I am new to thread programming in Java and hence this basic question. (I checked, but could not find this question previously asked) I read that threads can be created either by inheriting the Thread class or by implementing the

How do I change the rate or period of a repeating task using ScheduledExecutorService? [duplicate]

醉酒当歌 提交于 2019-12-04 17:37:35
问题 This question already has answers here : ScheduledExecutorService with variable delay (5 answers) Closed last year . I have a modified version of the bluetooth chat sample app. I have set up a ScheduledExecutorService which sends a command over bluetooth at a predefined rate using scheduleAtFixedRate . I have set up a PreferenceActivity to allow the period to be modified by the user. But I'm unsure how to get the actual recurring tasks to happen with the updated period. Do I need to cancel

Android thread runnable performance

给你一囗甜甜゛ 提交于 2019-12-04 10:08:43
I'm wondering about performance and cpu/ram requirements for 2 different methods of starting runnables I have some code that collects sensor data every 10ms and inserts the values into a database on a background thread (using a single thread executor). Executor service is created as follows: executor = Executors.newSingleThreadExecutor(); One way to do that would be something like... public void onSensorChanged(SensorEvent event) { //get sensor values //insert into database executor.execute(new Runnable(){ //database insert code here }); } I see this method a lot in tutorials, but because I'm

Runnable jar runs too slow compared to eclipse project

人走茶凉 提交于 2019-12-04 09:34:08
I have extracted a jar file from an eclipse project but it runs too slow. It takes almost twenty minutes to complete and the eclipse project only takes some seconds. I exported runnable jar with library handling with all three differenct choices. I also exported jar file with all library handling choices. I also run jar file with command: java -Xmx2048m -Xms1024m -jar "finalJar.jar" I have removed all System.out.println except the last one that gives me the answer. What can I do to export a jar that is almost fast as the original project? Or run it with a different way to be faster? Because

Which thread does Runnable run on?

让人想犯罪 __ 提交于 2019-12-04 09:25:59
I want to update UI every 100ms. After searching in StackOverflow, I found a solution using Runnable and Handler like this final Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { //update UI here handler.postDelayed(this, 100); } }; runnable.run(); It works! But I have some questions: Which thread does this Runnable run on? MainThread or another thread? Here is the docs about postDelay Handler is attached MainThread, so is Runnable running on MainThread? If Runnable is running on MainThread, why needs Handler ? According to my knowledge,

Single thread pool vs one thread pool per task

断了今生、忘了曾经 提交于 2019-12-04 04:40:20
问题 I want to use concurrency in Java to make requests to an online API, download and parse the response documents, and load the resulting data into a database. Is it standard to have one pool of threads in which each thread requests, parses, and loads? In other words, only one class implements Runnable . Or is it more efficient to have, say, three different pools of threads, with the first pool of threads making the requests and pushing them to a queue, the second pool of threads polling from

Jar File - Prevent Access to Source Code

依然范特西╮ 提交于 2019-12-04 03:37:45
问题 I want to hand over a small Java app as a runnable jar but I do not want anybody to have access to my source code. Am I right in presuming that there is no source code (.java files) included with a jar file? User269799 回答1: You are right, there is no source code in the jar (unless you configure your build system to specifically put it in there). But you are always at the risk you code gets decompiled from the bytecode. An obfuscater might help here. 回答2: Assuming you don't put the java files

Android - Correct Multithreading

你。 提交于 2019-12-03 21:38:44
Can someone please give me a hand with this image downloading code? I want it to run in the background, but it seems like new Thread(new Runnable()) is definitely not the way to go, according to the Android docs , and I'm not sure how else to approach this: // caller while( exhibitorCursor.moveToNext() ) { new Thread(new Runnable() { public void run() { downloadImage(exhibitorId, exhibitorString, DOWNLOAD_EXHIBITOR); } }).start(); } // first function public void downloadImage(long id, String externalImageUrl, int type) { // logic junk here if( !(new File(localImageName).exists()) ) {