Example usage for java.lang Thread Thread

List of usage examples for java.lang Thread Thread

Introduction

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

Prototype

public Thread(String name) 

Source Link

Document

Allocates a new Thread object.

Usage

From source file:Test.java

public static void main(String[] args) {
    Thread producerThread = new Thread(new ItemProducer());
    Thread consumerThread = new Thread(new ItemConsumer());
    producerThread.start();/*from   ww  w  .  j  av  a 2  s .c om*/
    consumerThread.start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Runner t = new Runner();
    Thread t1 = new Thread(t);
    for (int i = 0; i < 50; i++) {
        t.queue.add(i);//from   w w w .j a v  a 2  s.c  om
    }
    System.out.println(("Number of items in queue: " + t.queue.size()));
    t1.start();
    Thread.sleep(1000);
    t1.interrupt();
    t1.join();
    System.out.println(("Number of items in queue: " + t.queue.size()));
    System.out.println(("Joined t1. Finished"));
}

From source file:ThreadDemo.java

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

    Thread t = new Thread(new ThreadDemo());
    // this will call run() function
    t.start();/*from w  w  w.ja v a 2 s  .co  m*/
    // waits for this thread to die
    t.join();
    // tests if this thread is alive
    System.out.println("status = " + t.isAlive());
}

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());
}

From source file:Test.java

public static void main(String[] args) {
    final Lock firstLock = new ReentrantLock();
    final Lock secondLock = new ReentrantLock();
    firstLock.lock();/*w w  w.j  a v a 2s  .co  m*/
    Thread secondThread = new Thread(new Runnable() {
        public void run() {
            secondLock.lock();
            firstLock.lock();
        }
    });
    secondThread.start();
    try {
        Thread.sleep(250);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    secondLock.lock();

    secondLock.unlock();
    firstLock.unlock();
}

From source file:ThreadDemo.java

public static void main(String[] args) {

    Thread th = new Thread(new ThreadDemo());
    th.start();
}

From source file:Main.java

public static void main(String[] args) {
    CountDownLatch latch = new CountDownLatch(1);
    for (int threadNo = 0; threadNo < 1000; threadNo++) {
        Runnable t = new Test(latch);
        new Thread(t).start();
    }/*www.  j a  v a 2 s.  c o m*/
    try {
        latch.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.out.println("done");
}

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 w  w  w .  j a  v a 2 s  . c  om

    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());
    // this will call run() fucntion
    t.start();//from  www .  j  a  v  a 2 s  . co m

    // interrupt the threads
    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());
    t.start();/*from   ww  w  . j  a v  a2  s.c  o  m*/

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