Starting and Running Multiple Threads : Thread Starting « Thread « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Thread » Thread Starting 
7.2.5.Starting and Running Multiple Threads
class NameRunnable implements Runnable {
    public void run() {
        for (int x = 1; x <= 3; x++) {
            System.out.println("Run by "
                    + Thread.currentThread().getName()
                    ", x is " + x);
        }
    }
}
public class MainClass {
    public static void main(String [] args) {
        // Make one Runnable
        NameRunnable nr = new NameRunnable();
        Thread one = new Thread(nr);
        Thread two = new Thread(nr);
        Thread three = new Thread(nr);

        one.setName("A");
        two.setName("B");
        three.setName("C");
        one.start();
        two.start();
        three.start();
    }
}
Run by A, x is 1
Run by A, x is 2
Run by A, x is 3
Run by B, x is 1
Run by B, x is 2
Run by B, x is 3
Run by C, x is 1
Run by C, x is 2
Run by C, x is 3
7.2.Thread Starting
7.2.1.To make a thread execute, you call its start() method.
7.2.2.To start a thread: construct an instance of MyThread and then invoke its start() method
7.2.3.Starting a Thread
7.2.4.Instantiates a thread and gives it a name, and then the name is printed out from the run() method
7.2.5.Starting and Running Multiple Threads
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.