Example usage for java.lang Thread getName

List of usage examples for java.lang Thread getName

Introduction

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

Prototype

public final String getName() 

Source Link

Document

Returns this thread's name.

Usage

From source file:com.jkoolcloud.tnt4j.streams.StreamsAgentTest.java

@Test
public void testRunFromAPI() throws Exception {
    final String testStreamName = "TestStream"; // NON-NLS
    final File tempConfFile = File.createTempFile("testConfigutarion", ".xml");
    FileWriter fw = new FileWriter(tempConfFile);
    String sb = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + Utils.NEW_LINE + "<tnt-data-source"
            + Utils.NEW_LINE // NON-NLS
            + "        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + Utils.NEW_LINE // NON-NLS
            + "        xsi:noNamespaceSchemaLocation=\"https://raw.githubusercontent.com/Nastel/tnt4j-streams/master/config/tnt-data-source.xsd\">" // NON-NLS
            + Utils.NEW_LINE + "    <stream name=\"" + testStreamName // NON-NLS
            + "\" class=\"com.jkoolcloud.tnt4j.streams.inputs.CharacterStream\">" + Utils.NEW_LINE // NON-NLS
            + "        <property name=\"HaltIfNoParser\" value=\"false\"/>" + Utils.NEW_LINE // NON-NLS
            + "        <property name=\"Port\" value=\"9595\"/>" + Utils.NEW_LINE + "    </stream>"
            + Utils.NEW_LINE // NON-NLS
            + "</tnt-data-source>"; // NON-NLS
    fw.write(sb);// w w w . j  a  v a2 s  . c  o  m
    fw.flush();
    Utils.close(fw);
    StreamsAgent.runFromAPI(tempConfFile.getAbsolutePath());
    Thread.sleep(500);
    tempConfFile.delete();
    final Set<Thread> threads = Thread.getAllStackTraces().keySet();
    for (Thread thread : threads) {
        if (thread.getName().contains(testStreamName)) {
            return;
        } else {
            continue;
        }
    }
    fail("No streams thread created");
}

From source file:org.mailster.MailsterSWT.java

