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(Runnable target, String name) 

Source Link

Document

Allocates a new Thread object.

Usage

From source file:TwoObjects.java

public static void main(String[] args) {
    final TwoObjects obj1 = new TwoObjects("obj1");
    final TwoObjects obj2 = new TwoObjects("obj2");

    Runnable runA = new Runnable() {
        public void run() {
            obj1.synchronizedMethod(3);/*from w  w  w.j av a2 s .c om*/
        }
    };

    Thread threadA = new Thread(runA, "threadA");
    threadA.start();

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

    Runnable runB = new Runnable() {
        public void run() {
            obj2.synchronizedMethod(7);
        }
    };

    Thread threadB = new Thread(runB, "threadB");
    threadB.start();
}

From source file:Deadlock.java

public static void main(String[] args) {
    final Deadlock obj1 = new Deadlock("Thread 1");
    final Deadlock obj2 = new Deadlock("Thread 2");

    Runnable runA = new Runnable() {
        public void run() {
            obj1.checkOther(obj2);/*from   w  w w .  j  a  va2s  .  com*/
        }
    };

    Thread thread = new Thread(runA, "A");
    thread.start();

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

    Runnable runB = new Runnable() {
        public void run() {
            obj2.checkOther(obj1);
        }
    };

    Thread threadB = new Thread(runB, "B");
    threadB.start();

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

    threadPrint("finished sleeping");

    threadPrint("about to interrupt() threadA");
    thread.interrupt();

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

    threadPrint("about to interrupt() threadB");
    threadB.interrupt();

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

    threadPrint("did that break the deadlock?");
}

From source file:TransactionsDemo.java

public static void main(String[] args) {
    String url = "tcp://localhost:61616";
    String user = null;/*from   w ww  .j  a va 2  s  .co m*/
    String password = null;

    if (args.length >= 1) {
        url = args[0];
    }

    if (args.length >= 2) {
        user = args[1];
    }

    if (args.length >= 3) {
        password = args[2];
    }

    Retailer r = new Retailer(url, user, password);
    Vendor v = new Vendor(url, user, password);
    Supplier s1 = new Supplier("HardDrive", "StorageOrderQueue", url, user, password);
    Supplier s2 = new Supplier("Monitor", "MonitorOrderQueue", url, user, password);

    new Thread(r, "Retailer").start();
    new Thread(v, "Vendor").start();
    new Thread(s1, "Supplier 1").start();
    new Thread(s2, "Supplier 2").start();
}

From source file:TestPipes.java

public static void main(String[] args) throws Exception {
    final PipedOutputStream pos = new PipedOutputStream();

    final PipedInputStream pis = new PipedInputStream(pos);

    Runnable runOutput = new Runnable() {
        public void run() {
            writeData(pos);/*from  w  w w  .j  a va2  s .  c  o  m*/
        }
    };

    Thread outThread = new Thread(runOutput, "outThread");
    outThread.start();

    Runnable runInput = new Runnable() {
        public void run() {
            readData(pis);
        }
    };

    Thread inThread = new Thread(runInput, "inThread");
    inThread.start();
}

From source file:PipedCharacters.java

public static void main(String[] args) {
    try {//from   w  ww  .  j a  va  2s  .c om
        final PipedWriter out = new PipedWriter();

        final PipedReader in = new PipedReader(out);

        Runnable runA = new Runnable() {
            public void run() {
                writeStuff(out);
            }
        };

        Thread threadA = new Thread(runA, "threadA");
        threadA.start();

        Runnable runB = new Runnable() {
            public void run() {
                readStuff(in);
            }
        };

        Thread threadB = new Thread(runB, "threadB");
        threadB.start();
    } catch (IOException x) {
        x.printStackTrace();
    }
}

From source file:EarlyNotify.java

