added ex Dbis
This commit is contained in:
parent
bd44a92834
commit
e42def59f5
36
src/exercicses/ex_d_bis/StepThread.java
Normal file
36
src/exercicses/ex_d_bis/StepThread.java
Normal 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);
|
||||
}
|
||||
}
|
30
src/exercicses/ex_d_bis/TestStepThreads.java
Normal file
30
src/exercicses/ex_d_bis/TestStepThreads.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user