Question: Explain why the thread listed below may never terminate, and re - write it so that it is guaranteed to do so . class T

Explain why the thread listed below may never terminate, and re-write it so that it is guaranteed to do so.
class T1 extends Thread{
private boolean go = true;
public void run(){
while(go){
try{
this.sleep(1000); //1000 milliseconds ==1 sec }
catch(InterruptedException e){
}
}}
public void terminate(){
go = false; }}

Step by Step Solution

3.44 Rating (154 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The reason why the original code may never terminate is because the go flag is not accessed wi... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!