public static void main(String[] args) {
    final EarlyNotify enf = new EarlyNotify();

    Runnable runA = new Runnable() {
        public void run() {
            try {
                String item = enf.removeItem();
                print("returned: '" + item + "'");
            } catch (InterruptedException ix) {
                print("interrupted!");
            } catch (Exception x) {
                print("threw an Exception!!!\n" + x);
            }/*www  .  j  a  v  a2 s . c  o  m*/
        }
    };

    Runnable runB = new Runnable() {
        public void run() {
            enf.addItem("Hello!");
        }
    };

    try {
        Thread threadA1 = new Thread(runA, "A");
        threadA1.start();

        Thread.sleep(500);

        Thread threadA2 = new Thread(runA, "B");
        threadA2.start();

        Thread.sleep(500);

        Thread threadB = new Thread(runB, "C");
        threadB.start();

        Thread.sleep(1000);

        threadA1.interrupt();
        threadA2.interrupt();
    } catch (InterruptedException x) {
    }
}

From source file:PipedBytes.java

public static void main(String[] args) {
    try {//from  w ww .ja  v  a2s.  c o  m
        final PipedOutputStream out = new PipedOutputStream();

        final PipedInputStream in = new PipedInputStream(out);

        Runnable runA = new Runnable() {
            public void run() {
                writeStuff(out);
            }
        };

        Thread threadA = new Thread(runA, "threadA");
        threadA.start();

        Runnable runB = new Runnable() {
            public void run() {
                readStuff(in);
            }
        };

        Thread threadB = new Thread(runB, "threadB");
        threadB.start();
    } catch (IOException x) {
        x.printStackTrace();
    }
}

From source file:net.sf.janos.Janos.java

/**
 * @param args//from   ww  w  . java 2  s.  c  o m
 * @throws Exception 
 */
public static void main(String[] args) {
    if (args.length > 1) {
        System.out.println("Usage: Janos [port]");
        System.exit(1);
    }

    if (args.length == 1) {
        System.setProperty("net.sbbi.upnp.Discovery.bindPort", args[0]);
    }

    /*
     * [DW] For some reason unknown to me, given: 
     * 1) arch is intel
     * 2) os is Mac OS X 
     * 3) running in eclipse
     * 4) using SWT
     * no exceptions are displayed in the eclipse console in the main thread. 
     * To work around this, we just do everything in a new thread :-)
     */
    if (Boolean.getBoolean("net.sf.janos.forkNewThread")) {
        try {
            Thread mainThread = new Thread(new Janos(), "Janos-SWT");
            mainThread.start();
            mainThread.join();
        } catch (Throwable t) {
            LogFactory.getLog(Janos.class).fatal("Could not start thread: ", t);
            System.exit(1);
        }
    } else {
        new Janos().run();
    }
}

From source file:net.nikey.utils.NikeySecurityTest.java

public static void main(String[] agrs) {
    NikeySecurityTest td = new NikeySecurityTest();
    Thread t1 = new Thread(td, "001");
    Thread t2 = new Thread(td, "002");
    Thread t3 = new Thread(td, "003");
    Thread t4 = new Thread(td, "004");
    t1.start();//from w ww.  j  a va  2 s . c o m
    t2.start();
    t3.start();
    t4.start();
    System.out.println("name is:" + NikeySecurity.isSignedIn());
}

From source file:ProducerComsumer.java

public static void main(String[] args) {
    final ProducerComsumer ch = new ProducerComsumer();

    Runnable runA = new Runnable() {
        public void run() {
            try {
                String str;/*w w w. j  a  v  a 2s  .c o m*/
                Thread.sleep(500);

                str = "multithreaded";
                ch.putIn(str);
                str = "programming";
                ch.putIn(str);

                str = "with Java";
                ch.putIn(str);
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    };

    Runnable runB = new Runnable() {
        public void run() {
            try {
                Object obj;

                obj = ch.takeOut();
                System.out.println("in run() - just took out: '" + obj + "'");

                Thread.sleep(500);

                obj = ch.takeOut();
                System.out.println("in run() - just took out: '" + obj + "'");

                obj = ch.takeOut();
                System.out.println("in run() - just took out: '" + obj + "'");
            } catch (InterruptedException x) {
                x.printStackTrace();
            }
        }
    };

    Thread threadA = new Thread(runA, "threadA");
    threadA.start();

    Thread threadB = new Thread(runB, "threadB");
    threadB.start();
}