Export (0) Print
Expand All
Expand Minimize

java.lang.Thread.run()

Visual Studio 2005

Runs a thread in its entirety. The calling thread is blocked until the called thread completes execution.


public void run()

// thread_run.jsl

public class Program implements Runnable
{
    public static void main(String[] args)
    {
        Program p = new Program();
        p.init();
    }

    public void init()
    {
        // Create a second thread and run it.
        Thread thr2 = new Thread(this);
        System.out.println("Running " +
                thr2.getName() + "...");
        thr2.run();

        // Create a third thread and run it.
        Thread thr3 = new Thread(this);
        System.out.println("Running " +
                thr3.getName() + "...");
        thr3.run();

        // Display the number of active threads.  This
        // count includes the main thread.
        System.out.println("Active threads after run = " +
            Thread.activeCount());
    }

    // Implements Runnable.run()
    public void run()
    {
        for (int i = 0; i < 100000000; i++)
        {
            // Just increment a counter.
            counter++;
        }

        System.out.println(Thread.currentThread().getName() +
            " has finished executing.");
    }

    private int counter = 0;
}

/*
Sample Output:
Running Thread-0...
main has finished executing.
Running Thread-1...
main has finished executing.
Active threads after run = 1
*/

Community Additions

ADD
Show:
© 2014 Microsoft