First Commit

This commit is contained in:
2023-09-19 22:07:16 +03:00
commit 5e4b81c90d
35 changed files with 1092 additions and 0 deletions

19
Lab1/build.gradle.kts Normal file
View File

@@ -0,0 +1,19 @@
plugins {
id("java")
}
group = "org.lumijiez"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
}
tasks.test {
useJUnitPlatform()
}

View File

@@ -0,0 +1,33 @@
package org.lumijiez;
public class Main {
public static void main(String[] args) {
// Printer class initialization, with an PrintHandler dependency injection
Printer out = new Printer(new PrintHandler());
// Pool handler, stores all threads and manipulates them
ThreadPoolHandler TH = new ThreadPoolHandler();
// Fills the threadpool with runnables
TH.addThread(new Thread(new OutputRunnable(out, "Hello world!", 1000)));
TH.addThread(new Thread(new OutputRunnable(out, "I love OOP!", 2000)));
// Starts the async printing threads
TH.startAll();
// Keep printing for 5 seconds
ThreadPoolHandler.safeSleep(5000);
// Add a thread while the others are running
TH.addThreadWhileRunning(new Thread(new OutputRunnable(out, "Added while running!", 500)));
// Keep printing for 5 seconds
ThreadPoolHandler.safeSleep(5000);
// Stop all running threads
TH.deleteAll();
out.print("Bye-bye!");
}
}

View File

@@ -0,0 +1,30 @@
package org.lumijiez;
public class OutputRunnable implements Runnable {
// Printer dependency
private final Printer printer;
private final String text;
private final int sleepInterval;
public OutputRunnable(Printer printer, String text, int sleepInterval) {
this.printer = printer;
this.text = text;
this.sleepInterval = sleepInterval;
}
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
printer.print(text);
Thread.sleep(sleepInterval);
} catch (InterruptedException e) {
// Thread was interrupted, exit the loop
Thread.currentThread().interrupt();
break;
}
}
}
}

View File

@@ -0,0 +1,12 @@
package org.lumijiez;
import org.lumijiez.interfaces.PrintHandlerImpl;
// Handler, to be injected as a dependency
public class PrintHandler implements PrintHandlerImpl {
@Override
public void output(String text) {
System.out.println(text);
}
}

View File

@@ -0,0 +1,20 @@
package org.lumijiez;
import org.lumijiez.interfaces.PrinterImpl;
public class Printer implements PrinterImpl {
// Dependency Injection
private final PrintHandler handler;
// Constructor
public Printer(PrintHandler handler) {
this.handler = handler;
}
// Dependency Consumer
@Override
public void print(String text) {
this.handler.output(text);
}
}

View File

@@ -0,0 +1,41 @@
package org.lumijiez;
import java.util.ArrayList;
public class ThreadPoolHandler {
private final ArrayList<Thread> threadPool = new ArrayList<>();
public static void safeSleep(int milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public void addThread(Thread thread) {
threadPool.add(thread);
}
public void addThreadWhileRunning(Thread thread) {
threadPool.add(thread);
thread.start();
}
public void startAll() {
for (Thread thread : threadPool) {
thread.start();
}
}
public void interruptAll() {
for (Thread thread : threadPool) {
thread.interrupt();
}
}
public void deleteAll() {
this.interruptAll();
threadPool.clear();
}
}

View File

@@ -0,0 +1,5 @@
package org.lumijiez.interfaces;
public interface PrintHandlerImpl {
void output(String text);
}

View File

@@ -0,0 +1,5 @@
package org.lumijiez.interfaces;
public interface PrinterImpl {
void print(String text);
}