-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadCreationExample.java
More file actions
39 lines (31 loc) · 1 KB
/
ThreadCreationExample.java
File metadata and controls
39 lines (31 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* Day 24 - Multithreading: Creating Threads
*/
public class ThreadCreationExample {
// Method 1: Extend Thread
static class MyThread extends Thread {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("Thread running: " + i);
}
}
}
// Method 2: Implement Runnable
static class MyRunnable implements Runnable {
public void run() {
for (int i = 0; i < 3; i++) {
System.out.println("Runnable running: " + i);
}
}
}
public static void main(String[] args) {
System.out.println("=== Multithreading ===\n");
System.out.println("--- Method 1: Extend Thread ---");
MyThread thread1 = new MyThread();
thread1.start();
System.out.println("\n--- Method 2: Implement Runnable ---");
Thread thread2 = new Thread(new MyRunnable());
thread2.start();
System.out.println("\nMain thread continues");
}
}