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:Main.java

public static void main(String[] args) {
    Main ct1 = new Main("First Thread");
    Main ct2 = new Main("Second Thread");
    ct1.start();//from ww  w .  jav a 2s .co m
    ct2.start();

    Thread t = Thread.currentThread();
    String threadName = t.getName();
    System.out.println("Inside  main() method:  " + threadName);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ThreadGroup group = Thread.currentThread().getThreadGroup().getParent();
    while (group.getParent() != null) {
        group = group.getParent();/*from  w  ww.  jav a 2s  .c  o  m*/
    }
    int numThreads = group.activeCount();
    Thread[] threads = new Thread[numThreads * 2];
    numThreads = group.enumerate(threads, false);

    for (int i = 0; i < numThreads; i++) {
        Thread thread = threads[i];
        System.out.println(thread.getName());
    }

    int numGroups = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[numGroups * 2];
    numGroups = group.enumerate(groups, false);

    for (int i = 0; i < numGroups; i++) {
        System.out.println(groups[i]);
    }
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {

    ThreadGroup group = new ThreadGroup("new Group");

    MyThread t1 = new MyThread(group, "Thread1");
    MyThread t2 = new MyThread(group, "Thread2");

    t1.start();//from w ww.j  av  a  2 s . c  o  m
    t2.start();

    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    Thread th[] = new Thread[group.activeCount()];
    group.enumerate(th);
    for (Thread t : th) {
        System.out.println(t.getName());
    }
    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    group.interrupt();
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {

    ThreadGroup group = new ThreadGroup("Group");

    ThreadGroup newGroup = new ThreadGroup(group, "new group");

    MyThread t1 = new MyThread(group, "Thread1");
    MyThread t2 = new MyThread(group, "Thread2");

    t1.start();/*from   w  w  w.j a v  a  2  s  . c om*/
    t2.start();

    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    Thread th[] = new Thread[group.activeCount()];
    group.enumerate(th);
    for (Thread t : th) {
        System.out.println(t.getName());
    }
    Thread.sleep(1000);

    System.out.println(group.activeCount() + " threads in thread group...");

    group.interrupt();
}

From source file:ThreadDemo.java

public static void main(String args[]) {

    Thread t = new Thread(new ThreadDemo());
    System.out.println("Executing " + t.getName());
    t.start();/*from  ww w.  j a v a 2  s  .c o m*/

    if (!t.isInterrupted()) {
        t.interrupt();
    }
    try {
        t.join();
    } catch (InterruptedException e) {
    }
}

From source file:ThreadDemo.java

public static void main(String args[]) {

    Thread t = new Thread(new ThreadDemo());
    System.out.println("Executing " + t.getName());
    t.start();//from www.j a  va  2 s .  c  o  m

    if (!t.interrupted()) {
        t.interrupt();
    }
    // block until other threads finish
    try {
        t.join();
    } catch (InterruptedException e) {
    }
}

From source file:ThreadDemo.java

public static void main(String args[]) {
    Thread t = new Thread(new ThreadDemo());
    System.out.println("Executing " + t.getName());
    // this will call run() fucntion
    t.start();//  w  w w . j a va  2  s  .c  o  m

    // interrupt the threads
    if (!t.interrupted()) {
        t.interrupt();
    }
    // block until other threads finish
    try {
        t.join();
    } catch (InterruptedException e) {
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Runnable myRunnable = new Runnable() {
        @Override//from  w  w w. ja v  a  2 s  .co m
        public void run() {
            try {
                System.out.println("Start: " + Thread.currentThread().getName());
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    };
    Thread one = new Thread(myRunnable);
    Thread two = new Thread(myRunnable);
    one.start();
    two.start();

    List<Thread> threads = getThreadsFor(myRunnable);
    for (Thread thread : threads)
        System.out.println("Found: " + thread.getName());
}

From source file:MyThread.java

public static void main(String args[]) throws Exception {
    ThreadGroup tg = new ThreadGroup("My Group");

    MyThread thrd = new MyThread(tg, "MyThread #1");
    MyThread thrd2 = new MyThread(tg, "MyThread #2");
    MyThread thrd3 = new MyThread(tg, "MyThread #3");

    thrd.start();//w w w  .  j  av a  2s  .c  o m
    thrd2.start();
    thrd3.start();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in thread group.");

    Thread thrds[] = new Thread[tg.activeCount()];
    tg.enumerate(thrds);
    for (Thread t : thrds)
        System.out.println(t.getName());

    thrd.myStop();

    Thread.sleep(1000);

    System.out.println(tg.activeCount() + " threads in tg.");
    tg.interrupt();
}

From source file:Main.java

public static void main(String[] args) {
    Thread t1 = Thread.currentThread();

    ThreadGroup tg1 = t1.getThreadGroup();

    System.out.println("Current thread's  name:  " + t1.getName());
    System.out.println("Current thread's  group  name:  " + tg1.getName());

    Thread t2 = new Thread("my  new thread");

    ThreadGroup tg2 = t2.getThreadGroup();
    System.out.println("New  thread's name:  " + t2.getName());
    System.out.println("New  thread's  group  name:  " + tg2.getName());
}