java - Cannot find symbol: Thread.sleep (1000); -
java - Cannot find symbol: Thread.sleep (1000); -
this question has reply here:
java -the method sleep(int) undefined type thread 7 answersi want implement threads using runnable
interface.
i have next 3 classes:
firstthread.java
public class firstthread implements runnable { //this method executed when thread executed public void run() { //looping 1 10 display numbers 1 10 ( int i=1; i<=10; i++) { //displaying numbers thread system.out.println( "messag first thread : " +i); /*taking delay of 1 sec before displaying next number * * "thread.sleep(1000);" - when statement executed, * thread sleep 1000 milliseconds (1 second) * before executing next statement. * * since making thread sleep 1 second, * need handle "interruptedexception". our thread * may throw exception if interrupted while * sleeping. * */ seek { thread.sleep (1000); } grab (interruptedexception interruptedexception) { /*interrupted exception thrown when sleeping or waiting *thread interrupted. */ system.out.println( "first thread interrupted when sleeping" +interruptedexception); } } } }
secondthread.java:
public class secondthread implements runnable { //this method executed when thread executed public void run() { //looping 1 10 display numbers 1 10 ( int i=1; i<=10; i++) { system.out.println( "messag sec thread : " +i); /*taking delay of 1 sec before displaying next number * * "thread.sleep(1000);" - when statement executed, * thread sleep 1000 milliseconds (1 second) * before executing next statement. * * since making thread sleep 1 second, * need handle "interruptedexception". our thread * may throw exception if interrupted while * sleeping. */ seek { thread.sleep(1000); } grab (interruptedexception interruptedexception) { /*interrupted exception thrown when sleeping or waiting * thread interrupted. */ system.out.println( "second thread interrupted when sleeping" +interruptedexception); } } } }
threaddemo.java:
public class threaddemo { public static void main(string args[]) { //creating object of first thread firstthread firstthread = new firstthread(); //creating object of sec thread secondthread secondthread = new secondthread(); //starting first thread thread thread1 = new thread(firstthread); thread1.start(); //starting sec thread thread thread2 = new thread(secondthread); thread2.start(); } }
on compiling above programs, got next errors:
firstthread
class="lang-none prettyprint-override">shahjahan@shahjahan-aod270:~/documents/java$ javac firstthread.java firstthread.java:28: error: cannot find symbol thread.sleep (1000); ^ symbol: method sleep(int) location: class thread 1 error
secondthread:
class="lang-none prettyprint-override"> shahjahan@shahjahan-aod270:~/documents/java$ javac secondthread.java secondthread.java:26: error: cannot find symbol thread.sleep(1000); ^ symbol: method sleep(int) location: class thread 1 error
threaddemo:
class="lang-none prettyprint-override">shahjahan@shahjahan-aod270:~/documents/java$ javac threaddemo.java threaddemo.java:12: error: constructor thread in class thread cannot applied given types; thread thread1 = new thread(firstthread); ^ required: no arguments found: firstthread reason: actual , formal argument lists differ in length threaddemo.java:13: error: cannot find symbol thread1.start(); ^ symbol: method start() location: variable thread1 of type thread threaddemo.java:16: error: constructor thread in class thread cannot applied given types; thread thread2 = new thread(secondthread); ^ required: no arguments found: secondthread reason: actual , formal argument lists differ in length threaddemo.java:17: error: cannot find symbol thread2.start(); ^ symbol: method start() location: variable thread2 of type thread ./firstthread.java:28: error: cannot find symbol thread.sleep (1000); ^ symbol: method sleep(int) location: class thread ./secondthread.java:26: error: cannot find symbol thread.sleep(1000); ^ symbol: method sleep(int) location: class thread 6 errors
i novice in java. why thread.sleep not working. thread implementation depends on machine on compiled?
either importing class name thread
, not java.lang.thread
, or there class called thread
in current code directory 'polluting' imports class.
remove/rename classes called 'thread' in folder, , remove imports class called thread
java multithreading
Comments
Post a Comment