-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadquestion.java
More file actions
31 lines (26 loc) · 1.04 KB
/
Copy paththreadquestion.java
File metadata and controls
31 lines (26 loc) · 1.04 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
class threadquestion extends Thread {
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ": " + i);
try {
Thread.sleep(500); // Sleep for 0.5 seconds
} catch (InterruptedException e) {
System.out.println(e);
}
}
}
public static void main(String args[]) {
threadquestion t1 = new threadquestion();
System.out.println("Name of thread 't1': " + t1.getName());
threadquestion t2 = new threadquestion();
System.out.println("Name of thread 't2': " + t2.getName());
// Setting names before starting threads
t1.setName("January 1st is Monday");
t2.setName("January 2nd is Tuesday");
// Starting threads after setting names
t1.start();
t2.start();
System.out.println("New name of thread 't1': " + t1.getName());
System.out.println("New name of thread 't2': " + t2.getName());
}
}