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

Source Link

Document

Allocates a new Thread object.

Usage

From source file:Main.java

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

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            System.out.println("Do shutdown work ...");
        }/*from   w ww  . ja v  a2  s.c o  m*/
    });

}

From source file:Main.java

public static void main(String str[]) {
    final Object monitor = new Object();
    new Thread() {
        public void run() {
            try {
                synchronized (monitor) {
                    System.out.println("10 seconds ...");
                    monitor.wait(10000);
                    System.out.println("Wait over");
                }/*  w ww . j ava  2s . c  o  m*/
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }.start();
}

From source file:Main.java

public static void main(String[] args) {
    Thread thread = new Thread() {
        public void run() {
            throw new NullPointerException();
        }//from   www  .j a v a2 s  . com
    };
    thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread arg0, Throwable arg1) {
            arg1.printStackTrace();
        }
    });
    thread.start();
}

From source file:Main.java

public static void main(String[] args) {
    CyclicBarrier barrier = new CyclicBarrier(2);
    new Thread() {
        @Override//from  w w w.  j  a va  2 s  .  co m
        public void run() {
            try {
                System.out.println("in thread, before the barrier");
                barrier.await();
                System.out.println("in thread, after the barrier");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();

    try {
        System.out.println("main thread, before barrier");
        barrier.await();

        System.out.println("main thread, after barrier");
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    Thread t = Thread.currentThread();
    System.out.println("main Thread  Priority:" + t.getPriority());

    Thread t1 = new Thread();
    System.out.println("Thread(t1) Priority:" + t1.getPriority());

    t.setPriority(Thread.MAX_PRIORITY);
    System.out.println("main Thread  Priority:" + t.getPriority());

    Thread t2 = new Thread();
    System.out.println("Thread(t2) Priority:" + t2.getPriority());

    // Change thread t2 priority to minimum
    t2.setPriority(Thread.MIN_PRIORITY);
    System.out.println("Thread(t2) Priority:" + t2.getPriority());
}

From source file:Main2.java

public static void main(String arg[]) throws Exception {
    System.out.println("Main");
    Thread t1 = new Thread() {
        public void run() {
            Main2.main(new String[] {});
        }//from ww  w .j av  a2s.c o m
    };
    t1.start();
    t1.join();
    System.out.println("Main again");
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Object f = new Object() {
        public void finalize() {
            System.out.println("Running finalize()");
        }/*from  ww  w.jav a  2 s .  com*/
    };
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            System.out.println("Running Shutdown Hook");
        }
    });

    f = null;
    System.gc();

    System.out.println("Calling System.exit()");
    System.exit(0);
}

From source file:AnotherDeadLock.java

public static void main(String[] args) {
    final Object resource1 = "resource1";
    final Object resource2 = "resource2";
    // t1 tries to lock resource1 then resource2
    Thread t1 = new Thread() {
        public void run() {
            // Lock resource 1
            synchronized (resource1) {
                System.out.println("Thread 1: locked resource 1");

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }//from   w ww . ja  v a  2 s  .c om

                synchronized (resource2) {
                    System.out.println("Thread 1: locked resource 2");
                }
            }
        }
    };

    // t2 tries to lock resource2 then resource1
    Thread t2 = new Thread() {
        public void run() {
            synchronized (resource2) {
                System.out.println("Thread 2: locked resource 2");

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                }

                synchronized (resource1) {
                    System.out.println("Thread 2: locked resource 1");
                }
            }
        }
    };

    // If all goes as planned, deadlock will occur,
    // and the program will never exit.
    t1.start();
    t2.start();
}

From source file:edu.gmu.isa681.server.Main.java

public static void main(String[] args) {
    log.info("Starting server...");
    final Server server = new Server(Constants.SERVER_PORT);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            server.shutdown();/*from  ww w . j  a va2s. c  o m*/
        }
    });

    Thread thread = new Thread(server, "ServerThread");
    thread.start();

    try {
        thread.join();

    } catch (InterruptedException ex) {
        log.error(ex);
    }

    //TODO: implement some CLI to admin the server
}

From source file:ExecDemoPartial.java

public static void main(String argv[]) throws IOException {

    BufferedReader is; // reader for output of process
    String line;//from ww w. j av  a2s. c  om

    final Process p = Runtime.getRuntime().exec(PROGRAM);

    Thread waiter = new Thread() {
        public void run() {
            try {
                p.waitFor();
            } catch (InterruptedException ex) {
                // OK, just quit this thread.
                return;
            }
            System.out.println("Program terminated!");
            done = true;
        }
    };
    waiter.start();

    // getInputStream gives an Input stream connected to
    // the process p's standard output (and vice versa). We use
    // that to construct a BufferedReader so we can readLine() it.
    is = new BufferedReader(new InputStreamReader(p.getInputStream()));

    while (!done && ((line = is.readLine()) != null))
        System.out.println(line);

    return;
}