Java Thread run() method
The run() method
of thread class is called if the thread was constructed using a separate
Runnable object otherwise this method does nothing and returns. When the run()
method calls, the code specified in the run() method is executed. You can call
the run() method multiple times.
The
run() method can be called using the start() method or by calling the run()
method itself. But when you use run() method for calling itself, it creates
problems.
Fig: Thread Run Method |
Return
It does not return any value.
Example:
call the run() method using the
start() method
public class RunExp1 implements Runnable
{
public void run()
{
System.out.println("Thread is running...");
}
public static void main(String args[])
{
RunExp1 r1=new RunExp1();
Thread t1 =new Thread(r1);
// this will call run() method
t1.start();
}
}
Output:
Thread is running...
No comments:
Post a Comment