Example usage for java.lang Thread setDaemon

List of usage examples for java.lang Thread setDaemon

Introduction

In this page you can find the example usage for java.lang Thread setDaemon.

Prototype

public final void setDaemon(boolean on) 

Source Link

Document

Marks this thread as either a #isDaemon daemon thread or a user thread.

Usage

From source file:com.streamsets.pipeline.stage.origin.hdfs.cluster.ClusterHDFSSourceIT.java

private Thread createThreadForAddingBatch(final SourceRunner sourceRunner, final List<Map.Entry> list) {
    Thread sourceThread = new Thread() {
        @Override/*from  www  .ja  v  a 2s  .  c o m*/
        public void run() {
            try {
                ClusterHdfsSource source = (ClusterHdfsSource) sourceRunner.getStage();
                source.put(list);
            } catch (Exception ex) {
                LOG.error("Error in waiter thread: " + ex, ex);
            }
        }
    };
    sourceThread.setName(getClass().getName() + "-sourceThread");
    sourceThread.setDaemon(true);
    sourceThread.start();
    return sourceThread;
}

From source file:com.mobicage.rogerthat.registration.YSAAARegistrationActivity.java

private void tryConnect(final int attempt, final String statusMessage, final RegistrationInfo info) {
    T.UI();/*from   w w w .j  a va  2s.c o  m*/
    final Pausable pausable = this;

    if (attempt > XMPP_MAX_NUM_ATTEMPTS) {
        mTimer.cancel();
        mProgressBar.setVisibility(View.GONE);
        mStatusLbl.setText(R.string.registration_error);
        mRetryBtn.setVisibility(View.VISIBLE);

        new AlertDialog.Builder(YSAAARegistrationActivity.this)
                .setMessage(getString(R.string.registration_error)).setCancelable(true)
                .setPositiveButton(R.string.rogerthat, null).create().show();
        mWiz.reInit();
        return;
    }
    L.d("Registration attempt #" + attempt);

    final String xmppServiceName = info.mCredentials.getXmppServiceName();
    final String xmppAccount = info.mCredentials.getXmppAccount();
    final String xmppPassword = info.mCredentials.getPassword();

    final ConfigurationProvider cp = mService.getConfigurationProvider();
    Runnable runnable = new SafeRunnable() {

        @Override
        public void safeRun() {
            T.REGISTRATION();
            try {
                if (CloudConstants.USE_XMPP_KICK_CHANNEL) {
                    final ConnectionConfiguration xmppConfig = new XMPPConfigurationFactory(cp,
                            mService.getNetworkConnectivityManager(), null)
                                    .getSafeXmppConnectionConfiguration(xmppServiceName);
                    final XMPPConnection xmppCon = new XMPPConnection(xmppConfig);

                    xmppCon.setLogger(new Logger() {
                        @Override
                        public void log(String message) {
                            L.d(message);
                        }
                    });
                    xmppCon.connect();
                    xmppCon.login(xmppAccount, xmppPassword);

                    final Thread t2 = new Thread(new SafeRunnable() {
                        @Override
                        protected void safeRun() throws Exception {
                            L.d("REG Before disconnect (on separate thread) - xmpp-" + xmppCon.hashCode());
                            xmppCon.disconnect();
                            L.d("REG After disconnect (on separate thread) - xmpp-" + xmppCon.hashCode());
                        }
                    });
                    t2.setDaemon(true);
                    t2.start();
                }

                postFinishRegistration(info.mCredentials.getUsername(), info.mCredentials.getPassword(),
                        CloudConstants.APP_SERVICE_GUID, null);

                mUIHandler.post(new SafeRunnable(pausable) {

                    @Override
                    protected void safeRun() throws Exception {
                        T.UI();
                        mWiz.setCredentials(info.mCredentials);

                        if (CloudConstants.USE_GCM_KICK_CHANNEL && !"".equals(mGCMRegistrationId)) {
                            GoogleServicesUtils.saveGCMRegistrationId(mService, mGCMRegistrationId);
                        }

                        mService.setCredentials(mWiz.getCredentials());
                        mService.setRegisteredInConfig(true);
                        final Intent launchServiceIntent = new Intent(YSAAARegistrationActivity.this,
                                MainService.class);
                        launchServiceIntent.putExtra(MainService.START_INTENT_JUST_REGISTERED, true);
                        launchServiceIntent.putExtra(MainService.START_INTENT_MY_EMAIL, mWiz.getEmail());
                        YSAAARegistrationActivity.this.startService(launchServiceIntent);

                        startCheckingIfAppReady(pausable);
                    }
                });

            } catch (Exception e) {
                L.d("Exception while trying to end the registration process", e);
                mUIHandler.post(new SafeRunnable(pausable) {

                    @Override
                    protected void safeRun() throws Exception {
                        T.UI();
                        tryConnect(attempt + 1, statusMessage, info);
                    }
                });
            }
        }
    };
    if (attempt == 1) {
        mWorkerHandler.post(runnable);
    } else {
        mWorkerHandler.postDelayed(runnable, XMPP_CHECK_DELAY_MILLIS);
    }
}

