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

public static void main(String[] args) {
    Thread t = new Thread(new Main());
    t.setUncaughtExceptionHandler(new OverrideExceptionHandler());
    System.out.println(t.getUncaughtExceptionHandler());
    t.start();//from  w w  w  . jav a  2 s . c o  m
}

From source file:MultiThreadServer.java

public static void main(String args[]) throws Exception {
    ServerSocket ssock = new ServerSocket(1234);
    System.out.println("Listening");
    while (true) {
        Socket sock = ssock.accept();
        System.out.println("Connected");
        new Thread(new MultiThreadServer(sock)).start();
    }//w w  w.ja v  a  2  s  .  c o m
}

From source file:InterfaceTest.java

public static void main(String args[]) throws Exception {
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("javascript");
    engine.eval("function run() {print('www.java2s.com');}");
    Invocable invokeEngine = (Invocable) engine;
    Runnable runner = invokeEngine.getInterface(Runnable.class);
    Thread t = new Thread(runner);
    t.start();//  w  w w . ja  va 2  s .c  om
    t.join();
}

From source file:SleepInterrupt.java

public static void main(String[] args) {
    SleepInterrupt si = new SleepInterrupt();
    Thread t = new Thread(si);
    t.start();// w  w  w  .  ja v  a  2  s  .co  m

    // Be sure that the new thread gets a chance to
    // run for a while.
    try {
        Thread.sleep(2000);
    } catch (InterruptedException x) {
    }

    System.out.println("in main() - interrupting other thread");
    t.interrupt();
    System.out.println("in main() - leaving");
}

From source file:Main.java

public static void main(String args[]) {
    final Map<String, String> map = new WeakHashMap<String, String>();
    map.put(new String("A"), "B");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("A")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }/*  w  w  w .  j a v a 2 s  .  co m*/
                System.gc();
            }
        }
    };

    Thread t = new Thread(runner);
    t.start();
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Runnable myRunnable = new Runnable() {
        @Override/*w ww. j  a va2 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:Weak.java

public static void main(String args[]) {
    final Map map = new WeakHashMap();
    map.put(new String("Java2s"), "www.java2s.com");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("Java2s")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }//from   www. j a v  a  2 s.com
                System.out.println("Waiting");
                System.gc();
            }
        }
    };
    Thread t = new Thread(runner);
    t.start();
    System.out.println("Main waiting");
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}

From source file:DaemonThread.java

public static void main(String[] args) {
    System.out.println("entering main()");

    Thread t = new Thread(new DaemonThread());
    t.setDaemon(true);//from  w ww .  ja v  a2  s  .co  m
    t.start();

    try {
        Thread.sleep(3000);
    } catch (InterruptedException x) {
    }

    System.out.println("leaving main()");
}

From source file:TestOverrideThread.java

public static void main(String[] args) {
    Thread t = new Thread(new TestOverrideThread());
    t.setUncaughtExceptionHandler(new OverrideExceptionHandler());
    System.out.println(t.getUncaughtExceptionHandler());
    t.start();//from   ww w.  j  av a 2 s .c  o  m
}

From source file:MyThread.java

public static void main(String args[]) {
    System.out.println("Main thread starting.");
    Thread thrd = new Thread(new MyThread());
    thrd.start();//from   w ww.j a v  a  2 s  .  c o  m
    try {
        thrd.join();
    } catch (InterruptedException exc) {
        System.out.println("Main thread interrupted.");
    }
    System.out.println("Main thread ending.");
}