Interview Questions in Multithreading

Interview Questions in Multithreading


Runnable Interface in Java:

By Implementing `Runnable` Interface


class MyRunnable implements Runnable {
public void run() {System.out.println("Runnable thread running");}

public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable());
t1.start();
}
}
```
---

Add: Nanotechnology Solution That Supports Healthy Nails And Skin

# Difference Between `start()` and `run()`

| Method | Purpose |
| --------- | ------------------------------------------------- |
| `start()` | Creates a new thread and calls `run()` internally |
| `run()` | Executes like a normal method |

Example:

```java
t1.start(); // Multithreading
t1.run(); // Normal method call
```
---

# Thread Methods

| Method | Description |
| ----------- | -------------------------------- |
| `start()` | Starts thread |
| `sleep()` | Pauses thread |
| `join()` | Waits for thread completion |
| `yield()` | Gives chance to other threads |
| `isAlive()` | Checks whether thread is running |
---

Add: The Unique Dental Solution That Supports Healthy Teeth & Oxygenates Gums

# Example Using `sleep()`

```java
class SleepDemo extends Thread {
public void run() {
for(int i = 1; i <= 5; i++) {
System.out.println(i);

try {
Thread.sleep(1000);
} catch(Exception e) {System.out.println(e);}
}
}

public static void main(String[] args) {
SleepDemo t1 = new SleepDemo();
t1.start();
}
}
```
---

Add: iGenics is a Premium Eyesupport without Operation


Wishing you all the best,
http://www.seeyourneeds.in