Java Thread run() methodThe 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. ReturnExample 1: call the run() method using the start() methodTest it NowOutput: Thread is running... Example 2: call the run() method using the run() method itselfTest it NowOutput: running... In this case, it goes to the current call stack rather than at the beginning of a new call stack. Example 3: call the run() method more than one timeTest it NowOutput: 1 2 3 4 5 1 2 3 4 5 In the above Example 3, there is no content switching because here t1 and t2 is treated as a normal object not thread object. Next TopicMultithreading in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India