-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadingExample.java
More file actions
32 lines (28 loc) · 903 Bytes
/
MultiThreadingExample.java
File metadata and controls
32 lines (28 loc) · 903 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
public class MultiThreadingExample {
public static void main(String[] args) {
// Create and start the first thread
Thread thread1 = new Thread(new MyRunnable("Thread 1"));
thread1.start();
// Create and start the second thread
Thread thread2 = new Thread(new MyRunnable("Thread 2"));
thread2.start();
}
}
class MyRunnable implements Runnable {
private String name;
public MyRunnable(String name) {
this.name = name;
}
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(name + ": Count " + i);
try {
Thread.sleep(1000); // Sleep for 1 second
} catch (InterruptedException e) {
System.err.println(name + " was interrupted.");
}
}
System.out.println(name + " has completed.");
}
}