diff --git a/src/exercicses/ex_d_bis/StepThread.java b/src/exercicses/ex_d_bis/StepThread.java new file mode 100644 index 0000000..b1218c2 --- /dev/null +++ b/src/exercicses/ex_d_bis/StepThread.java @@ -0,0 +1,36 @@ +package exercicses.ex_d_bis; + +public class StepThread extends Thread { + private int i; + private String startMsg; + private String endMsg; + private long duration; + private Thread dependency; + + public StepThread(int i, String startMsg, String endMsg, long duration, Thread dependency) { + this.i = i; + this.startMsg = startMsg; + this.endMsg = endMsg; + this.duration = duration; + this.dependency = dependency; + } + + @Override + public void run() { + if (dependency != null) { + try { + dependency.join(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + + System.out.println("Stage " + i + ": " + startMsg); + try { + Thread.sleep(duration); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + System.out.println("Stage " + i + ": " + endMsg); + } +} diff --git a/src/exercicses/ex_d_bis/TestStepThreads.java b/src/exercicses/ex_d_bis/TestStepThreads.java new file mode 100644 index 0000000..ee267b3 --- /dev/null +++ b/src/exercicses/ex_d_bis/TestStepThreads.java @@ -0,0 +1,30 @@ +package exercicses.ex_d_bis; + +public class TestStepThreads { + public static void main(String[] args) { + Thread rawMatPrep = new StepThread( + 1, + "Preparing raw materials...", + "Completed raw materials preparation.", + 1000, + null + ); + Thread assembly = new StepThread( + 2, + "Assembling the product...", + "Completed product assembly.", + 2000, + rawMatPrep + ); + Thread qualityCtrl = new StepThread( + 3, + "Quality control...", + "Quality control completed.", + 500, + assembly + ); + rawMatPrep.start(); + assembly.start(); + qualityCtrl.start(); + } +}