added ex Dbis

This commit is contained in:
Louis Heredero 2024-12-10 14:00:04 +01:00
parent bd44a92834
commit e42def59f5
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 66 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}