List of usage examples for java.lang Thread Thread
public Thread(String name)
From source file:Main.java
public static void main(String[] args) { Student a = new Student("A"); Student b = new Student("B"); new Thread(() -> a.bow(b)).start(); new Thread(() -> b.bow(a)).start(); }
From source file:Main.java
public static void main(String[] args) { Runnable runnable = andThen(() -> System.out.print("Hello "), () -> System.out.print("World!")); new Thread(runnable).start(); }
From source file:FlagComm.java
public static void main(String args[]) { FlagSend s = new FlagSend(); FlagRec r = new FlagRec(s); Thread st = new Thread(s); Thread rt = new Thread(r); rt.setDaemon(true);/*www. j a va2 s. c o m*/ st.start(); rt.start(); try { while (s.isValid) { Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) throws Exception { Semaphore sem = new Semaphore(1, true); Thread thrdA = new Thread(new MyThread(sem, "Message 1")); Thread thrdB = new Thread(new MyThread(sem, "Message 2")); thrdA.start();/*from w w w . ja v a 2 s . c om*/ thrdB.start(); thrdA.join(); thrdB.join(); }
From source file:Main.java
public static void main(String args[]) throws Exception { Semaphore sem = new Semaphore(1, true); Thread thrdA = new Thread(new SyncOutput(sem, "Message 1")); Thread thrdB = new Thread(new SyncOutput(sem, "Message 2!")); thrdA.start();//from w w w . j a v a2 s.c om thrdB.start(); thrdA.join(); thrdB.join(); }
From source file:Main.java
public static void main(String... s) { cdl = new CountDownLatch(1); Thread a = new Thread(() -> { System.out.println("started a"); try {/*from ww w . j a va 2s . c om*/ Thread.sleep(4000); } catch (InterruptedException e) { e.printStackTrace(); } cdl.countDown(); System.out.println("stoped a"); }); Thread b = new Thread(() -> { System.out.println("started b"); System.out.println("wait a"); try { cdl.await(); } catch (Exception e) { e.printStackTrace(); } System.out.println("stoped b"); }); b.start(); a.start(); }
From source file:WaitComm.java
public static void main(String args[]) { WFlagSend s = new WFlagSend(); WFlagRec r = new WFlagRec(s); Thread st = new Thread(s); Thread rt = new Thread(r); rt.setDaemon(true);/* ww w . ja v a2 s.c om*/ st.start(); rt.start(); try { st.join(); while (s.isValid) { Thread.sleep(100); } } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { Thread thread1 = new Thread(new PipeOutput("Producer")); Thread thread2 = new Thread(new PipeInput("Consumer")); thread1.start();//w ww . j av a 2 s .c o m thread2.start(); boolean thread1IsAlive = true; boolean thread2IsAlive = true; do { if (thread1IsAlive && !thread1.isAlive()) { thread1IsAlive = false; } if (thread2IsAlive && !thread2.isAlive()) { thread2IsAlive = false; } } while (thread1IsAlive || thread2IsAlive); }
From source file:Main.java
public static void main(String[] arg) { Runtime runTime = Runtime.getRuntime(); Main hook = new Main(); runTime.addShutdownHook(new Thread(hook)); }
From source file:ThreadDemo.java
public static void main(String args[]) { ThreadDemo trace = new ThreadDemo(); Thread t = new Thread(trace); // this will call run() method t.start();/*from ww w .j a v a 2 s . c o m*/ // returns a map of stack traces Map m = Thread.getAllStackTraces(); }