-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoad_Balancing.java
More file actions
119 lines (103 loc) · 4.62 KB
/
Load_Balancing.java
File metadata and controls
119 lines (103 loc) · 4.62 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* Name: Amey Mahendra Thakur
* Course: Distributed Computing Lab (CSL802)
* Roll No: 50 | Batch: B3
* Date of Experiment: March 11, 2022
* Repository: https://github.com/Amey-Thakur/DISTRIBUTED-COMPUTING-AND-DISTRIBUTED-COMPUTING-LAB
* Description: Experiment 8 - Implementation of a Static Load Balancing Algorithm using multithreading in Java.
* This simulation assumes a multi-processor system where tasks are distributed based on processor capacity (limits).
*/
import java.util.Scanner;
public class Load_Balancing {
// Shared state variables for thread communication
public static int l1; // Limit for Processor 1
public static int l2; // Limit for Processor 2
public static int n; // Total number of processes to be executed
public static int r1; // Remainder (overflow) from Processor 1
public static void main(String[] args) {
r1 = 0;
Scanner sc = new Scanner(System.in);
System.out.println("--- [STATIC LOAD BALANCING SIMULATOR] ---");
System.out.println("[*] Assumption: Two Processors in the system.");
// Input phase
System.out.print("Enter execution limit for Processor 1: ");
l1 = sc.nextInt();
System.out.print("Enter execution limit for Processor 2: ");
l2 = sc.nextInt();
System.out.print("Enter total number of processes to execute: ");
n = sc.nextInt();
// Task distribution using individual threads for each processor
Runnable r1Handler = new Processor1Handler();
Thread t1 = new Thread(r1Handler);
Runnable r2Handler = new Processor2Handler();
Thread t2 = new Thread(r2Handler);
// Start multi-threaded execution
t1.start();
t2.start();
// Close scanner
sc.close();
}
}
/**
* Handler for the first processor logic.
*/
class Processor1Handler implements Runnable {
public void run() {
System.out.println("\n[PROCESSOR 1] Status (Capacity: " + Load_Balancing.l1 + ")");
int limit = Load_Balancing.l1;
int total = Load_Balancing.n;
int remaining = 0;
if (total == 0) {
System.out.println(" -> No processes available for execution.");
} else {
if (limit > total) {
System.out.println(" -> State: Underloaded");
System.out.println(" -> Executed: " + total + " processes.");
} else if (limit == total) {
System.out.println(" -> State: Normal Load");
System.out.println(" -> Executed: " + total + " processes.");
System.out.println(" -> Result: All tasks completed at local site.");
} else {
System.out.println(" -> State: OVERLOADED");
System.out.println(" -> Executed: " + limit + " processes (Max Capacity).");
remaining = total - limit;
System.out.println(" -> Overflow: " + remaining + " processes forwarded to Processor 2.");
}
}
// Update shared remainder for Processor 2
Load_Balancing.r1 = remaining;
}
}
/**
* Handler for the second processor logic.
* Includes a brief sleep to ensure sequential logic demonstration.
*/
class Processor2Handler implements Runnable {
public void run() {
try {
// Simulated delay to ensure Processor 1 finishes calculation
Thread.sleep(2000);
} catch (InterruptedException e) {
System.err.println("Thread Interrupted: " + e.getMessage());
}
System.out.println("\n[PROCESSOR 2] Status (Capacity: " + Load_Balancing.l2 + ")");
int limit = Load_Balancing.l2;
int workload = Load_Balancing.r1; // Gets overflow from P1
if (workload == 0) {
System.out.println(" -> No overflow tasks received from Processor 1.");
} else {
if (limit > workload) {
System.out.println(" -> State: Underloaded");
System.out.println(" -> Executed: " + workload + " processes.");
} else if (limit == workload) {
System.out.println(" -> State: Normal Load");
System.out.println(" -> Executed: " + workload + " processes.");
} else {
System.out.println(" -> State: OVERLOADED");
System.out.println(" -> Executed: " + limit + " processes (Max Capacity).");
int finalRemainder = workload - limit;
System.out.println(" -> Critical: " + finalRemainder + " processes still queued (System Overloaded).");
}
}
}
}