aspose file tools*
The moose likes JSP and the fly likes How to use the Runnable?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to use the Runnable??" Watch "How to use the Runnable??" New topic
Author

How to use the Runnable??

Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
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!


When I face C# and Java I choose Java.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
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
 
Consider Paul's rocket mass heater.
 
subject: How to use the Runnable??