First Commit
This commit is contained in:
33
Lab1/src/main/java/org/lumijiez/Main.java
Normal file
33
Lab1/src/main/java/org/lumijiez/Main.java
Normal 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!");
|
||||
}
|
||||
}
|
||||
30
Lab1/src/main/java/org/lumijiez/OutputRunnable.java
Normal file
30
Lab1/src/main/java/org/lumijiez/OutputRunnable.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
Lab1/src/main/java/org/lumijiez/PrintHandler.java
Normal file
12
Lab1/src/main/java/org/lumijiez/PrintHandler.java
Normal 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);
|
||||
}
|
||||
}
|
||||
20
Lab1/src/main/java/org/lumijiez/Printer.java
Normal file
20
Lab1/src/main/java/org/lumijiez/Printer.java
Normal 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);
|
||||
}
|
||||
}
|
||||
41
Lab1/src/main/java/org/lumijiez/ThreadPoolHandler.java
Normal file
41
Lab1/src/main/java/org/lumijiez/ThreadPoolHandler.java
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.lumijiez.interfaces;
|
||||
|
||||
public interface PrintHandlerImpl {
|
||||
void output(String text);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package org.lumijiez.interfaces;
|
||||
|
||||
public interface PrinterImpl {
|
||||
void print(String text);
|
||||
}
|
||||
Reference in New Issue
Block a user