From source file:com.mobicage.rogerthat.registration.ContentBrandingRegistrationActivity.java

private void tryConnect(final int attempt, final String statusMessage, final RegistrationInfo info) {
    T.UI();/*from  www  .  j av a2s .  c  o m*/
    final Pausable pausable = this;

    if (attempt > XMPP_MAX_NUM_ATTEMPTS) {
        mProgressContainer.setVisibility(View.GONE);
        mButtonContainer.setVisibility(View.VISIBLE);
        new AlertDialog.Builder(ContentBrandingRegistrationActivity.this)
                .setMessage(getString(R.string.registration_error)).setCancelable(true)
                .setPositiveButton(R.string.try_again, null).create().show();
        mWiz.reInit();
        return;
    }
    mProgressContainer.setVisibility(View.VISIBLE);
    mButtonContainer.setVisibility(View.GONE);
    L.d("Registration attempt #" + attempt);

    final String xmppServiceName = info.mCredentials.getXmppServiceName();
    final String xmppAccount = info.mCredentials.getXmppAccount();
    final String xmppPassword = info.mCredentials.getPassword();

    final ConfigurationProvider cp = mService.getConfigurationProvider();
    Runnable runnable = new SafeRunnable() {

        @Override
        public void safeRun() {
            T.REGISTRATION();
            try {

                if (CloudConstants.USE_XMPP_KICK_CHANNEL) {
                    final ConnectionConfiguration xmppConfig = new XMPPConfigurationFactory(cp,
                            mService.getNetworkConnectivityManager(), null)
                                    .getSafeXmppConnectionConfiguration(xmppServiceName);
                    final XMPPConnection xmppCon = new XMPPConnection(xmppConfig);

                    xmppCon.setLogger(new Logger() {
                        @Override
                        public void log(String message) {
                            L.d(message);
                        }
                    });
                    xmppCon.connect();
                    xmppCon.login(xmppAccount, xmppPassword);

                    final Thread t2 = new Thread(new SafeRunnable() {
                        @Override
                        protected void safeRun() throws Exception {
                            L.d("REG Before disconnect (on separate thread) - xmpp-" + xmppCon.hashCode());
                            xmppCon.disconnect();
                            L.d("REG After disconnect (on separate thread) - xmpp-" + xmppCon.hashCode());
                        }
                    });
                    t2.setDaemon(true);
                    t2.start();

                }

                postFinishRegistration(info.mCredentials.getUsername(), info.mCredentials.getPassword(), null,
                        null);

                mUIHandler.post(new SafeRunnable(pausable) {

                    @Override
                    protected void safeRun() throws Exception {
                        T.UI();
                        mWiz.setCredentials(info.mCredentials);

                        if (CloudConstants.USE_GCM_KICK_CHANNEL && !"".equals(mGCMRegistrationId)) {
                            GoogleServicesUtils.saveGCMRegistrationId(mService, mGCMRegistrationId);
                        }

                        mService.setCredentials(mWiz.getCredentials());
                        mService.setRegisteredInConfig(true);
                        final Intent launchServiceIntent = new Intent(ContentBrandingRegistrationActivity.this,
                                MainService.class);
                        launchServiceIntent.putExtra(MainService.START_INTENT_JUST_REGISTERED, true);
                        launchServiceIntent.putExtra(MainService.START_INTENT_MY_EMAIL, mWiz.getEmail());
                        ContentBrandingRegistrationActivity.this.startService(launchServiceIntent);

                        Intent intent = new Intent(ContentBrandingRegistrationActivity.this,
                                ContentBrandingMainActivity.class);
                        intent.setAction(MainActivity.ACTION_REGISTERED);
                        intent.setFlags(MainActivity.FLAG_CLEAR_STACK);
                        startActivity(intent);
                        ContentBrandingRegistrationActivity.this.finish();
                    }
                });

            } catch (Exception e) {
                L.d("Exception while trying to end the registration process", e);
                mUIHandler.post(new SafeRunnable(pausable) {

                    @Override
                    protected void safeRun() throws Exception {
                        T.UI();
                        tryConnect(attempt + 1, statusMessage, info);
                    }
                });
            }
        }
    };
    if (attempt == 1) {
        mWorkerHandler.post(runnable);
    } else {
        mWorkerHandler.postDelayed(runnable, XMPP_CHECK_DELAY_MILLIS);
    }
}

