cancel
Showing results for 
Search instead for 
Did you mean: 

Thread class in java

Former Member
0 Kudos

Hello ! i a begginner in java..kindly help me in solving these questions.

wat'll happen when u compile and run this code?

public class Bground extends Thread{

public static void main(String argv[]){

Bground b=new Bground();

b.run();

}

public void start(){

for(int i= 0;i<10;i++){

System.out.println("Value of i=" + i);

}

}

}

Options----

1. A compile time error indicating that no run method is defined for the Thread class

2.A run time error indicating that no run method is defined for the Thread class

3.Clean compile and at run time the values 0 to 9 are printed out

4. Clean compile but no output at runtime

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi

The Answer is

4. Clean compile but no output at runtime

Kind regads

Mukesh

Answers (2)

Answers (2)

Former Member
0 Kudos

/*

Hai enclosed is the code for your query.

option 4 is correct.

*/

public class Bground10 extends Thread{

public static void main(String args[ ]){

Bground10 b =new Bground10();

//b.run();clean compilation no out put because run method cannot be

//called by start

b.start(); //prints Value of i is 0 to Value of i is 9

// as start method is called.

//System.out.println("Hai Hello");

}

public void start(){

for(int i=0;i<10;i++){

System.out.println("value of i="+i);

}

}

}

SRudra
Contributor
0 Kudos

Payal,

For getting some output for the program u have posted , do override the default run method. This method is from tyhe Thread class. It will give some out. For trial, u can put some SOP (System.out.println) statement in a loop and check it out.

Cheers!!!!

Sukant Rudra

Note: Helpful answer, plz donate some points generously.

Former Member
0 Kudos

hi,

I executed the code.

public class Bground extends Thread{
	public static void main(String argv[]){
		Bground b=new Bground();
		b.run();
	}
	public void start(){
		for(int i= 0;i<10;i++){
		System.out.println("Value of i=" + i);
		}
	}
}

it complied and there was no output at runtime.

Regards,

Richa.