
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What will happen if we directly call the run() method in Java?
A direct call of a Thread object's run() method does not start a separate thread and it can be executed within the current thread. To execute Runnable.run from within a separate thread, do one of the following
- Construct a thread using the Runnable object and call start() method on the Thread.
- Define a subclass of a Thread object and override the definition of its run() method. Then construct an instance of this subclass and call start() method on that instance directly.
Example
public class ThreadRunMethodTest { public static void main(String args[]) { MyThread runnable = new MyThread(); runnable.run(); // Call to run() method does not start a separate thread System.out.println("Main Thread"); } } class MyThread extends Thread { public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("Child Thread interrupted."); } System.out.println("Child Thread"); } }
In the above example, the main thread, ThreadRunMethodTest, calls the child thread, MyThread, using the run() method. This causes the child thread to run to completion before the rest of the main thread is executed, so that "Child Thread" is printed before "Main Thread".
Output
Child Thread Main Thread
- Related Articles
- Can we call run() method directly instead of start() in Java
- Can we call a constructor directly from a method in java?
- What would happen if we run SELECT WHERE columnName = zero in MySQL?
- What will happen when we try to override final method of the superclass in Java?
- Call the run() method of the Timer Task in Java
- What will happen, if we did not have cartilage in our body?
- Can we synchronize a run() method in Java?
- How can we call the invokeLater() method in Java?\n
- If our joints will not move what will happen?
- How do we call a Java method recursively?
- What will happen if we have set UNIQUE and multiple insertion with duplicate values
- Can we call the wait() method without acquiring the lock in Java?
- What will happen if we pull the yarn from a torn pair of socks? Why so?
- Can we call Superclass’s static method from subclass in Java?
- What will happen if there are no platelets in the blood?

Advertisements