From source file:com.xabber.android.data.connection.ConnectionThread.java

public ConnectionThread(final ConnectionItem connectionItem) {
    LogManager.i(this, "NEW connection thread " + connectionItem.getRealJid());

    this.connectionItem = connectionItem;
    executorService = Executors.newSingleThreadExecutor(new ThreadFactory() {
        @Override/*from   w  w  w.  java  2 s  .c om*/
        public Thread newThread(Runnable runnable) {
            Thread thread = new Thread(runnable,
                    "Connection thread for " + (connectionItem instanceof AccountItem
                            ? ((AccountItem) connectionItem).getAccount()
                            : connectionItem));
            thread.setPriority(Thread.MIN_PRIORITY);
            thread.setDaemon(true);
            return thread;
        }
    });
    ConnectionManager.getInstance().onConnection(this);
    ConnectionSettings connectionSettings = connectionItem.getConnectionSettings();
    protocol = connectionSettings.getProtocol();
    serverName = connectionSettings.getServerName();
    token = connectionSettings.getPassword();
    resource = connectionSettings.getResource();
    saslEnabled = connectionSettings.isSaslEnabled();
    tlsMode = connectionSettings.getTlsMode();
    compression = connectionSettings.useCompression();
    if (saslEnabled && protocol == AccountProtocol.gtalk)
        login = connectionSettings.getUserName() + "@" + connectionSettings.getServerName();
    else
        login = connectionSettings.getUserName();
    proxyType = connectionSettings.getProxyType();
    proxyHost = connectionSettings.getProxyHost();
    proxyPort = connectionSettings.getProxyPort();
    proxyUser = connectionSettings.getProxyUser();
    proxyPassword = connectionSettings.getProxyPassword();
    started = false;
}

From source file:com.mobicage.rogerthat.registration.AbstractRegistrationActivity.java

