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

public static void main(String[] args) {
    try {/*from  ww  w  .ja  va  2s .  c  o m*/
        Runnable r = new Runnable() {
            public void run() {
                print("getNextSerialNum()=" + getNextSerialNum());
            }
        };

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

        Thread.sleep(1500);

        Thread threadB = new Thread(r, "threadB");
        threadB.start();

        Thread.sleep(500);

        Thread threadC = new Thread(r, "threadC");
        threadC.start();

        Thread.sleep(2500);

        Thread threadD = new Thread(r, "threadD");
        threadD.start();
    } catch (InterruptedException x) {
        // ignore
    }
}

From source file:OnlyOneInMethod.java

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

    Runnable runA = new Runnable() {
        public void run() {
            ooim.doStuff(3);//from   w  w  w . j  av  a 2s.c o m
        }
    };

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

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

    Runnable runB = new Runnable() {
        public void run() {
            ooim.doStuff(7);
        }
    };

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

From source file:EarlyReturn.java

public static void main(String[] args) {
    try {/*w  ww . j av  a 2s  .c o m*/
        final EarlyReturn er = new EarlyReturn(0);

        Runnable r = new Runnable() {
            public void run() {
                try {
                    Thread.sleep(1500);
                    er.setValue(2);
                    Thread.sleep(500);
                    er.setValue(3);
                    Thread.sleep(500);
                    er.setValue(4);
                } catch (Exception x) {
                    x.printStackTrace();
                }
            }
        };

        Thread t = new Thread(r);
        t.start();

        System.out.println("waitUntilAtLeast(5, 3000)");
        long startTime = System.currentTimeMillis();
        boolean retVal = er.waitUntilAtLeast(5, 3000);
        long elapsedTime = System.currentTimeMillis() - startTime;

        System.out.println(elapsedTime + " ms, retVal=" + retVal);
    } catch (InterruptedException ix) {
        ix.printStackTrace();
    }
}

From source file:trendulo.ingest.Ingest.java

/**
 * @param args// ww w . j  a va2  s . c  o  m
 */
public static void main(String[] args) {

    final Logger log = Logger.getLogger(Ingest.class);

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    // Start the N-Gram Source
    final TemporalNGramSource nGramSource = (TemporalNGramSource) context.getBean("nGramSource");
    Thread nGramSourceThread = new Thread(nGramSource);
    nGramSourceThread.start();

    // Start the N-Gram Ingester that will take from the source
    final TemporalNGramIngester nGramIngester = (TemporalNGramIngester) context.getBean("nGramIngester");
    Thread nGramIngesterThread = new Thread(nGramIngester);
    nGramIngesterThread.start();

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            log.info("Shutting Down Ingest...");
            nGramSource.shutdown();
            nGramIngester.shutdown();
        }
    });
}

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);/*  ww  w  .j  a  v  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:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Stepping Progress");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JProgressBar aJProgressBar = new JProgressBar(0, 50);
    aJProgressBar.setStringPainted(true);

    final JButton aJButton = new JButton("Start");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            aJButton.setEnabled(false);/*from w ww . j a  va 2  s .  c o  m*/
            Thread stepper = new BarThread(aJProgressBar);
            stepper.start();
        }
    };
    aJButton.addActionListener(actionListener);
    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.add(aJButton, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:BarThread.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Stepping Progress");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
    aJProgressBar.setStringPainted(true);

    final JButton aJButton = new JButton("Start");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            aJButton.setEnabled(false);//from  w  w  w .  ja v  a 2 s . c  o m
            Thread stepper = new BarThread(aJProgressBar);
            stepper.start();
        }
    };
    aJButton.addActionListener(actionListener);
    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.add(aJButton, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:BarThread.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JProgressBar aJProgressBar = new JProgressBar(JProgressBar.VERTICAL);
    aJProgressBar.setStringPainted(true);

    JButton aJButton = new JButton("Start");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            aJButton.setEnabled(false);/*from  ww  w  . j a  va 2 s .  c o m*/
            Thread stepper = new BarThread(aJProgressBar);
            stepper.start();
        }
    };
    aJButton.addActionListener(actionListener);
    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.add(aJButton, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:com.eaio.uuid.UUIDPerformance.java

public static void main(String[] args) {

    Thread[] threads = new Thread[Runtime.getRuntime().availableProcessors()];

    for (int i = 0; i < threads.length; ++i) {
        threads[i] = new Thread(new UUIDRunnable(count / threads.length));
    }/*from   w  ww.j  a v a  2  s  .  com*/

    StopWatch watch = new StopWatch();
    watch.start();

    for (Thread t : threads) {
        t.start();
    }

    for (Thread t : threads) {
        try {
            t.join();
        } catch (InterruptedException e) {
            // Moo
        }
    }

    watch.stop();
    System.out.println(watch.getTime());
}

From source file:BarThread.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JProgressBar aJProgressBar = new JProgressBar(0, 50);
    aJProgressBar.setStringPainted(true);

    JButton aJButton = new JButton("Start");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            aJButton.setEnabled(false);/* w w  w.  java 2 s  .  c o m*/
            Thread stepper = new BarThread(aJProgressBar);
            stepper.start();
        }
    };
    aJButton.addActionListener(actionListener);
    frame.add(aJProgressBar, BorderLayout.NORTH);
    frame.add(aJButton, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}