runnable

Update Android UI from a thread in another class

99封情书 提交于 2019-12-03 20:20:50
I've seen a few questions on here asking similar questions, but I've not yet seen a suitable answer. Many people have asked how to update the UI from a thread, but they're almost always in the same class as the UI. What I'm trying to do is update the UI from a thread which has been created in another class. I've seen all of the suggestions, such as async, handlers, runnable, etc... but I've having real trouble implementing them in separate classes. I'm trying to keep my UI class minimal and only deal with interactions with the GUI, such as when a user presses a button. Now, I've created a new

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

别来无恙 提交于 2019-12-03 12:26:13
This question already has an answer 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 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 Runnable interface. I saw a code which had both to the same class. public class ThreadExample extends Thread implements Runnable

Does the future object returned by executorService.submit(Runnable) hold any reference to the runnable object?

橙三吉。 提交于 2019-12-03 11:38:28
Let's assume we have the following code: List<Future<?>> runningTasks; ExecutorService executor; ... void executeTask(Runnable task){ runningTasks.add(executor.submit(task)); } My questions are: Does runningTasks hold a reference to the task object? How long does it hold it for? Does it still hold it after the task is complete? In order to avoid memory leaks do I have to take care to remove the future that was added to the list? Until when the executor or the Future object holds a reference to it is an implementation detail . Therefore, if your tasks use a lot of memory such that you have to

Calling @Transactional methods from another thread (Runnable)

帅比萌擦擦* 提交于 2019-12-03 11:32:56
问题 Is there any simple solution to save data into database using JPA in a new thread? My Spring based web application allows user to manage scheduled tasks. On runtime he can create and start new instances of predefined tasks. I am using spring's TaskScheduler and everything works well. But I need to save boolean result of every fired task into database. How can I do this? EDIT: I have to generalize my question: I need to call a method on my @Service class from tasks. Because the task result has

java command pattern example with Runnable class : Is Receiver missing?

女生的网名这么多〃 提交于 2019-12-03 09:55:52
问题 From Examples of GoF Design Patterns in Java's core libraries question, it was quoted that All implementations of java.lang.Runnable are examples of Command pattern. As per my understanding of Command pattern, Client calls Invoker => Invoker calls ConcreteCommand => ConcreteCommand calls Receiver method, which implements abstract Command method. Have a look at this working example Command pattern UML diagram from this article is shown as below. Have a look at this code: public class

Android Asynctask vs Runnable vs timertask vs Service

空扰寡人 提交于 2019-12-03 07:58:13
问题 What are the differences between these methods (classes)? I want to run a app that runs every 5 seconds, clear the memory when it is finished and when the cpu is in standby mode, that you can run the app. So that the app is not bound to a wakelock. Regards, Shafqat 回答1: The difference between first three is just the amount of work that has been done for you. And a Service is a fundamental Android application component. AsyncTask as a convenience class for doing some work on a new thread and

Calling @Transactional methods from another thread (Runnable)

让人想犯罪 __ 提交于 2019-12-03 01:50:55
Is there any simple solution to save data into database using JPA in a new thread? My Spring based web application allows user to manage scheduled tasks. On runtime he can create and start new instances of predefined tasks. I am using spring's TaskScheduler and everything works well. But I need to save boolean result of every fired task into database. How can I do this? EDIT: I have to generalize my question: I need to call a method on my @Service class from tasks. Because the task result has to be "processed" before saving into database. EDIT 2: Simplified version of my problematic code comes

java command pattern example with Runnable class : Is Receiver missing?

南笙酒味 提交于 2019-12-03 01:32:16
From Examples of GoF Design Patterns in Java's core libraries question, it was quoted that All implementations of java.lang.Runnable are examples of Command pattern. As per my understanding of Command pattern, Client calls Invoker => Invoker calls ConcreteCommand => ConcreteCommand calls Receiver method, which implements abstract Command method. Have a look at this working example Command pattern UML diagram from this article is shown as below. Have a look at this code: public class ThreadCommand{ public static void main(String args[]){ Thread t = new Thread(new MyRunnable()); t.start(); } }

Android Asynctask vs Runnable vs timertask vs Service

喜欢而已 提交于 2019-12-02 20:38:47
What are the differences between these methods (classes)? I want to run a app that runs every 5 seconds, clear the memory when it is finished and when the cpu is in standby mode, that you can run the app. So that the app is not bound to a wakelock. Regards, Shafqat The difference between first three is just the amount of work that has been done for you. And a Service is a fundamental Android application component . AsyncTask as a convenience class for doing some work on a new thread and use the results on the thread from which it got called (usually the UI thread) when finished. It's just a

Can't create handler inside thread that has not called Looper.prepare() on some devices

☆樱花仙子☆ 提交于 2019-12-02 09:14:23
问题 I know there are already a lot of questions like this but I don't see what I'm doing wrong. The app crashes without anything shown. Also, the error doesn't occur on my device or emulator. Just on some devices (say 30-40%?). Can't create handler inside thread that has not called Looper.prepare() MainActivity.java public class MainActivity extends Activity implements Runnable { Gebruiker gebruiker = new Gebruiker(); private DatabaseHelper db; Context context; @Override protected void onCreate