run Method
Collapse the table of content
Expand the table of content
The document is archived and information here might be outdated

Runnable.run Method

Visual Studio 2005

An object implementing Runnable must define the run method. When the object is used to create a thread the method is called and executed in a separate thread.

Package: java.lang

Assembly: vjslib (in vjslib.dll)


public abstract void run();

In this example, MyClass implements the Runnable interface and defines the run method. In main(), two threads are started. Each one displays a massage and then sleeps for 2 milliseconds.

When you run the program, it keeps printing "Hi..." and "Bye!" until you terminate the program execution.

// Runnable.run example

public class MyClass implements Runnable
{
    String myString;
    int delayTime;

    MyClass(String myString, int delayTime)
    {
        this.myString = myString;
        this.delayTime = delayTime;
    }
      
    public void run()
    {
        try
        {
            for (;;)
            {
                System.out.println(myString);
                Thread.sleep(delayTime);
            }
        }
        catch (InterruptedException e)
        {
            // ...
        }
    }
            
    public static void main(String[] args)
    {
        Runnable proc1 = new MyClass("Hi... ",2);
        Runnable proc2 = new MyClass("Bye! ",2);
        
        new Thread(proc1,"thread1").start();
        new Thread(proc2,"thread2").start();
    } 
}
Show:
© 2016 Microsoft