mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 20:40:11 +00:00
Adds code for threads.
This commit is contained in:
parent
b1f7d94e53
commit
8c24d0345a
@ -4,10 +4,20 @@ package com.scaler;
|
||||
* Hello world!
|
||||
*
|
||||
*/
|
||||
public class App
|
||||
{
|
||||
public static void main( String[] args )
|
||||
{
|
||||
System.out.println( "Hello World!" );
|
||||
public class App {
|
||||
|
||||
public static void print() {
|
||||
System.out.println("Hello World! printed by: " + Thread.currentThread().getName());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
print();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
TextPrinter textPrinter = new TextPrinter(String.valueOf(i));
|
||||
Thread thread = new Thread(textPrinter);
|
||||
thread.start();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
14
os/code/os/src/main/java/com/scaler/TextPrinter.java
Normal file
14
os/code/os/src/main/java/com/scaler/TextPrinter.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.scaler;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
public class TextPrinter implements Runnable {
|
||||
|
||||
private String text;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(text + " printed by: " + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
@ -10,27 +10,39 @@ import com.scaler.scheduling.models.ScheduledProcess;
|
||||
public class FirstComeFirstServe {
|
||||
|
||||
public List<ScheduledProcess> schedule(List<IncomingProcess> processes) {
|
||||
|
||||
// Get a sorted list of processes
|
||||
Queue<IncomingProcess> queue = prepareQueue(processes);
|
||||
List<ScheduledProcess> scheduledProcesses = new LinkedList<>();
|
||||
|
||||
int time = 0;
|
||||
int index = 0;
|
||||
|
||||
// Check if there are any processes in the queue
|
||||
while (!queue.isEmpty()) {
|
||||
IncomingProcess process = queue.poll();
|
||||
|
||||
// Increment elapsed time by the burst time of the process
|
||||
time += process.getBurstTime();
|
||||
index += 1;
|
||||
|
||||
process.setCompletedAt(time);
|
||||
|
||||
// Add the process to the list of scheduled processes
|
||||
scheduledProcesses.add(new ScheduledProcess(index, process));
|
||||
}
|
||||
|
||||
return scheduledProcesses;
|
||||
}
|
||||
|
||||
// When ever we get a new process, we add it to the queue
|
||||
private Queue<IncomingProcess> prepareQueue(List<IncomingProcess> processes) {
|
||||
// Simulate a queue using a linked list
|
||||
Queue<IncomingProcess> queue = new LinkedList<>();
|
||||
|
||||
List<IncomingProcess> sortedProcesses = new LinkedList<>(processes);
|
||||
|
||||
// Sort the processes by arrival time to simulate FCFS
|
||||
sortedProcesses.sort((p1, p2) -> p1.getArrivalTime() - p2.getArrivalTime());
|
||||
|
||||
for (IncomingProcess process : sortedProcesses) {
|
||||
|
@ -1,8 +1,6 @@
|
||||
package com.scaler.scheduling.models;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
|
Loading…
x
Reference in New Issue
Block a user