runnable

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

主宰稳场 提交于 2019-12-21 03:47:07
问题 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? 回答1: Until when the executor or the Future object holds a

NetworkOnMainThreadException on Runnable

可紊 提交于 2019-12-20 07:24:12
问题 I am making Android 4.4 project. I've got NetworkOnMainThreadException . Below is my process. Service(sticky) -> Handler(per 5 minutes) -> Runnable -> HttpPost Isn't Runnable a separate thread? Shoud I use AsyncTask in Runnable? 回答1: Runnable is a simple interface, that, as per the Java documentation, " should be implemented by any class whose instances are intended to be executed by a thread ." (Emphasis mine.) For instance, defining a Runnable as follows will simply execute it in the same

Java - Run a string as normal code [duplicate]

假如想象 提交于 2019-12-20 06:25:35
问题 This question already has answers here : Convert String to Code (18 answers) How to compile .java file from within java program [duplicate] (3 answers) Closed 5 years ago . Is there a way to run a string as code? I mean if I had a string run having value System.out.println("Hello World) could I run the string as normal code the then output will be Hello World? For Example: String code = "System.out.println("Hello World)"; code.run(); //I know this doesn't work Console: Hello World 回答1: You

Stopping Thread in Java

僤鯓⒐⒋嵵緔 提交于 2019-12-20 03:30:33
问题 if i have got such java code: public static void main(String[] args) { for(int i = 0;i<100;i++) { Future<?> f = ThreadPoolManager.getInstance().schedule(new START(), 500); f.cancel(true); } } private class START implements Runnable { @Override public void run() { System.out.println(1); } } And run it in debug, i can see that all of those threads(after cancel) are still running, so are they taking my memory too? And if yes, how can i destroy those Threads completely? 回答1: cancel(true) calls

How to stop a thread after it has completed the runnable?

谁都会走 提交于 2019-12-19 10:56:07
问题 I have a list of tasks and a limited number of threads. The goal is to time how long the tasks take to finish using this number of threads. I know something is wrong with the way I am using threads and Runnable object. I am new to them and can't seem to figure out how to fix it. It errors with a java.lang.OutOfMemoryError: Java heap space error on the line worker.start() after a few seconds. Here is my code: public class Tasks { static Timer timer; //times how long it takes to complete all

Given two Java threads, stop one thread when one of them finishes

六眼飞鱼酱① 提交于 2019-12-18 08:46:50
问题 I'm looking for a clean design/solution for this problem: I have two threads, that may run as long as the user wants to, but eventually stop when the user issues the stop command. However if one of the threads ends abruptly (eg. because of a runtime exception) I want to stop the other thread. Now both threads execute a Runnable (so when I say 'stop a thread' what I mean is that I call a stop() method on the Runnable instance), what I'm thinking is to avoid using threads (Thread class) and use

Updating UI from a service (using a handler?)

為{幸葍}努か 提交于 2019-12-17 20:14:41
问题 I am trying to update my UI in FirstActivity when I receive a notification but is confused by runOnUiThread , Runnable and Handler . Here is what I have: I am running FirstActivity and NotificationService. When NotificationService reeives a notification, it will update FirstActivity UI. I also have another service AlarmService running. First Activity @Override public void onResume() { super.onResume(); //some other code for alarm service } NotificationService //on receiving notification

How does one implement a truly asynchronous java thread

五迷三道 提交于 2019-12-17 18:35:22
问题 I have a function that needs to perfom two operations, one which finishes fast and one which takes a long time to run. I want to be able to delegate the long running operation to a thread and I dont care when the thread finishes, but the threads needs to complete. I implemented this as shown below , but, my secondoperation never gets done as the function exits after the start() call. How I can ensure that the function returns but the second operation thread finishes its execution as well and

Thread not interrupting even though I'm calling thread.interrupt()

女生的网名这么多〃 提交于 2019-12-17 13:59:44
问题 I'm learning how to use threads in Android, and to do that I've made a small application that plays a series of notes. The idea is that there is a start button and an end button and that (obviously) if you press the start button it starts playing music, and if you press the end button, it stops. The start button works just fine, but the problem is that the end button doesn't. I'm having trouble figuring out why, so maybe some of you can help me out. This is the code: public class PressAndPlay

Should you synchronize the run method? Why or why not?

此生再无相见时 提交于 2019-12-17 06:43:11
问题 I have always thought that synchronizing the run method in a java class which implements Runnable is redundant. I am trying to figure out why people do this: public class ThreadedClass implements Runnable{ //other stuff public synchronized void run(){ while(true) //do some stuff in a thread } } } It seems redundant and unnecessary since they are obtaining the object's lock for another thread. Or rather, they are making explicit that only one thread has access to the run() method. But since