java - Lancer les messages de Handler dans un fil de fond

Mots clés : javaandroidmultithreadinghandlerjava

meilleur 4 Réponses java - Lancer les messages de Handler dans un fil de fond

vote vote

91

private Handler mHandler;  private HandlerThread mHandlerThread;  public void startHandlerThread(){     mHandlerThread = new HandlerThread("HandlerThread");     mHandlerThread.start();     mHandler = new Handler(mHandlerThread.getLooper()); } 
mHandler.postDelayed(new Runnable() {         @Override         public void run() {           // Your task goes here         }     },1000); 
vote vote

82

    private void createHandler() {         Thread thread = new Thread() {             public void run() {                 Looper.prepare();                  final Handler handler = new Handler();                 handler.postDelayed(new Runnable() {                     @Override                     public void run() {                        // Do Work                         handler.removeCallbacks(this);                         Looper.myLooper().quit();                    }                 }, 2000);                  Looper.loop();             }         };         thread.start();     } 
vote vote

78

public class TwoThreads {   public static void main(String args[]) throws InterruptedException {     System.out.println("TwoThreads:Test");     new TwoThreads().test();   }   // The end of the list.   private static final Integer End = -1;    static class Producer implements Runnable {     final Queue<Integer> queue;      public Producer(Queue<Integer> queue) {       this.queue = queue;     }      @Override     public void run() {       try {         for (int i = 0; i < 1000; i++) {           queue.add(i);           Thread.sleep(1);         }         // Finish the queue.         queue.add(End);       } catch (InterruptedException ex) {         // Just exit.       }     }   }    static class Consumer implements Runnable {     final Queue<Integer> queue;      public Consumer(Queue<Integer> queue) {       this.queue = queue;     }      @Override     public void run() {       boolean ended = false;       while (!ended) {         Integer i = queue.poll();         if (i != null) {           ended = i == End;           System.out.println(i);         }       }     }   }    public void test() throws InterruptedException {     Queue<Integer> queue = new LinkedBlockingQueue<>();     Thread pt = new Thread(new Producer(queue));     Thread ct = new Thread(new Consumer(queue));     // Start it all going.     pt.start();     ct.start();     // Wait for it to finish.     pt.join();     ct.join();   } } 
vote vote

64

fun background(function: () -> Unit) = handler.post(function)  private val handler: Handler by lazy { Handler(handlerThread.looper) }  private val handlerThread: HandlerThread by lazy {     HandlerThread("RenetikBackgroundThread").apply {         setUncaughtExceptionHandler { _, e -> later { throw RuntimeException(e) } }         start()     } } 
 background {     some task to do in background...    } 
 background {     other task to do in background...       later {         on main thread when all tasks are finished...     }  } 

Questions similaires