- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Which method must be implemented by all threads in Java?

While creating a thread class we must override the **run()** method of the **Thread** class. This method provides an entry point for the thread and you will put your complete business logic inside this method.

## Example

class ThreadDemo extends Thread { private String threadName; ThreadDemo( String name) { threadName = name; System.out.println("Creating " + threadName ); } public void run() { System.out.println("Running " + threadName ); try { for(int i = 4; i > 0; i--) { System.out.println("Thread: " + threadName + ", " + i); // Let the thread sleep for a while. Thread.sleep(50); } } catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } } public class TestThread { public static void main(String args[]) { ThreadDemo T1 = new ThreadDemo( "Thread-1"); T1.start(); ThreadDemo T2 = new ThreadDemo( "Thread-2"); T2.start(); } }

## Output

Creating Thread-1 Creating Thread-2 Running Thread-1 Thread: Thread-1, 4 Running Thread-2 Thread: Thread-2, 4 Thread: Thread-1, 3 Thread: Thread-2, 3 Thread: Thread-1, 2 Thread: Thread-2, 2 Thread: Thread-1, 1 Thread: Thread-2, 1 Thread Thread-1 exiting. Thread Thread-2 exiting.

- Related Articles
- Why main() method must be static in java?
- Difference Between Daemon Threads and User Threads In Java
- Joining Threads in Java
- Killing threads in Java
- Get all the interfaces implemented or inherited by the current Type in C#
- Which of the following statements are true?(a) If a number is divisible by 3, it must be divisible by 9.(b) If a number is divisible by 9, it must be divisible by 3.(c) A number is divisible by 18, if it is divisible by both 3 and 6.(d) If a number is divisible by 9 and 10 both, then it must be divisible by 90.(e) If two numbers are co-primes, at least one of them must be prime.(f) All numbers which are divisible by 4 must also be divisible by 8.(g) All numbers which are divisible by 8 must also be divisible by 4.(h) If a number exactly divides two numbers separately, it must exactly divide their sum.(i) If a number exactly divides the sum of two numbers, it must exactly divide the two numbers separately.
- Which is the smallest number by which 725 must be divided to make it a perfect cube?
- State whether True or FalseAll numbers which are divisible by 4 must also be divisible by 8.
- Find the smallest number by which 1.243 must be multiplied to obtain a perfect cube.
- What must be multiplied by 3 to get 24?
- What must be multiplied by 5 to get 45?
- Find the least number by which 8 must be multiplied to make it a perfect square.
- Find the least number by which 45 must be divided to make it a perfect square.
- Find the smallest number by which $53240$ must be multiplied to make it a perfect cube.
- Minimum and Maximum Priority Threads in Java

Advertisements