public void tryConnect(final ProgressDialog pd, final int attempt, final String statusMessage,
        final RegistrationInfo info) {
    T.UI();//  w ww .  j  a v a 2s.  c om
    final Pausable pausable = this;

    if (attempt > XMPP_MAX_NUM_ATTEMPTS) {
        pd.dismiss();
        String message = getString(R.string.registration_error);
        UIUtils.showDialog(mActivity, null, message, getString(R.string.try_again), null, null, null);
        mWizard.reInit();
        mWizard.goBackToPrevious();
        return;
    }
    pd.setMessage(statusMessage + attempt);
    if (!pd.isShowing())
        pd.show();
    L.d("Registration attempt #" + attempt);

    final String xmppServiceName = info.mCredentials.getXmppServiceName();
    final String xmppAccount = info.mCredentials.getXmppAccount();
    final String xmppPassword = info.mCredentials.getPassword();

    final ConfigurationProvider cp = mService.getConfigurationProvider();
    Configuration cfg = cp.getConfiguration(RegistrationWizard2.CONFIGKEY);
    final String invitorCode = (cfg == null) ? null : cfg.get(INVITOR_CODE_CONFIGKEY, null);
    final String invitorSecret = (cfg == null) ? null : cfg.get(INVITOR_SECRET_CONFIGKEY, null);

    Runnable runnable = new SafeRunnable() {

        @Override
        public void safeRun() {
            T.REGISTRATION();
            try {

                if (CloudConstants.USE_XMPP_KICK_CHANNEL) {

                    final ConnectionConfiguration xmppConfig = new XMPPConfigurationFactory(cp,
                            mService.getNetworkConnectivityManager(), null)
                                    .getSafeXmppConnectionConfiguration(xmppServiceName);
                    final XMPPConnection xmppCon = new XMPPConnection(xmppConfig);

                    xmppCon.setLogger(new Logger() {
                        @Override
                        public void log(String message) {
                            L.d(message);
                        }
                    });
                    xmppCon.connect();
                    xmppCon.login(xmppAccount, xmppPassword);

                    final Thread t2 = new Thread(new SafeRunnable() {
                        @Override
                        protected void safeRun() throws Exception {
                            L.d("REG Before disconnect (on separate thread) - xmpp-" + xmppCon.hashCode());
                            xmppCon.disconnect();
                            L.d("REG After disconnect (on separate thread) - xmpp-" + xmppCon.hashCode());
                        }
                    });
                    t2.setDaemon(true);
                    t2.start();

                }

                postFinishRegistration(info.mCredentials.getUsername(), info.mCredentials.getPassword(),
                        invitorCode, invitorSecret);

                runOnUI(new SafeRunnable(pausable) {

                    @Override
                    protected void safeRun() throws Exception {
                        T.UI();
                        mWizard.setCredentials(info.mCredentials);

                        if (CloudConstants.USE_GCM_KICK_CHANNEL && !"".equals(mGCMRegistrationId)) {
                            GoogleServicesUtils.saveGCMRegistrationId(mService, mGCMRegistrationId);
                        }

                        mService.setCredentials(mWizard.getCredentials());
                        mService.setRegisteredInConfig(true);

                        final Intent launchServiceIntent = new Intent(mActivity, MainService.class);
                        launchServiceIntent.putExtra(MainService.START_INTENT_JUST_REGISTERED, true);
                        launchServiceIntent.putExtra(MainService.START_INTENT_MY_EMAIL, mWizard.getEmail());
                        mActivity.startService(launchServiceIntent);
                        stopRegistrationService();
                        pd.dismiss();

                        mWizard.finish(); // finish
                    }
                });

            } catch (Exception e) {
                L.d("Exception while trying to end the registration process", e);
                runOnUI(new SafeRunnable(pausable) {

                    @Override
                    protected void safeRun() throws Exception {
                        T.UI();
                        tryConnect(pd, attempt + 1, statusMessage, info);
                    }
                });
            }
        }
    };
    if (attempt == 1) {
        runOnWorker(runnable);
    } else {
        runDelayedOnWorker(runnable, XMPP_CHECK_DELAY_MILLIS);
    }
}

From source file:com.bt.download.android.gui.Librarian.java

public void syncMediaStore() {
    if (!isExternalStorageMounted()) {
        return;/*  w  w w. java 2 s  . c o  m*/
    }

    Thread t = new Thread(new Runnable() {
        public void run() {
            syncMediaStoreSupport();
        }
    });
    t.setName("syncMediaStore");
    t.setDaemon(true);
    t.start();
}

From source file:com.bt.download.android.gui.Librarian.java

public void syncApplicationsProvider() {
    if (!isExternalStorageMounted()) {
        return;/*from  w  w w . jav  a  2  s .  c  om*/
    }

    Thread t = new Thread(new Runnable() {
        public void run() {
            syncApplicationsProviderSupport();
        }
    });
    t.setName("syncApplicationsProvider");
    t.setDaemon(true);
    t.start();
}

From source file:strat.mining.stratum.proxy.Launcher.java

