Example usage for java.lang Thread start

List of usage examples for java.lang Thread start

Introduction

In this page you can find the example usage for java.lang Thread start.

Prototype

public synchronized void start() 

Source Link

Document

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame parentFrame = new JFrame();
    parentFrame.setSize(500, 150);/*from   w ww  . ja va  2 s.c  o  m*/
    JLabel jl = new JLabel();
    jl.setText("Count : 0");

    parentFrame.add(BorderLayout.CENTER, jl);
    parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    parentFrame.setVisible(true);

    final JDialog dlg = new JDialog(parentFrame, "Progress Dialog", true);
    JProgressBar dpb = new JProgressBar(0, 500);
    dlg.add(BorderLayout.CENTER, dpb);
    dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
    dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dlg.setSize(300, 75);
    dlg.setLocationRelativeTo(parentFrame);

    Thread t = new Thread(new Runnable() {
        public void run() {
            dlg.setVisible(true);
        }
    });
    t.start();
    for (int i = 0; i <= 500; i++) {
        jl.setText("Count : " + i);
        dpb.setValue(i);
        if (dpb.getValue() == 500) {
            dlg.setVisible(false);
            System.exit(0);

        }
        try {
            Thread.sleep(25);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    dlg.setVisible(true);
}

From source file:TryThread.java

public static void main(String[] args) {
    Thread first = new TryThread("A ", "a ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    first.start();
    second.start();/*www .jav  a2s. c  o m*/
    third.start();
    try {
        Thread.sleep(3000);
    } catch (Exception e) {
        System.out.println(e);
    }
    first.interrupt();
    second.interrupt();
    third.interrupt();
    if (first.isInterrupted()) {
        System.out.println("First thread has been interrupted.");
    }
}

From source file:com.jgoetsch.eventtrader.EventTraderSpringLauncher.java

public static void main(String[] args) {
    if (args.length < 1) {
        System.out.println("Usage: " + EventTraderSpringLauncher.class.getSimpleName() + " <files>...");
        System.out.println("       files - List of paths to spring bean definition xml files.");
        System.out.println("               Each object defined that implements Runnable will be executed");
        System.out.println("               in its own thread.");
    } else {/*from   w  ww.j a va  2  s  .co m*/
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(args);

        // auto register growl notifications after all GrowlNotification objects have been instantiated
        // if it is found on the classpath
        try {
            Class.forName("com.jgoetsch.eventtrader.processor.GrowlNotification").getMethod("autoRegister")
                    .invoke(null);
        } catch (Exception e) {
            log.warn("Growl not found, cannot autoRegister notifications: {}", e.getMessage());
        }

        Map<String, Runnable> runnables = BeanFactoryUtils.beansOfTypeIncludingAncestors(context,
                Runnable.class);
        List<Thread> threads = new ArrayList<Thread>(runnables.size());
        for (final Map.Entry<String, Runnable> runner : runnables.entrySet()) {
            final Thread th = new Thread(runner.getValue(), runner.getKey());
            threads.add(th);
            th.start();
        }

        // close spring context on JVM shutdown
        // this causes all @PreDestroy methods in the runnables to be called to allow for
        // them to shutdown gracefully
        context.registerShutdownHook();

        // wait for launched threads to finish before cleaning up beans
        for (Thread th : threads) {
            try {
                th.join();
            } catch (InterruptedException e) {
            }
        }
    }
}

From source file:PriorityCompete.java

public static void main(String[] args) {
    Runnable r = new Runnable() {
        public void run() {
            System.out.println("Run without using yield()");
            System.out.println("=========================");
            runSet(false);/*from   w w  w. j a va 2 s . c  o m*/

            System.out.println();
            System.out.println("Run using yield()");
            System.out.println("=================");
            runSet(true);
        }
    };

    Thread t = new Thread(r, "Priority");
    t.setPriority(Thread.MAX_PRIORITY - 1);
    t.start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Runner t = new Runner();
    Thread t1 = new Thread(t);
    for (int i = 0; i < 50; i++) {
        t.queue.add(i);//from  w ww.  ja v a 2  s  .  co  m
    }
    System.out.println(("Number of items in queue: " + t.queue.size()));
    t1.start();
    Thread.sleep(1000);
    t1.interrupt();
    t1.join();
    System.out.println(("Number of items in queue: " + t.queue.size()));
    System.out.println(("Joined t1. Finished"));
}

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();
    t2.start();//from   www. ja  va 2s.com
    t3.start();
    t4.start();
    System.out.println("name is:" + NikeySecurity.isSignedIn());
}

From source file:j8583.example.Client.java

public static void main(String[] args) throws Exception {
    Random rng = new Random(System.currentTimeMillis());
    log.debug("Reading config");
    mfact = ConfigParser.createFromClasspathConfig("j8583/example/config.xml");
    mfact.setAssignDate(true);/*  ww  w  . ja va 2  s .co  m*/
    mfact.setTraceNumberGenerator(new SimpleTraceGenerator((int) (System.currentTimeMillis() % 10000)));
    System.err.println("Connecting to server");
    Socket sock = new Socket("localhost", 9999);
    // Send 10 messages, then wait for the responses
    Client client = new Client(sock);
    Thread reader = new Thread(client, "j8583-client");
    reader.start();
    for (int i = 0; i < 10; i++) {
        IsoMessage req = mfact.newMessage(0x200);
        req.setValue(4, amounts[rng.nextInt(amounts.length)], IsoType.AMOUNT, 0);
        req.setValue(12, req.getObjectValue(7), IsoType.TIME, 0);
        req.setValue(13, req.getObjectValue(7), IsoType.DATE4, 0);
        req.setValue(15, req.getObjectValue(7), IsoType.DATE4, 0);
        req.setValue(17, req.getObjectValue(7), IsoType.DATE4, 0);
        req.setValue(37, System.currentTimeMillis() % 1000000, IsoType.NUMERIC, 12);
        req.setValue(41, data[rng.nextInt(data.length)], IsoType.ALPHA, 16);
        req.setValue(48, data[rng.nextInt(data.length)], IsoType.LLLVAR, 0);
        pending.put(req.getField(11).toString(), req);
        System.err.println(String.format("Sending request %s", req.getField(11)));
        req.write(sock.getOutputStream(), 2);
    }
    log.debug("Waiting for responses");
    while (pending.size() > 0 && sock.isConnected()) {
        Thread.sleep(500);
    }
    client.stop();
    reader.interrupt();
    log.debug("DONE.");
}

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();
    thread2.start();// www .j  a  v  a  2  s  .  com
    thread3.start();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Thread thread1 = new Thread(new TestThread(1));
    Thread thread2 = new Thread(new TestThread(2));

    thread1.setPriority(Thread.MAX_PRIORITY);
    thread2.setPriority(Thread.MIN_PRIORITY);

    thread1.start();
    thread2.start();/*from   ww  w .j  a v a  2  s .  com*/

    thread1.join();
    thread2.join();
}

From source file:ThreadDemo.java

public static void main(String args[]) {
    Thread currThread = Thread.currentThread();
    // thread created
    Thread t = new Thread(new ThreadDemo(), "java2s.com thread");

    System.out.println("current thread = " + currThread);
    System.out.println("thread created = " + t);
    // this will call run() function
    t.start();
}