-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternatePrinting.java
More file actions
101 lines (93 loc) · 3.47 KB
/
Copy pathAlternatePrinting.java
File metadata and controls
101 lines (93 loc) · 3.47 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package threads;
public class AlternatePrinting {
private static final Object lock = new Object();
// private static boolean isNumberTurn = true; // Flag to control the
// alternation
// public static void main(String[] args) {
// // Thread to print numbers (1, 2, 3)
// Thread numberThread = new Thread(() -> {
// for (int i = 1; i <= 3; i++) {
// synchronized (lock) {
// while (!isNumberTurn) { // Wait if it's not this thread's turn
// try {
// lock.wait();
// } catch (InterruptedException e) {
// Thread.currentThread().interrupt();
// }
// }
// System.out.print(i + " "); // Print the number
// isNumberTurn = false; // Set flag for the letter thread
// lock.notify(); // Notify the other thread
// }
// }
// });
// // Thread to print letters (A, B, C)
// Thread letterThread = new Thread(() -> {
// char[] letters = { 'A', 'B', 'C' };
// for (char letter : letters) {
// synchronized (lock) {
// while (isNumberTurn) { // Wait if it's not this thread's turn
// try {
// lock.wait();
// } catch (InterruptedException e) {
// Thread.currentThread().interrupt();
// }
// }
// System.out.print(letter + " "); // Print the letter
// isNumberTurn = true; // Set flag for the number thread
// lock.notify(); // Notify the other thread
// }
// }
// });
private static boolean isLetterTurn = true; // Flag to control the alternation
public static void main(String[] args) {
// Thread to print letters (A, B, C)
Thread letterThread = new Thread(() -> {
char[] letters = { 'A', 'B', 'C' };
for (char letter : letters) {
synchronized (lock) {
while (!isLetterTurn) { // Wait if it's not this thread's turn
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.print(letter + " ");
isLetterTurn = false; // Set the flag for the number thread
lock.notify(); // Notify the other thread
}
}
});
// Thread to print numbers (1, 2, 3)
Thread numberThread = new Thread(() -> {
int[] numbers = { 1, 2, 3 };
for (int number : numbers) {
synchronized (lock) {
while (isLetterTurn) { // Wait if it's not this thread's turn
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.print(number + " ");
isLetterTurn = true; // Set the flag for the letter thread
lock.notify(); // Notify the other thread
}
}
});
// Start both threads
numberThread.start();
letterThread.start();
}
/*
* • Synchronization: Ensures threads do not interfere with each other and print
* in the correct order.
*
* • Wait and Notify: Provides a way to coordinate between the two threads.
*
* • Thread Safety: The shared flag (isLetterTurn) and lock object ensure
* thread-safe access and synchronization.
*/
}