/**
 * Initialize the shutdown hook to close gracefully all connections.
 *//*  w  w  w  .j av  a 2 s  .  c  om*/
private static void initShutdownHook() {
    Thread hookThread = new Thread() {
        public void run() {

            // Shutdown the database
            DatabaseManager.close();

            // Start a timer task that will exit the program after 1 second
            // if the cleanup is not over.
            Timer.getInstance().schedule(new Timer.Task() {
                public void run() {
                    if (LOGGER != null) {
                        LOGGER.error("Force killing of the proxy...");
                    } else {
                        System.err.println("Force killing of the proxy...");
                    }
                    Runtime.getRuntime().halt(0);
                }
            }, 1000);

            if (ProxyManager.getInstance() != null) {
                if (LOGGER != null) {
                    LOGGER.info("User requested shutdown... Gracefuly kill all connections...");
                } else {
                    System.out.println("User requested shutdown... Gracefuly kill all connections...");
                }
                ProxyManager.getInstance().stopListeningIncomingConnections();
                ProxyManager.getInstance().closeAllWorkerConnections();
                ProxyManager.getInstance().stopPools();
            }
            if (apiHttpServer != null) {
                apiHttpServer.shutdownNow();
            }
            if (getWorkHttpServer != null) {
                getWorkHttpServer.shutdownNow();
            }
            if (LOGGER != null) {
                LOGGER.info("Shutdown !");
            } else {
                System.out.println("Shutdown !");
            }
        }
    };
    hookThread.setDaemon(true);

    Runtime.getRuntime().addShutdownHook(hookThread);
}

From source file:org.dawnsci.commandserver.ui.view.ConsumerView.java

@Override
public void createPartControl(Composite content) {

    content.setLayout(new GridLayout(1, false));
    GridUtils.removeMargins(content);/*  www.j a  va  2s  .c  o m*/

    this.viewer = new TableViewer(content, SWT.FULL_SELECTION | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
    viewer.setUseHashlookup(true);
    viewer.getTable().setHeaderVisible(true);
    viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    createColumns();
    viewer.setContentProvider(createContentProvider());

    consumers = new TreeMap<String, ConsumerBean>(Collections.reverseOrder());
    viewer.setInput(consumers);

    createActions();
    try {
        createTopicListener(getUri());
    } catch (Exception e) {
        logger.error("Cannot listen to topic of command server!", e);
    }

    final Thread job = new Thread(new Runnable() {
        @Override
        public void run() {

            while (!viewer.getTable().isDisposed()) {
                try {
                    Thread.sleep(Constants.NOTIFICATION_FREQUENCY);
                    if (viewer.getControl().isDisposed())
                        return;

                    viewer.getControl().getDisplay().syncExec(new Runnable() {
                        public void run() {
                            viewer.refresh();
                        }
                    });
                } catch (InterruptedException e) {
                    return;
                }
            }
        }
    });

    job.setPriority(Thread.MIN_PRIORITY);
    job.setDaemon(true);
    job.setName("Refresh consumer table");
    job.start();
}

From source file:net.dv8tion.jda.entities.impl.TextChannelImpl.java

@Override
public void sendFileAsync(File file, Message message, Consumer<Message> callback) {
    checkVerification();// w w  w  .j  a  v a  2s.co m
    if (!checkPermission(getJDA().getSelfInfo(), Permission.MESSAGE_WRITE))
        throw new PermissionException(Permission.MESSAGE_WRITE);
    if (!checkPermission(getJDA().getSelfInfo(), Permission.MESSAGE_ATTACH_FILES))
        throw new PermissionException(Permission.MESSAGE_ATTACH_FILES);

    Thread thread = new Thread(() -> {
        Message messageReturn;
        try {
            messageReturn = sendFile(file, message);
        } catch (RateLimitedException e) {
            JDAImpl.LOG.warn("Got ratelimited when trying to upload file. Providing null to callback.");
            messageReturn = null;
        }

        if (callback != null)
            callback.accept(messageReturn);
    });
    thread.setName("TextChannelImpl sendFileAsync Channel: " + id);
    thread.setDaemon(true);
    thread.start();
}