-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunnableThread.java
More file actions
38 lines (32 loc) · 942 Bytes
/
RunnableThread.java
File metadata and controls
38 lines (32 loc) · 942 Bytes
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
38
class MyThreadRunnable1 implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("Thread1 is running");
}
}
}
class MyThreadRunnable2 implements Runnable {
@Override
public void run() {
while (true) {
System.out.println("Thread2 is running");
}
}
}
public class RunnableThread {
public static void main(String[] args) {
// ? Bullet & Gun Analogy:
// Bullet (runnable) by itself cannot shoot
// We need a gun (thread) to shoot the bullet(runnable)
MyThreadRunnable1 bullet1 = new MyThreadRunnable1();
Thread gun1 = new Thread(bullet1);
MyThreadRunnable2 bullet2 = new MyThreadRunnable2();
Thread gun2 = new Thread(bullet2);
gun1.start();
gun2.start();
while (true) {
System.out.println("Main Thread is Running.");
}
}
}