Code example for SynchronousQueue

Methods: take

0
final class GeckoBackgroundThread extends Thread {
    private static final String LOOPER_NAME = "GeckoBackgroundThread";
 
    // Guarded by 'this'. 
    private static Handler sHandler = null;
    private SynchronousQueue<Handler> mHandlerQueue = new SynchronousQueue<Handler>();
 
    // Singleton, so private constructor. 
    private GeckoBackgroundThread() { 
        super(); 
    } 
 
    @Override 
    public void run() { 
        setName(LOOPER_NAME);
        Looper.prepare();
        try { 
            mHandlerQueue.put(new Handler());
        } catch (InterruptedException ie) {}
 
        Looper.loop();
    } 
 
    // Get a Handler for a looper thread, or create one if it doesn't yet exist. 
    /*package*/ static synchronized Handler getHandler() {
        if (sHandler == null) {
            GeckoBackgroundThread lt = new GeckoBackgroundThread();
            ThreadUtils.setBackgroundThread(lt);
            lt.start();
            try { 
                sHandler = lt.mHandlerQueue.take();
            } catch (InterruptedException ie) {}
        } 
        return sHandler;