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: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);/*  w  ww .  j  a  v a 2 s  .c om*/
        }
    };

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

public static void main(String[] args) throws Exception {
    Runnable myRunnable = new Runnable() {
        @Override//from   ww  w .ja  v a  2 s.c  om
        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:Volatile.java

public static void main(String[] args) {
    try {// w  w  w . j ava2 s .  com
        Volatile vol = new Volatile();

        // slight pause to let some time elapse
        Thread.sleep(100);

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

        // slight pause to allow run() to go first
        Thread.sleep(100);

        vol.workMethod();
    } catch (InterruptedException x) {
        System.err.println("one of the sleeps was interrupted");
    }
}

From source file:PipedCharacters.java

public static void main(String[] args) {
    try {//  w ww  . j  a v  a 2  s  . 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:AnimationTester.java

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

    ArrayComponent panel = new ArrayComponent();
    frame.add(panel, BorderLayout.CENTER);

    frame.setSize(800, 300);//from  w  w w  .java 2 s. c o  m
    frame.setVisible(true);

    Double[] values = new Double[100];
    for (int i = 0; i < values.length; i++)
        values[i] = Math.random() * panel.getHeight();

    final Sorter sorter = new Sorter(values, panel);

    Thread sorterThread = new Thread(sorter);
    sorterThread.start();
}

From source file:PipedBytes.java

public static void main(String[] args) {
    try {/*from  w  w w.  j a  v a 2  s  .  co  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:AlternateSuspendResume.java

public static void main(String[] args) {
    AlternateSuspendResume asr = new AlternateSuspendResume();

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

    try {//from   w  ww  .  j  a v a2s  .  c o m
        Thread.sleep(1000);
    } catch (InterruptedException x) {
    }

    for (int i = 0; i < 10; i++) {
        asr.suspendRequest();

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

        System.out.println("dsr.areValuesEqual()=" + asr.areValuesEqual());

        asr.resumeRequest();

        try {
            Thread.sleep((long) (Math.random() * 2000.0));
        } catch (InterruptedException x) {
        }
    }
    System.exit(0);
}

From source file:PrepareProduction.java

public static void main(String[] args) throws Exception {
    List q = Collections.synchronizedList(new LinkedList<String>());
    Thread p1 = new Thread(new PrepareProduction(q));
    Thread c1 = new Thread(new DoProduction(q));
    p1.start();
    c1.start();// w w  w .ja  v a 2  s.  com
    p1.join();
    c1.join();
}

From source file:ProgressBarStep.java

public static void main(String args[]) {
    // Initialize
    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);/*  w  w w.  jav a 2s .  c  o m*/
            Thread stepper = new BarThread(aJProgressBar);
            stepper.start();
        }
    };

    aJButton.addActionListener(actionListener);

    String title = (args.length == 0 ? "Stepping Progress" : args[0]);
    JFrame theFrame = new JFrame(title);
    theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = theFrame.getContentPane();
    contentPane.add(aJProgressBar, BorderLayout.NORTH);
    contentPane.add(aJButton, BorderLayout.SOUTH);
    theFrame.setSize(300, 200);
    theFrame.setVisible(true);
}

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);//w ww . j av  a 2 s. com
    st.start();
    rt.start();
    try {
        while (s.isValid) {
            Thread.sleep(100);
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}