Let ScheduledThreadPoolExecutor execute tasks on UI thread - java

How can I use a ScheduledThreadPoolExecutor and schedule a task which is always executed on the UI thread?
Currently, I do the following
mScheduledTask = sBackgroundExecutor.scheduleAtFixedRate(new Runnable() {
#Override
public void run() {
if (mActivity != null) {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
// do UI stuff
}
});
}
}
}, 0, 200, TimeUnit.MILLISECONDS);
This seems unnecessary and hard to read, but I couldn't find anything in the docs. Is it possible or is this the only way?

Related

JavaFX: Task will not update UI

I'm working on an Omaha online poker client written in javaFX+java.
I need to show an anchorPane containing 3 buttons after control.call() finishes executing. I know for a fact that control.call() finishes executing but for some reason task.setOnSucceeded(new EventHandler() {'s handle method does not update the User interface.
What am I doing wrong?
public void newRound() {
sog = StateOfGame.PREFLOP;
ControlGameOnNewThread control = new ControlGameOnNewThread();
Task task = new Task() {
#Override
protected Object call() throws Exception {
control.call();
return null;
}
};
task.setOnSucceeded(new EventHandler() {
#Override
public void handle(Event event) {
if (client.getAction() == FirstToAct.me) {
System.out.println("Task finsished");
showOptions(client.getToCall());
opponentBetField.setText(new Integer(opp.chipsInvested).toString());
myBetField.setText(new Integer(client.chipsInvested).toString());
System.out.println("Task finsished");
}
}
});
new Thread(task).start();
}
The problem is that you are updating the user interface in the other thread.. if you are in the other thread and you want to update the user interface
You need to call the
Platform.runLater(new Runnable() {
#Override public void run() {
//Update UI here
}
});
calling this will call the main thread and update all the necessary update to the user interface
EDIT
public void newRound() {
sog = StateOfGame.PREFLOP;
ControlGameOnNewThread control = new ControlGameOnNewThread();
Task task = new Task() {
#Override
protected Object call() throws Exception {
control.call();
return null;
}
};
task.setOnSucceeded(new EventHandler() {
#Override
public void handle(Event event) {
if (client.getAction() == FirstToAct.me) {
Platform.runLater(new Runnable() {
#Override public void run() {
System.out.println("Task finsished");
showOptions(client.getToCall());
opponentBetField.setText(new Integer(opp.chipsInvested).toString());
myBetField.setText(new Integer(client.chipsInvested).toString());
System.out.println("Task finsished");
}
});
}
}
});
new Thread(task).start();
}
As you override the call() method, you can override the succeeded() method: (See last example in the javadoc of Task.)
#Override protected void succeeded() {
super.succeeded();
updateMessage("Done!");
}
succeeded() is already called on the JavaFX Application Thread, so you do not need to do this for yourself.
Are you sure, that the code in your handle method isn't called? Maybe a NullPointerException is thrown, which you might not see? Is the comparison working as expected?
Try moving the code with if (client.getAction()... into the overridden succeeded() method and put a println before the if-statement in order to see whether it is called or not.
(EDIT: typos)
Your information has been helpful, but i think all methods suggested would have worked (including my initial one). The problem is that the task was never ended that's why the onTaskSucceded was never executed. In order to have the task exit after it has finished, i had to se the daemon to true:
Thread thread = new Thread(task);
thread.setDaemon(true);
thread.start();

Execute method every second

I want to execude some code every second in android, but I'd like to do is in one thread (main thread). So far I have this:
locationTimer = new Timer("locationTimer", false);
locationTimer.schedule(new LocationCheckerTask(this), 0, 1000);
public class LocationCheckerTask extends TimerTask {
private GeoWatcher watcher;
public LocationCheckerTask(Context context) {
watcher = new GeoWatcher(context);
}
#Override
public void run() {
// funky stuff
}
}
Unfortunately, Timer class runs it's tasks on another thread.
Why I want to do this in a single thread?
Code in run() method will be executing really fast, so I figured I don't need another thread for it. What I want to do is to construct separate threads in run() method based on condition calculated every second. So instead of having child thread constructing another threads, I'd like to do this on the main one.
You can do this with Handler
public class Job implements Runnable{
private Handler handler;
public Job () {
handler = new Handler(Looper.getMainLooper());
loop();
}
#Override
public void run() {
// funky stuff
loop();
}
private void loop() {
handler.postDelayed(this, 1000);
}
}
use runOnUiThread(Runnable) method of Activity to run the task in UI Thread
public class LocationCheckerTask extends TimerTask {
private GeoWatcher watcher;
public LocationCheckerTask(Context context) {
watcher = new GeoWatcher(context);
}
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
// funky stuff
}
});
}
}
the Handler is a perfect candidate for such tasks (dont try to combine TimerTask + runOnUiThread - it is useless as it uses a Handler under the hood)
private Runnable fiveSecondRunnable = new Runnable() {
#Override
public void run() {
if (count5 < 0) {
switchT030Sec();
} else {
tvSec5.setText(""+count5);
Log.v("5sec set", "yes");
count5--;
man.postDelayed(this, 1000);
}
}
};
and start it by calling
man.post(fiveSecondRunnable);

How to start a thread after specified time delay in java

I have called a method in ServletContextListener as thread ..Now as per my need i have to delay the thread for 1 minutes and then start executing the method called in the thread but i am not able to do that as i am very new in this...
Here is my code ...
public class Startup implements ServletContextListener {
#Override
public void contextDestroyed(ServletContextEvent sce) {
}
public void contextInitialized(ServletContextEvent sce) {
// Do your startup work here
System.out.println("Started....");
//captureCDRProcess();
new Thread(new Runnable() {
#Override
public void run() {
captureCDRProcess();
}
}).start();
}
Please help me ..
Thanks in advance..
To do this properly, you need to use a ScheduledThreadPoolExecutor and use the function schedule like this:
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(NUM_THREADS);
executor.schedule(new Runnable() {
#Override
public void run() {
captureCDRProcess();
}
}, 1, TimeUnit.MINUTES);
Thread.sleep is not the way to go, because it does not guarantee that it wakes up after a minute. Depending on the OS and the background tasks, it could be 60 seconds, 62 seconds or 3 hours, while the scheduler above actually uses the correct OS implementation for scheduling and is therefore much more accurate.
In addition this scheduler allows several other flexible ways to schedule tasks like at a fixed rate or fixed delay.
Edit: Same solution using the new Java8 Lamda syntax:
final ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(NUM_THREADS);
executor.schedule(() -> captureCDRProcess(), 1, TimeUnit.MINUTES);
Or you can delay creating the thread with Timer and TimerTask:
public void contextInitialized() {
// Do your startup work here
System.out.println("Started....");
Timer timer = new Timer();
TimerTask delayedThreadStartTask = new TimerTask() {
#Override
public void run() {
//captureCDRProcess();
//moved to TimerTask
new Thread(new Runnable() {
#Override
public void run() {
captureCDRProcess();
}
}).start();
}
};
timer.schedule(delayedThreadStartTask, 60 * 1000); //1 minute
}
Have a look at Thread.sleep(). Maybe add it to the new thread's run method, so that it sleeps the needed time before doing any meaningful work.
You can start thread and inside the thread use sleep method for one minute.
ScheduledThreadPoolExecutor has this ability, but it's quite heavyweight.
Here's a simple implementation with a test (signature close to Android's Handler.postDelayed()):
public class JavaUtil {
public static void postDelayed(final Runnable runnable, final long delayMillis) {
final long requested = System.currentTimeMillis();
new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
long leftToSleep = requested + delayMillis - System.currentTimeMillis();
if (leftToSleep > 0) {
Thread.sleep(leftToSleep);
}
break;
} catch (InterruptedException ignored) {
}
}
runnable.run();
}
}).start();
}
}
Test:
#Test
public void testRunsOnlyOnce() throws InterruptedException {
long delay = 100;
int num = 0;
final AtomicInteger numAtomic = new AtomicInteger(num);
JavaUtil.postDelayed(new Runnable() {
#Override
public void run() {
numAtomic.incrementAndGet();
}
}, delay);
Assert.assertEquals(num, numAtomic.get());
Thread.sleep(delay + 10);
Assert.assertEquals(num + 1, numAtomic.get());
Thread.sleep(delay * 2);
Assert.assertEquals(num + 1, numAtomic.get());
}

Block EDT till executorservice completes all task

In my application i have two jtextpanes and i have a executorservice instacne which contains few task. I want to assure that all the task in the executorservice instance are completed before executing the FocusListener focusGained method of any jtextpanes. Also i add some more task to the executorservice when focusLost method is called.
Code
top.addFocusListener(new FocusListener()
{
public void focusLost(FocusEvent e)
{
if (ecaViewControl.isInitialized())
{
stopRecording();
executor.execute(new Runnable()
{
public void run()
{
ecaViewControl.save(top);
}
});
executor.execute(new Runnable()
{
public void run()
{
ecaViewControl.closeDocument();
}
});
}
}
public void focusGained(FocusEvent e)
{
//Need to have completed all the task in executor service before EDT executes the code in here
});
}
if you do know the number of tasks to be finished, consider using of CountDownLatch
so your code might look like:
public void run() {
try {
// do something here
} finally {
latch.countDown()
}
}
then in the code you're waiting for tasks to complete - simply wait for latch:
latch.await();
or, instead use CompletionService if you may live without executor.

Is this how to schedule a java method to run 1 second later?

In my method, I want to call another method that will run 1 second later. This is what I have.
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
MyMethod();
Log.w("General", "This has been called one second later");
timer.cancel();
}
}, 1000);
Is this how it's supposed to be done?
Are there other ways to do it since I'm on Android?
Can it be repeated without any problems?
There are several alternatives. But here is Android specific one.
If you thread is using Looper (and Normally all Activity's, BroadcastRecevier's and Service's methods onCreate, onReceive, onDestroy, etc. are called from such a thread), then you can use Handler. Here is an example:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
myMethod();
}
}, 1000);
Note that you do not have to cancel anything here. This will be run only once on the same thread your Handler was created.
Instead of a Timer, I'd recommend using a ScheduledExecutorService
final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
exec.schedule(new Runnable(){
#Override
public void run(){
MyMethod();
}
}, 1, TimeUnit.SECONDS);
If you are not in UI thread, consider adding a very simple:
try
{
Thread.sleep( 1000 );
}//try
catch( Exception ex)
{ ex.printStackTrace(); }//catch
//call your method
ScheduledExecutorService or AsyncTask for UI related.
Note that if you are to update UI, that code should be posted to UI thread. as in Processes and Threads Guide
final Bitmap bitmap = loadImageFromNetwork("http://example.com/image.png");
mImageView.post(new Runnable() {
public void run() {
mImageView.setImageBitmap(bitmap);
}
});
There is also nice postDelayed method in View
mImageView.postDelayed(new Runnable(){
#Override
public void run() {
mImageView.setImageResource(R.drawable.ic_inactive);
}
}, 1000);
that will update UI after 1 sec.

Categories

Resources