Predict the output from the following program. Select all lines of output that are produced by this program. If there are computed values in an output line, you must select the line with the correct value as computed by the program.
public class WalkToSchool20 {
public static void main (String[] args) {
Pedestrian p1 = new Pedestrian ( 1 );
Pedestrian p2 = new Pedestrian ( 2 );
Pedestrian p3 = new Pedestrian ( 3 );
p1.start();
p2.start();
p3.start();
p2.walk (64);
p3.walk (16);
p1.walk (8);
p3.getTicket (45);
p2.takeDetour ();
p3.takeDetour ();
p2.getTicket (128);
p1.walk (4);
p2.walk (16);
p3.walk (96);
p2.walk (14);
p1.interrupt ();
p1.walk (75);
p3.stopWalking();
p2.stopWalking();
p1.stopWalking();
try {
p1.join();
p2.join();
p3.join();
}
catch (InterruptedException e) {}
System.out.println("Total time = " +
(p1.gettime() + p2.gettime() + p3.gettime()));
}
} // WalkToSchool20
class Pedestrian extends Thread {
private static final int DETOUR_DELAY = 48;
private int id;
private int time;
private boolean walking;
Pedestrian (int id) {
this.id = id;
this.time = 0;
this.walking = true;
}
public void walk (int t) {
time = time + id*t;
}
public void takeDetour () {
time = time + id * DETOUR_DELAY;
}
public void stopWalking () {
walking = false;
}
public void getTicket (int t) {
time = time + t;
}
public int gettime () {
return time;
}
public void run () {
while (walking) {
Thread.yield();
}
if (interrupted())
System.out.println ("Pedestrian " + id +
": did not complete the trip");
else
System.out.println ("Pedestrian " + id +
": walking time = " + time);
}
} // Pedestrian
Question 6 options:
Pedestrian 3: did not complete the trip |
|
Pedestrian 2: walking time = 384 |
|
Pedestrian 3: walking time = 525 |
|
Pedestrian 1: did not complete the trip |
|
Pedestrian 2: walking time = 412 |
|
Pedestrian 3: walking time = 445 |
|
Pedestrian 2: did not complete the trip |
|
Total time = 1024 |
|
Total time = 841 |
Please help due within 30 mins
The output will be
Pedestrian 3: walking time = 525
Pedestrian 1: did not complete the trip
Pedestrian 2: walking time = 412
Total time = 1024
because for p1 in the program we have p1.interrupt()This will be the output for the given program
when we have interrupt we are just displaying that the pedestrain did not complete the trip
remaining pedestrain 2 and 3 completes within 412 and 525
the order may be changed because of threads
i.e. pedestrain order will be changed because of parallel running of threads
but the time will be same
hope this is helpful
Please please upvote it helps me alot
Please dont downvote
Get Answers For Free
Most questions answered within 1 hours.