private static void startApplication(String[] args) {
    MailsterSWT main = getInstance();//  ww  w.  j a  va  2  s  .c  o m
    main.smtpService = new MailsterSmtpService();
    MailsterPrefStore store = ConfigurationManager.CONFIG_STORE;

    try {
        store.load();
    } catch (IOException e) {
        LOG.debug("Unable to read preferences file. Loading defaults ...");

        // Set default preferences
        store.setValue(ConfigurationManager.MAIL_QUEUE_REFRESH_INTERVAL_KEY, "300000"); //$NON-NLS-1$
        store.setValue(ConfigurationManager.ASK_ON_REMOVE_MAIL_KEY, true);
        store.setValue(ConfigurationManager.APPLY_MAIN_WINDOW_PARAMS_KEY, true);
        store.setValue(ConfigurationManager.PREFERRED_BROWSER_KEY,
                Messages.getString("MailsterSWT.default.browser")); //$NON-NLS-1$
        store.setValue(ConfigurationManager.PREFERRED_CONTENT_TYPE_KEY, "text/html"); //$NON-NLS-1$

        store.setValue(ConfigurationManager.NOTIFY_ON_NEW_MESSAGES_RECEIVED_KEY, true);
        store.setValue(ConfigurationManager.AUTO_HIDE_NOTIFICATIONS_KEY, true);

        store.setValue(ConfigurationManager.LANGUAGE_KEY, "en");

        store.setValue(ConfigurationManager.EXECUTE_ENCLOSURE_ON_CLICK_KEY, true);
        store.setValue(ConfigurationManager.DEFAULT_ENCLOSURES_DIRECTORY_KEY, System.getProperty("user.home")); //$NON-NLS-1$

        store.setValue(ConfigurationManager.START_POP3_ON_SMTP_START_KEY, true);

        store.setValue(ConfigurationManager.POP3_SERVER_KEY, "");
        store.setValue(ConfigurationManager.POP3_PORT_KEY, MailsterPop3Service.POP3_PORT);
        store.setValue(ConfigurationManager.POP3_SPECIAL_ACCOUNT_KEY,
                MailBoxManager.POP3_SPECIAL_ACCOUNT_LOGIN);
        store.setValue(ConfigurationManager.POP3_ALLOW_APOP_AUTH_METHOD_KEY, true);
        store.setValue(ConfigurationManager.POP3_REQUIRE_SECURE_AUTH_METHOD_KEY, true);
        store.setValue(ConfigurationManager.POP3_PASSWORD_KEY, UserManager.DEFAULT_PASSWORD);
        store.setValue(ConfigurationManager.POP3_CONNECTION_TIMEOUT_KEY,
                Pop3ProtocolHandler.DEFAULT_TIMEOUT_SECONDS);

        store.setValue(ConfigurationManager.AUTH_SSL_CLIENT_KEY, true);
        store.setValue(ConfigurationManager.PREFERRED_SSL_PROTOCOL_KEY, SSLProtocol.TLS.toString());
        store.setValue(ConfigurationManager.CRYPTO_STRENGTH_KEY, 512);

        store.setValue(ConfigurationManager.SMTP_SERVER_KEY, "");
        store.setValue(ConfigurationManager.SMTP_PORT_KEY, MailsterSMTPServer.DEFAULT_SMTP_PORT);
        store.setValue(ConfigurationManager.SMTP_CONNECTION_TIMEOUT_KEY,
                MailsterSMTPServer.DEFAULT_TIMEOUT / 1000);
    }

    String localeInfo = store.getString(ConfigurationManager.LANGUAGE_KEY);

    if (localeInfo != null && !"".equals(localeInfo)) {
        if (localeInfo.indexOf('_') != -1)
            Messages.setLocale(new Locale(localeInfo.substring(0, 2), localeInfo.substring(3)));
        else
            Messages.setLocale(new Locale(localeInfo));
    }

    main.smtpService
            .setQueueRefreshTimeout(store.getLong(ConfigurationManager.SMTP_CONNECTION_TIMEOUT_KEY) / 1000);

    main.smtpService.setAutoStart(store.getBoolean(ConfigurationManager.START_SMTP_ON_STARTUP_KEY));

    if (args.length > 3)
        usage();
    {
        for (int i = 0, max = args.length; i < max; i++) {
            if ("-autostart".equals(args[i]))
                main.smtpService.setAutoStart(true);
            else if (args[i].startsWith("-lang=")) {
                Messages.setLocale(new Locale(args[i].substring(6)));
            } else {
                try {
                    main.smtpService.setQueueRefreshTimeout(Long.parseLong(args[i]));
                } catch (NumberFormatException e) {
                    usage();
                }
            }
        }
    }

    final MailsterSWT _main = main;
    Thread.UncaughtExceptionHandler exHandler = new Thread.UncaughtExceptionHandler() {
        public void uncaughtException(final Thread t, final Throwable ex) {
            ex.printStackTrace();
            Display.getDefault().asyncExec(new Runnable() {
                public void run() {
                    _main.log(Messages.getString("MailsterSWT.exception.log1") + t.getName() //$NON-NLS-1$
                            + Messages.getString("MailsterSWT.exception.log2") //$NON-NLS-1$
                            + ex.getMessage());
                }
            });
            _main.smtpService.shutdownServer(false);
        }
    };
    Thread.setDefaultUncaughtExceptionHandler(exHandler);
    Thread.currentThread().setUncaughtExceptionHandler(exHandler);

    LOG.debug("Creating shell ...");
    main.createSShell();
    main.applyPreferences();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                _main.smtpService.shutdownServer(true);
                if (_main.trayItem != null)
                    _main.trayItem.dispose();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        };
    });
}

From source file:org.jajuk.JajukTestCase.java

@Override
protected void tearDown() throws Exception {
    Map<Thread, StackTraceElement[]> traces = Thread.getAllStackTraces();
    Iterator<Thread> i = traces.keySet().iterator();
    while (i.hasNext()) {
        Thread thd = i.next();
        if (thd.getName().contains("MPlayer reader thread")
                || thd.getName().contains("MPlayer writer thread")) {
            TestHelpers.dumpThreads();//w ww. j  a v a2  s  .  com
            throw new IllegalStateException("Had leftover MPlayer thread: " + thd.getName());
        }
    }
    super.tearDown();
}

From source file:com.alibaba.napoli.gecko.service.timer.ThreadRenamingRunnable.java

public void run() {
    final Thread currentThread = Thread.currentThread();
    final String oldThreadName = currentThread.getName();
    final String newThreadName = this.proposedThreadName;

    // Change the thread name before starting the actual runnable.
    boolean renamed = false;
    if (!oldThreadName.equals(newThreadName)) {
        try {/* w  w w . j a  v  a 2s  . c o m*/
            currentThread.setName(newThreadName);
            renamed = true;
        } catch (SecurityException e) {
            logger.debug("Failed to rename a thread " + "due to security restriction.", e);
        }
    }

    // Run the actual runnable and revert the name back when it ends.
    try {
        this.runnable.run();
    } finally {
        if (renamed) {
            // Revert the name back if the current thread was renamed.
            // We do not check the exception here because we know it works.
            currentThread.setName(oldThreadName);
        }
    }
}

