• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use the Runnable??

 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why I must create a Thread object when I use the run method? Despite I use the Runnable implements?
my code is:
public class testRun implements Runnable{
String id;
Thread thread=new Thread();
public testRun(String str){
id=str;
}
public void run(){
for(int i=0;i<1000;i++){
for(int j=1;j<100;j++);
try{
thread.sleep((int)(1000*Math.random()));
}catch(InterruptedException e){}
System.out.println(id+"is running");
}
}
}
class testC{
public static void main(String args[]){
testRun dog=new testRun("hello");
testRun cat=new testRun("JavaBean");
Thread t1=new Thread(dog);
Thread t2=new Thread(cat);
t1.start();
t2.start();
}
}
////////////////////////////////////////////
when I delete the :
Thread t1=new Thread(dog);
Thread t2=new Thread(cat);
use dog.start();
cat.start();
It can't run! saying:can't solve the dog?Why I must create a Thread Object? Thank you!
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is very basic Java. You should study the free Java tutorial available at java.sun.com or any of the other free resources. This forum exists for JavaServer Pages questions.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic