| | |
implementing runnable instead of extends Thread
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2009
Posts: 4
Reputation:
Solved Threads: 0
I the program I wish to run is a search for a number in 4 threads. Its working out fine and here is the code.
Now I wish to use Runnable instead of extends Thread
But this code is not compiling. How to correct the code?
Java Syntax (Toggle Plain Text)
import java.lang.Math; import java.lang.Thread; public class NumberFinder { public static void main(String args[]) { int target = (int) (Math.random()*1000); System.out.println("The number is " + target); Thread thread0 = new Finder(target, 0, 249); Thread thread1 = new Finder(target, 250, 499); Thread thread2 = new Finder(target, 500, 749); Thread thread3 = new Finder(target, 750, 1000); thread0.start(); thread1.start(); thread2.start(); thread3.start(); } } class Finder extends Thread { int searchFor; int beginRange; int endRange; public Finder(int searchFor, int beginRange, int endRange) { this.searchFor = searchFor; this.beginRange = beginRange; this.endRange = endRange; System.out.println ("in constructor " + this.getName() + " " + beginRange + " " + endRange); } public void run() { for (int i = beginRange; i < endRange; i++){ if (searchFor == i){ System.out.println("found at " +this.getName()+ " and the number is "+ i); } } } }
Now I wish to use Runnable instead of extends Thread
Java Syntax (Toggle Plain Text)
import java.lang.Math; import java.lang.Thread; public class NumberFinder { public static void main(String args[]) { int target = (int) (Math.random()*1000); System.out.println("The number is " + target); Thread thread0 = new Finder(target, 0, 249); Thread thread1 = new Finder(target, 250, 499); Thread thread2 = new Finder(target, 500, 749); Thread thread3 = new Finder(target, 750, 1000); thread0.start(); thread1.start(); thread2.start(); thread3.start(); } } class Finder implements Runnable { Thread t = Thread.currentThread(); int searchFor; int beginRange; int endRange; public Finder(int searchFor, int beginRange, int endRange) { t.searchFor = searchFor; t.beginRange = beginRange; t.endRange = endRange; System.out.println ("in constructor " + t.getName() + " " + beginRange + " " + endRange); } public void run() { for (int i = beginRange; i < endRange; i++){ if (searchFor == i){ System.out.println("found at " +this.getName()+ " and the number is "+ i); } } } }
But this code is not compiling. How to correct the code?
•
•
Join Date: Apr 2009
Posts: 4
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
import java.lang.Math; import java.lang.Thread; public class NumberFinder { public static void main(String args[]) { int target = (int) (Math.random()*1000); System.out.println("The number is " + target); Thread t = Thread.currentThread(); Thread thread0 = new Thread(new Finder(target, 0, 250)); Thread thread1 = new Thread(new Finder(target, 251,500 )); Thread thread2 = new Thread(new Finder(target,501, 750)); Thread thread3 = new Thread(new Finder(target,751, 1000)); thread0.start(); thread1.start(); thread2.start(); thread3.start(); } } class Finder implements Runnable { int searchFor; int beginRange; int endRange; public Finder(int searchFor, int beginRange, int endRange) { Thread t = Thread.currentThread(); t.searchFor = searchFor; t.beginRange = beginRange; t.endRange = endRange; System.out.println ("in constructor " + t.getName() + " " + beginRange + " " + endRange); } public void run() { for (int i = beginRange; i < endRange; i++){ if (searchFor == i){ System.out.println("found at " +t.getName()+ " and the number is "+ i); } } } }
Java Syntax (Toggle Plain Text)
C:\Users\>javac NumberFinder.java NumberFinder.java:32: cannot find symbol symbol : variable searchFor location: class java.lang.Thread t.searchFor = searchFor; ^ NumberFinder.java:33: cannot find symbol symbol : variable beginRange location: class java.lang.Thread t.beginRange = beginRange; ^ NumberFinder.java:34: cannot find symbol symbol : variable endRange location: class java.lang.Thread t.endRange = endRange; ^ NumberFinder.java:46: cannot find symbol symbol : variable t location: class Finder System.out.println("found at " +t.getName()+ " and the number is "+ i); ^ 4 errors
Can somebody help me out here?
Last edited by grumpty; Apr 25th, 2009 at 4:09 am.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
In method main write In constructor Finder 4x change Replace Write own method to start thread.
java Syntax (Toggle Plain Text)
Finder thread0 = new Finder(target, 0, 249);//4x change type of Thread->Finder
java Syntax (Toggle Plain Text)
this.searchFor = searchFor; // change t. -> this.
java Syntax (Toggle Plain Text)
Thread t = new Thread(this);//Thread.currentThread();
java Syntax (Toggle Plain Text)
//
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
Of course kind of view presented by nmaillet is good too. In both cases you need Inside main method line is no needed.
Inside Finder constructor you don't need the line In both println methods use simply static metod to reflect actual (not stored) name of thread.
•
•
•
•
Use Thread thread0 = new Thread(new Finder(target, 0, 249));
•
•
•
•
In constructor Finder 4x change
this.searchFor = searchFor; // change t. -> this.
java Syntax (Toggle Plain Text)
Thread t = Thread.currentThread();
Inside Finder constructor you don't need the line
java Syntax (Toggle Plain Text)
Thread t = Thread.currentThread();
java Syntax (Toggle Plain Text)
... + Thread.currentThread().getName() + ...
![]() |
Similar Threads
- Progress Bar problems (Java)
- Error w. drawing line (Java)
Other Threads in the Java Forum
- Previous Thread: Inventory Program Part 5
- Next Thread: Writing data to a file from an applet
Views: 723 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android api apple applet application arguments array arrays automation binary block bluetooth chat class classes client code compile component database developmenthelp draw eclipse encode error event exception file fractal freeze game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer iphone j2me j2seprojects java javac javaprojects jmf jni jpanel julia lego linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number object online oracle print problem program programming project recursion scanner screen server set singleton size sms socket sort sql string swing system template test textfields threads time title transfer tree tutorial-sample update windows working





