timertask

“Only the original thread that created a view hierarchy can touch its views.” error when using TimerTask

我的未来我决定 提交于 2021-02-05 12:24:43
问题 I have created an app composed of one single activity which include a single TextView . Here is the XML of my activity : activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="0dp" android:paddingLeft="0dp" android:paddingRight="0dp" android:paddingTop="0dp" tools:context=

“Only the original thread that created a view hierarchy can touch its views.” error when using TimerTask

空扰寡人 提交于 2021-02-05 12:22:01
问题 I have created an app composed of one single activity which include a single TextView . Here is the XML of my activity : activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="0dp" android:paddingLeft="0dp" android:paddingRight="0dp" android:paddingTop="0dp" tools:context=

How to run a particular task every week, every month and every six month?

一笑奈何 提交于 2020-01-16 05:09:11
问题 I am working on making a cron job in Java. I want to run a particular task every week, month, three month, six month and nine month. public Interface interfaceA { public String abc() throws Exception; } public class TestTaskA implements interfaceA { @Override public String abc() throws Exception { // some code } } I am running it like this - TestTaskA testTaskA = new TestTaskA(); testTaskA.abc(); I want to run TestTaskA every week, every month, every three month, every six month, every nine

How to make timer task to wait till runOnUiThread completed

十年热恋 提交于 2020-01-15 06:22:12
问题 I need to update UI after certain time period, for which I have create a timer schedule and inside it I am calling the runOnUiThread. timer.scheduleAtFixedRate(new TimerTask() { public void run() { System.out.println("1"); try { System.out.println("2"); System.out.println("3"); runOnUiThread(new Runnable() { public void run() { System.out.println("4"); System.out.println("5"); System.out.println("6"); System.out.println("7"); } }); System.out.println("8"); } catch (Exception e) { e

TimerTask can't update the JavaFX Label text

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-14 01:34:09
问题 I have a fairly simple JavaFX GUI application which has an label that shows how much time is left until a certain action starts. To achieve this, I've created a DownloadTimer class as shown below: public class DownloadTimer(){ private int minutes; private int seconds; private Timer innerTimer = new Timer(); private TimerTask innerTask; private boolean isActive; public DownloadTimer(int minutes, int seconds) { if (seconds > 60) { int minToAdd = seconds / 60; this.minutes = minutes; this

Timer源码之TaskQueue

依然范特西╮ 提交于 2020-01-07 04:49:30
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> TaskQueue类是在Timer文件中的TaskQueue,它是一个又堆实现的优先队列。TaskQueue按任务剩下一次执行时间排序的优先级队列,使用的数据结构是小顶堆(保证堆顶的元素最小) 堆的性质并不保证有序,只是保证能在O(1)找到最小值(小顶堆)或者最大值(大顶堆) 堆本质上是一个完全二叉树,保证了根节点总是比叶子节点大或者小。因为这个优先级队列主要是由来存放TimerTask的,所以它使用的是一个TimerTask的数组来实现的。 import java.util.Arrays; import java.util.TimerTask; class TaskQueue { /** * 使用数组存为,数据是TimerTask类型 */ private TimerTask[] queue = new TimerTask[128]; /** * 任务队列的大小,queue[0]没有使用,queue[1]-queue[size]是排好序的 */ private int size = 0; int size() { return size; } /** * */ TimerTask getMin() { return queue[1]; } TimerTask get(int i) { return queue

Timer and TimerTask Java

让人想犯罪 __ 提交于 2020-01-06 15:38:05
问题 I want my program to print Hello User, a ten seconds later print Ten Seconds Have Gone By, finally, a few seconds later print Goodbye User. I just don't know what or how to start to do this. Thanks Here's the code: import java.util.Timer; import java.util.TimerTask; public class S1p4 { public static void main(String[] args) { Timer timer = new Timer(); Task task = new Task(); timer.schedule(task, 1000, 1000); } } class Task extends TimerTask { int i=1; @Override public void run() { i++;

Which objects do I attach the Timer class event listener to, in Java?

给你一囗甜甜゛ 提交于 2020-01-06 09:06:34
问题 I am trying to create a whack a mole game. I have used swing to create background and add mole images with event listeners which increment a score each time they are clicked, but I am having problems setting whether they should be visible or not. I thought the best way to do this would be to use a timer to set/reset a boolean (vis). Randomizing the period for which the images are visible would be ideal. I have tried using a swing timer several times but doesn't seem to be working. Where do I

Java - Combine Observer pattern with timer task?

不羁岁月 提交于 2020-01-04 08:06:07
问题 In a previous post, I used the observer pattern. Description - class Flight has a status (ie int) - before time, on time, late. This is my Observable class FlightStatusMonitor has an ArrayList of Flights. This class is my observer. There is only one such observer. The update(Observable o, Object arg) method will update the status of the flight and also display the refreshed flight status of all flights that it observes. I was thinking of using timer tasks to change the status of flights at