From source file:de.doering.dwca.flickr.OccurrenceExport.java

private void searchYear(int year) {
    if (threads.size() < THREADS) {
        threads.add(startThread(year));/*from   w w  w  .  ja v  a2s  .  c  o  m*/
    } else {
        // wait until one thread is finished
        log.debug("Waiting for a thread to finish");
        do {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            Iterator<Thread> iter = threads.iterator();
            while (iter.hasNext()) {
                Thread t = iter.next();
                if (!t.isAlive()) {
                    log.debug("Thread " + t.getName() + " finished");
                    iter.remove();
                }
            }
        } while (threads.size() == THREADS);

        threads.add(startThread(year));
    }
}

From source file:org.apache.bookkeeper.test.ZooKeeperUtil.java

public void sleepServer(final int seconds, final CountDownLatch l) throws InterruptedException, IOException {
    Thread[] allthreads = new Thread[Thread.activeCount()];
    Thread.enumerate(allthreads);
    for (final Thread t : allthreads) {
        if (t.getName().contains("SyncThread:0")) {
            Thread sleeper = new Thread() {
                public void run() {
                    try {
                        t.suspend();/* w w  w  .  j  a va 2s .c  o  m*/
                        l.countDown();
                        Thread.sleep(seconds * 1000);
                        t.resume();
                    } catch (Exception e) {
                        LOG.error("Error suspending thread", e);
                    }
                }
            };
            sleeper.start();
            return;
        }
    }
    throw new IOException("ZooKeeper thread not found");
}

From source file:com.brienwheeler.lib.concurrent.NamedThreadFactoryTest.java

@Test
public void testNewThread() {
    NamedThreadFactory factory = new NamedThreadFactory();
    factory.setName(NAME);/*from w ww  . j ava 2s  .com*/
    Thread thread = factory.newThread(new NullRunnable());
    Assert.assertEquals(NAME + "-1", thread.getName());
}

From source file:com.brienwheeler.lib.concurrent.NamedThreadFactoryTest.java

@Test
public void testTwoThreadsSameGroup() {
    NamedThreadFactory factory = new NamedThreadFactory();
    factory.setName(NAME);/*from w ww  .  ja va  2s  .c o  m*/
    Thread thread1 = factory.newThread(new NullRunnable());
    Assert.assertEquals(NAME + "-1", thread1.getName());
    Thread thread2 = factory.newThread(new NullRunnable());
    Assert.assertEquals(NAME + "-2", thread2.getName());
    Assert.assertEquals(thread1.getThreadGroup(), thread2.getThreadGroup());
}

From source file:com.brienwheeler.lib.concurrent.NamedThreadFactoryTest.java

@Test
public void testTwoFactoriesSameName() {
    NamedThreadFactory factory1 = new NamedThreadFactory();
    factory1.setName(NAME);//from  w w  w.jav a2s.  c om
    Thread thread1 = factory1.newThread(new NullRunnable());
    Assert.assertEquals(NAME + "-1", thread1.getName());

    NamedThreadFactory factory2 = new NamedThreadFactory();
    factory2.setName(NAME);
    Thread thread2 = factory2.newThread(new NullRunnable());
    Assert.assertEquals(NAME + "-1", thread2.getName());

    Assert.assertNotSame(thread1.getThreadGroup(), thread2.getThreadGroup());
}

From source file:org.apache.atlas.web.filters.AuditFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    final String requestTimeISO9601 = DateTimeHelper.formatDateUTC(new Date());
    final HttpServletRequest httpRequest = (HttpServletRequest) request;
    final String requestId = UUID.randomUUID().toString();
    final Thread currentThread = Thread.currentThread();
    final String oldName = currentThread.getName();
    String user = getUserFromRequest(httpRequest);

    try {/*from w  ww. j  av  a2 s .  c o m*/
        currentThread.setName(formatName(oldName, requestId));
        RequestContext requestContext = RequestContext.createContext();
        requestContext.setUser(user);
        recordAudit(httpRequest, requestTimeISO9601, user);
        filterChain.doFilter(request, response);
    } finally {
        // put the request id into the response so users can trace logs for this request
        ((HttpServletResponse) response).setHeader(AtlasClient.REQUEST_ID, requestId);
        currentThread.setName(oldName);
        recordMetrics();
        RequestContext.clear();
        RequestContextV1.clear();
    }
}