-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTask.java
More file actions
30 lines (26 loc) · 790 Bytes
/
Copy pathMyTask.java
File metadata and controls
30 lines (26 loc) · 790 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
public class MyTask implements Runnable {
public void run() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("Doing task: " + i);
Thread.sleep(1000); // Simulating work by sleeping
}
} catch (InterruptedException e) {
System.out.println("Thread was interrupted");
}
}
}
public class Interrupt {
public static void main(String[] args) {
Thread myThread = new Thread(new MyTask());
myThread.start();
try {
Thread.sleep(3000); // Main thread sleeps for 3 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
myThread.interrupt(); // Interrupting the myThread
}
}
{
}