runnable

Handler/Runnable delays producing events that are out of sync sometimes

二次信任 提交于 2019-12-06 05:04:43
When trying to learn how to create a delay I researched and found the dominant answer to be to use Handler/Runnable/postDelayed. Handler handler=new Handler(); final Runnable r = new Runnable() { public void run() { delayedMethod(); } }; handler.postDelayed(r, 1000); That worked ok for a while, but I've added a few more things going on and now they are sometimes happening in the wrong order. This set of events: paintScreen1() ... delayedPaintScreen2() ... paintScreen3() is screwing up (sometimes) and doing this: paintScreen1() ... paintScreen3() ... delayedPaintScreen2() (runs last and gets

Android Chronometer own implementation

一世执手 提交于 2019-12-06 04:19:46
I've developed an own implementation of a Chronometer. I did the follow: Create a Service (CronoService), that use a Runnable object that execute the thread. The Runnable will loop each 0.1 secs. Messenger Object that will receive messages from Ui to Start, Pause or Resume the Chronometer. Intent that will broadcast the time after each loop of the Runnable object. The code is: public class CronoService extends Service { public static final int PARAR = 0; public static final int EMPEZAR = 1; public static final int ESTABLECER_TIEMPO = 2; private static final String TAG = "BroadcastService";

How to implement a queue of runnables

陌路散爱 提交于 2019-12-06 03:37:27
问题 I am trying to implement a queue of runnables to be executed one after another(meaning the next in the queue will execute after the other has completed) during an asynchronous task. I wrote a Manager to manage these runnables and task that is a runnable itself. I then get the first task in the Asynchronous task and run it in hopes that it will run through the queue, however It just ends up running the first runnable twice. Can anyone help me with the code I have been working with or point me

Which thread does Runnable run on?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 02:53:47
问题 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

Running a task at a specific time using postDelayed

守給你的承諾、 提交于 2019-12-06 02:41:07
I would like to start a task at a specific time. For that I use runnable and postDelayed method as follows: private Runnable mLaunchTask = new Runnable() { public void run() { try { MY TASK } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }; In my code I use mLunchTask as follows: mHandler = new Handler(); mHandler.postDelayed(mLaunchTask, myDelay*1000); and myDelay is computed as follows: s = DateFormat.format("hh:mm:ss aaa", d.getTime()); cTime = s.toString(); // current Time ch = Integer.parseInt(cTime.substring(0,2)); // current

Is it too costly to do a (Runnable & Serializable) when you want to send an anonymous function?

你说的曾经没有我的故事 提交于 2019-12-06 00:06:09
I am doing sht like: executor.execute((Runnable & Serializable)func); Where func is an anonymous function, I have to heavily use this in the project otherwise I would have to create a class for every different function I want to call and implement Runnable and Serializable in each of those class, the advantage would be that I would have the type at compile time rather than casting it at run time, I would like to know if doing this cast is too costly or is trivial and does not represent a big gap in performance. If you have a real life experience on this and you are willing to share it, it

Loop with delay period

痞子三分冷 提交于 2019-12-05 18:04:46
I have a TextView and I want each second to highlight another letter in the word. For example: h e l l o - h e l l o - h e l l o - h e l l o - h e l l o What I have done: int i = 0; String text; Handler handler = new Handler(); public void spanText(String txt) { text = txt; for(int i=0; i<text.length(); i++) { handler.post(runnable); } Runnable runnable = new Runnable() { @Override public void run() { Spannable spannable = Spannable.Factory.getInstance().newSpannable(text); StyleSpan style = new StyleSpan(Typeface.BOLD); spannable.setSpan(style, i, i+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

ScheduledExecutorService execute every night at 12 AM UTC Time

六月ゝ 毕业季﹏ 提交于 2019-12-05 12:48:18
I want to start ScheduledExecutorService exactly 12 AM daily ,Schedule has to Start at today at 22/02/2017 00:00:00 (UTC TIME),Can any one tell me Whether my code is Correct or not? DateTime today = new DateTime().withTimeAtStartOfDay(); DateTime startOfTommorrow = today.plusDays(1).withTimeAtStartOfDay(); Long midnight = startOfTommorrow.getMillis(); long midnights = (midnight / 1000) / 60; final DateFormat nextDateTymFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("***********************************"); System.out.println("Schedule Updater "+nextDateTymFormat.format

Creating Jar file - doesn't work on other computers

只愿长相守 提交于 2019-12-05 07:44:59
I'm trying to package my program into a JAR file so it can be used on multiple computers. My program is composed of start.java , userinterface.java and writer.java . The program, written in Eclipse, works perfectly on my computer. When exported, it will work on my computer but cause the following error on other computers: "Could not find the main class: start. Program will exit". Again, my program runs fine on my computer when I double click on it. I've tried creating the JAR file via command prompt, and my Manifest file is correct. What is happening? This is a very strange bug which I've also

Java: How do I catch InterruptedException on a thread, when interrupted by another thread?

徘徊边缘 提交于 2019-12-05 07:42:52
I'm developing a multithreaded application to make connections to external servers - each on separate threads - and will be blocked until there is input. Each of these extends the Thread class. For the sake of explanation, let's call these "connection threads". All these connection threads are stored in a concurrent hashmap. Then, I allow RESTful web services method call to cancel any of the threads. (I'm using Grizzly/Jersey, so each call is a thread on its own.) I retrieve the specific connection thread (from the hashmap) and call the interrupt() method on it. So, here is the question,