• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why can't I use sleep method?

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had created the Thread object but the program can't be compiled by javac!It says:Can't solve symbol.
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();
}
}
Add I find when I use sleep method and it can output few of results.But it can output many results
when I don't use the sleep method.Why? Thank you!
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't *resolve* symbol.

Your error is: C:\testRun.java:12: cannot resolve symbol
symbol : method sleep (double)

Your code:
thread.sleep((int)1000*Math.random());

Breaks down to Math.random() returns a double, multiply that by the result of casting 1000 to an integer. Which gives you a double. Which is why javac cannot resolve the method sleep(double).


And what does this have to do with JSP?
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for your help! I want to test the Thread class only and don't want to insert it to jsp.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic