Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running code on the main thread from a secondary thread?

This is a general Java question and not an Android one first off!

I'd like to know how to run code on the main thread, from the context of a secondary thread. For example:

new Thread(new Runnable() {         public void run() {             //work out pi to 1,000 DP (takes a while!)              //print the result on the main thread         }     }).start(); 

That sort of thing - I realise my example is a little poor since in Java you don't need to be in the main thread to print something out, and that Swing has an event queue also - but the generic situation where you might need to run say a Runnable on the main thread while in the context of a background thread.

EDIT: For comparison - here's how I'd do it in Objective-C:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0UL), ^{     //do background thread stuff      dispatch_async(dispatch_get_main_queue(), ^{         //update UI     }); }); 

Thanks in advance!

like image 890
Javawag Avatar asked Apr 28 '13 22:04

Javawag


People also ask

How Stop main thread while another thread is running?

You can not stop the main thread while any other thread are running. (All the child threads born out of main thread.) You can use function Thread. join() to keep the main thread waiting while other thread(s) execute.

How do I run a thread from one after another?

We can use use join() method of thread class. To ensure three threads execute you need to start the last one first e.g. T3 and then call join methods in reverse order e.g. T3 calls T2. join, and T2 calls T1.

Why should you avoid to run non UI code on the main thread?

All Android apps use a main thread to handle UI operations. Calling long-running operations from this main thread can lead to freezes and unresponsiveness. For example, if your app makes a network request from the main thread, your app's UI is frozen until it receives the network response.

How do I run something on the main thread?

A condensed code block is as follows: new Handler(Looper. getMainLooper()). post(new Runnable() { @Override public void run() { // things to do on the main thread } });


2 Answers

There is no universal way to just send some code to another running thread and say "Hey, you, do this." You would need to put the main thread into a state where it has a mechanism for receiving work and is waiting for work to do.

Here's a simple example of setting up the main thread to wait to receive work from other threads and run it as it arrives. Obviously you would want to add a way to actually end the program and so forth...!

public static final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();  public static void main(String[] args) throws Exception {     new Thread(new Runnable(){         @Override         public void run() {             final int result;             result = 2+3;             queue.add(new Runnable(){                 @Override                 public void run() {                     System.out.println(result);                 }             });         }     }).start();      while(true) {         queue.take().run();     } } 
like image 165
Affe Avatar answered Oct 06 '22 00:10

Affe


In case you are on Android, using a Handler should do the job?

new Handler(Looper.getMainLooper()).post(new Runnable () {     @Override     public void run () {         ...     } }); 
like image 45
HarryS Avatar answered Oct 06 '22 00:10

HarryS