-
Notifications
You must be signed in to change notification settings - Fork 0
/
runnablethread.java
38 lines (34 loc) · 1.13 KB
/
runnablethread.java
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 MyRunnable implements Runnable {
private String threadName;
public MyRunnable(String threadName) {
this.threadName = threadName;
}
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
System.out.println("Thread " + threadName + ": Count - " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
System.out.println("Thread " + threadName + " interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
}
public class RunnableThreadDemo {
public static void main(String[] args) {
System.out.println("Main thread starting.");
Thread thread1 = new Thread(new MyRunnable("Thread 1"));
Thread thread2 = new Thread(new MyRunnable("Thread 2"));
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread ending.");
}
}