Example usage for android.os HandlerThread getThreadId

List of usage examples for android.os HandlerThread getThreadId

Introduction

In this page you can find the example usage for android.os HandlerThread getThreadId.

Prototype

public int getThreadId() 

Source Link

Document

Returns the identifier of this thread.

Usage

From source file:com.amazonaws.mobileconnectors.iot.AWSIotMqttManager.java

/**
 * Schedule an auto-reconnect attempt using backoff logic.
 *
 * @return true if attempt was scheduled, false otherwise.
 *//*from   w  w w .  j  a  v  a 2 s  .  com*/
private boolean scheduleReconnect() {
    LOGGER.info("schedule Reconnect attempt " + autoReconnectsAttempted + " of " + maxAutoReconnectAttempts
            + " in " + currentReconnectRetryTime + " seconds.");
    // schedule a reconnect if unlimited or if we haven't yet hit the limit

    if (maxAutoReconnectAttempts == -1 || autoReconnectsAttempted < maxAutoReconnectAttempts) {
        //Start a separate thread to do reconnect, because connection must not occur on the main thread.
        final HandlerThread ht = new HandlerThread("Reconnect thread");
        ht.start();
        Looper looper = ht.getLooper();
        Handler handler = new Handler(looper);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                LOGGER.debug("TID: " + ht.getThreadId() + " trying to reconnect to session");
                if (mqttClient != null && !mqttClient.isConnected()) {
                    reconnectToSession();
                }
            }
        }, MILLIS_IN_ONE_SECOND * currentReconnectRetryTime);
        currentReconnectRetryTime = Math.min(currentReconnectRetryTime * 2, maxReconnectRetryTime);
        return true;
    } else {
        LOGGER.warn("schedule reconnect returns false");
        return false;
    }
}