forked from akash-coded/C133-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultithreading.java
More file actions
69 lines (56 loc) · 2.04 KB
/
Multithreading.java
File metadata and controls
69 lines (56 loc) · 2.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;
class MyThread1 extends Thread {
@Override
public void run() {
// System.out.println("MyThread1 run() function");
// Scanner sc = new Scanner(System.in);
// int x = sc.nextInt(); // Blocking I/O operation
for (int i = 0; i < 1; i++) {
System.out.println("MyThread1 run() function");
}
}
}
class MyThread2 implements Runnable {
public void run() {
Thread.currentThread().setPriority(6);
System.out.println(Thread.currentThread().getPriority());
Thread.currentThread().setPriority(7);
System.out.println(Thread.currentThread().getPriority());
for (int i = 0; i < 3; i++) {
System.out.println("MyThread2 run() function");
}
Thread.currentThread().setPriority(2);
System.out.println("MyThread2 run() function");
}
}
public class Multithreading {
public static void main(String[] args) {
MyThread1 t1 = new MyThread1(); // Newborn state
t1.setPriority(2);
t1.start(); // Ready state
// Scanner sc = new Scanner(System.in);
// int x = sc.nextInt(); // Blocking I/O operation
MyThread2 t = new MyThread2();
Thread t2 = new Thread(t); // Newborn state
t2.setPriority(8);
t2.start(); // Ready state
System.out.println(t1.getName());
System.out.println(t2.getName());
t1.setName("Thread of MyThread1");
t2.setName("Thread of MyThread2");
System.out.println(t1.getName());
System.out.println(t2.getName());
System.out.println(Thread.currentThread().getName());
System.out.println(t1.getId());
System.out.println(t2.getId());
System.out.println(Thread.currentThread().getId());
// try {
// Thread.sleep(10);
// t1.join();
// t2.join(1000);
// } catch (InterruptedException e) {
// System.out.println(e);
// }
System.out.println("Main function terminating"); // Blocking I/O operation
}
}