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

public static void main(String args[]) {
    Thread t = new Thread(new ThreadDemo());
    // this will call run() function
    t.start();/*from w  ww.ja  v  a  2 s.  c om*/
}

From source file:ThreadDemo.java

public static void main(String args[]) {

    // thread created
    Thread t = new Thread("java2s.com thread");
    // set thread priority
    t.setPriority(1);//  www  .  j  a  v  a  2 s .c  o  m
    // prints thread created
    System.out.println("thread  = " + t);
    // this will call run() function
    t.start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    PipedInputStream pis = new PipedInputStream();
    PipedOutputStream pos = new PipedOutputStream();
    pos.connect(pis);//  w w  w . j  av a2 s . co m

    Runnable producer = () -> produceData(pos);
    Runnable consumer = () -> consumeData(pis);
    new Thread(producer).start();
    new Thread(consumer).start();
}

From source file:Main.java

public static void main(String[] args) {
    Main main = new Main();
    for (int i = 0; i < NUM_THREADS; i++)
        new Thread(main).start();
}

From source file:Main.java

public static void main(String... args) {
    //Anonymous ways
    Runnable r1 = new Runnable() {
        public void run() {
            System.out.println("Hello world from " + Thread.currentThread().getName());
        }/*w  w  w .  j a  va2  s  . c o  m*/
    };

    // Runnable is already a functional interface
    Runnable r2 = () -> System.out.println("Hello world  from " + Thread.currentThread().getName());
    (new Thread(r1)).start();
    (new Thread(r2)).start();

}

From source file:Pipe.java

public static void main(String args[]) throws Exception {
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(in);

    Sender s = new Sender(out);
    Receiver r = new Receiver(in);
    Thread t1 = new Thread(s);
    Thread t2 = new Thread(r);
    t1.start();/* ww w .  j  av  a2  s  .  c om*/
    t2.start();
}

From source file:Main.java

public static void main(String[] args) {
    int[] runningThreads = { 0 };
    int[] taskcount = { 10 };
    Lock myLock = new ReentrantLock(true);
    int maxThreadQty = 3;
    while ((taskcount[0] > 0) && (runningThreads[0] < maxThreadQty)) {
        myLock.lock();/*  w  w w. j  av  a  2 s  .  co  m*/
        runningThreads[0]++;
        myLock.unlock();
        new Thread("T") {
            public void run() {
                myLock.lock();
                taskcount[0]--;
                runningThreads[0]--;
                myLock.unlock();
            }
        }.start();
    }
}

From source file:ThreadDemo.java

public static void main(String[] args) throws Exception {
    Thread t = new Thread(new ThreadDemo());
    t.start();//from w ww .ja  v  a2s. com

}

From source file:Test.java

public static void main(String[] args) {
    final AtomicLong orderIdGenerator = new AtomicLong(0);
    final List<Item> orders = Collections.synchronizedList(new ArrayList<Item>());

    for (int i = 0; i < 10; i++) {
        Thread orderCreationThread = new Thread(new Runnable() {
            public void run() {
                for (int i = 0; i < 10; i++) {
                    long orderId = orderIdGenerator.incrementAndGet();
                    Item order = new Item(Thread.currentThread().getName(), orderId);
                    orders.add(order);//from  w w  w.j av  a  2  s  . c o m
                }
            }
        });
        orderCreationThread.setName("Order Creation Thread " + i);
        orderCreationThread.start();
    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    Set<Long> orderIds = new HashSet<Long>();
    for (Item order : orders) {
        orderIds.add(order.getID());
        System.out.println("Order id:" + order.getID());
    }
}

From source file:ThreadTester.java

public static void main(String[] args) {
    SynchronizedQueue<String> queue = new SynchronizedQueue<String>(10);
    final int GREETING_COUNT = 100;
    Runnable run1 = new Producer("Hello, World!", queue, GREETING_COUNT);
    Runnable run2 = new Producer("Goodbye, World!", queue, GREETING_COUNT);
    Runnable run3 = new Consumer(queue, 2 * GREETING_COUNT);

    Thread thread1 = new Thread(run1);
    Thread thread2 = new Thread(run2);
    Thread thread3 = new Thread(run3);

    thread1.start();//w w  w.  j a  va2  s.  co  m
    thread2.start();
    thread3.start();
}