Class Looper

    • Field Detail

      • lock

        protected final Lock lock
        The thread holds this lock whenever it is running iterate().
      • threadLock

        protected final Lock threadLock
        The looper holds this lock whenever it is reading or writing the state of the underlying thread (including replacing that thread).
      • thread

        protected transient volatile Thread thread
        The thread where this looper's loop is running.
    • Constructor Detail

      • Looper

        protected Looper()
        Constructs a Looper with all properties as defaults. The thread is not started in the constructor, because subclass fields won't have been initialized.
      • Looper

        protected Looper​(String name)
        Constructs a Looper with a thread name. The thread is not started in the constructor, because subclass fields won't have been initialized.
        Parameters:
        name - the name of the thread to create
      • Looper

        protected Looper​(ThreadFactory factory)
        Constructs a Looper with a given thread factory. The thread is not started in the constructor, because subclass fields won't have been initialized.
        Parameters:
        factory - the thread factory that will create this instance's thread
    • Method Detail

      • isRunning

        public boolean isRunning()
        Returns whether there is a running thread executing this Looper's loop.
        Returns:
        true if this has a running thread; false otherwise
      • initTransientFields

        protected void initTransientFields()
        Subclasses should override this if they have transient fields to set up before starting.
      • iterate

        protected abstract boolean iterate()
                                    throws InterruptedException
        The task that will be iterated until it returns false. Cannot be abstract for serialization reasons, but must be overridden in subclasses if they are instantiated without a target Runnable.
        Returns:
        true if this thread should iterate again.
        Throws:
        InterruptedException - if interrupted in mid-execution.
      • start

        protected void start()
        Starts the thread if it's not already running, creating it if it doesn't exist, has died or has been interrupt()ed.
      • interrupt

        public void interrupt()
        Interrupts the thread if it's running. The thread will be replaced by a new one the next time start() is called.