runnable

The difference between the Runnable and Callable interfaces in Java

我的梦境 提交于 2019-12-17 01:23:10
问题 What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other? 回答1: See explanation here. The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception. 回答2: What are the differences in the applications of Runnable and Callable . Is

subclass of thread implementing Runnable interface

▼魔方 西西 提交于 2019-12-14 03:53:30
问题 It confuses me why a subclass of thread that implements a runnable interface doesn't force me to override the run method. Basically, when i create simple class that implements Runnable it forces me to override the run method. But when i made the ordinary class a subclass of thread, it didn't force me to override the class anymore. What's the logic behind this? 回答1: When a non-abstract class declares that it implements an interface, what that means is that the class must have a concrete

Shutdown or Not Shutdown? in ExecutorService (Java8)

梦想的初衷 提交于 2019-12-14 01:46:53
问题 I am trying to understand the behaviour of the executor service relative to shutdown. The documentation says that the application won't terminate unless there is a shutdown() call - but in this simple example. It exits after one minute precisely. Any idea? Runnable r = new Runnable() { @Override public void run() { Print.println("do nothing"); } }; ThreadFactory TF = (Runnable run) -> new Thread(run); ExecutorService exec = Executors.newCachedThreadPool(TF); exec.submit(r); returns this: 11

Get current instance of Runnable

本小妞迷上赌 提交于 2019-12-13 20:05:45
问题 I'm making an application that allows users to view task lists stored in different databases. So what happens is that, I have a list of the names of browsable databases (stored as a text file). Program loads the first database in that list and displays contents. Then from a menu, I allow users to select another database in the list. (Kind of like, I want to view the tasks for Andy, and now Bob, and now Carl...). Problem is, I don't know how to update the UI so that the contents of the new

Execute two Runnables Android

风流意气都作罢 提交于 2019-12-13 17:40:32
问题 I'd like to make two Runnables work for my Android App: 1) Runnable r1 for changing an ImageView: Runnable r1 = new Runnable() { @Override public void run() { albumpic.setImageResource(pub[i]); i++; if(i >= pub.length) { i = 0; } albumpic.postDelayed(this, 3000); } }; albumpic.postDelayed(r1, 3000); 2) Runnable r2 for changing a TextView: Runnable r2 = new Runnable(){ @Override public void run(){ out = "Title: " + retriever.extractMetadata(ShoutCastMetadataRetriever.METADATA_KEY_TITLE) + "

Using Variables on UI Thread from Worker Thread

♀尐吖头ヾ 提交于 2019-12-13 15:22:35
问题 The Android Developers site states that two rules must be followed when working in the UI thread, i.e., Do not block the UI thread Do not access the Android UI toolkit from outside the UI thread* Does this mean that I can access variables in the UI thread from within a worker thread, i.e., not UI toolkit? If so, do any special considerations need to be given if the variable is being continually updated, e.g., from a SensorEventListener . Thanks. 回答1: Does this mean that I can access variables

Odd behavior with Runnable and ExecutorService

試著忘記壹切 提交于 2019-12-13 04:28:42
问题 I'm getting some really weird behavior with multithreading. I have two classes: DipoleTester and Dipole. DipoleTester attempts to create several Dipole objects, and then run them asynchronously. The problem is that DipoleTester just runs all of its Dipole objects at once, rather than 2 at a time. Here is DipoleTester: public class DipoleTester { public static String DIR = "./save/"; public static void main(String[] args) throws InterruptedException { Dipole trial; ExecutorService service =

Does Runnable share the data structure if they are getting changed?

家住魔仙堡 提交于 2019-12-13 03:29:29
问题 I am creating a Runnable in the following way: public class AbcRunnable implements Runnable { Qwe qwe; Rst rst; public void run() { // some operations on qwe and rst which are changing their value } } public class AbcThreadPool { private final AbcThreadPoolExecutor executor; public InventoryAvailabilityThreadPool(final AbcRunnableFactory factory, final Integer poolSize) { executor = new AbcThreadPoolExecutor(factory, poolSize); for (int i = 0; i < poolSize; ++i) { executor.execute(factory.get

Extending an Interface vs Instantiating via Anonymous Class

£可爱£侵袭症+ 提交于 2019-12-13 02:35:36
问题 NOTE: I am aware that this is dangerously close to many other questions. I haven't seen any, however, that do not pertain specifically to Android's OnClickListener interface. I am asking in a general sense. I understand the difference between instantiating an interface via an anonymous class... a la: private final Runnable runnable = new Runnable() { @Override public void run() { draw(); } }; ... and extending the interface. public class ClassyClass implements Runnable { ... //do other cool

java thread start not working

时光怂恿深爱的人放手 提交于 2019-12-12 11:50:57
问题 I have a simple exercise I am trying to do involving threads. (a) Create a class called SumAction that implements Runnable. The class contains 3 instance variables – start, end, and sum. start and end are initialized by the constructor. sum is set to 0. (b) The run() method should have a for loop which should find the sum of all values from start to end. There should be a method getSum() to return the value of sum. (c) In main create 2 instances of this Runnable class, one which takes 1 and