implementing runnable instead of extends Thread

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2009
Posts: 4
Reputation: grumpty is an unknown quantity at this point 
Solved Threads: 0
grumpty grumpty is offline Offline
Newbie Poster

implementing runnable instead of extends Thread

 
0
  #1
Apr 24th, 2009
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.
  1. import java.lang.Math;
  2. import java.lang.Thread;
  3. public class NumberFinder
  4. {
  5. public static void main(String args[])
  6. {
  7. int target = (int) (Math.random()*1000);
  8. System.out.println("The number is " + target);
  9.  
  10. Thread thread0 = new Finder(target, 0, 249);
  11. Thread thread1 = new Finder(target, 250, 499);
  12. Thread thread2 = new Finder(target, 500, 749);
  13. Thread thread3 = new Finder(target, 750, 1000);
  14. thread0.start();
  15. thread1.start();
  16. thread2.start();
  17. thread3.start();
  18. }
  19. }
  20.  
  21. class Finder extends Thread
  22. {
  23.  
  24. int searchFor;
  25. int beginRange;
  26. int endRange;
  27.  
  28.  
  29.  
  30. public Finder(int searchFor, int beginRange, int endRange)
  31. {
  32. this.searchFor = searchFor;
  33. this.beginRange = beginRange;
  34. this.endRange = endRange;
  35. System.out.println ("in constructor " + this.getName() + " " + beginRange + " " + endRange);
  36. }
  37.  
  38.  
  39. public void run()
  40. {
  41. for (int i = beginRange; i < endRange; i++){
  42.  
  43.  
  44. if (searchFor == i){
  45.  
  46. System.out.println("found at " +this.getName()+ " and the number is "+ i);
  47. }
  48.  
  49. }
  50. }
  51. }

Now I wish to use Runnable instead of extends Thread

  1. import java.lang.Math;
  2. import java.lang.Thread;
  3. public class NumberFinder
  4. {
  5. public static void main(String args[])
  6. {
  7. int target = (int) (Math.random()*1000);
  8. System.out.println("The number is " + target);
  9.  
  10. Thread thread0 = new Finder(target, 0, 249);
  11. Thread thread1 = new Finder(target, 250, 499);
  12. Thread thread2 = new Finder(target, 500, 749);
  13. Thread thread3 = new Finder(target, 750, 1000);
  14. thread0.start();
  15. thread1.start();
  16. thread2.start();
  17. thread3.start();
  18. }
  19. }
  20.  
  21. class Finder implements Runnable
  22. {
  23. Thread t = Thread.currentThread();
  24. int searchFor;
  25. int beginRange;
  26. int endRange;
  27.  
  28.  
  29.  
  30. public Finder(int searchFor, int beginRange, int endRange)
  31. {
  32. t.searchFor = searchFor;
  33. t.beginRange = beginRange;
  34. t.endRange = endRange;
  35. System.out.println ("in constructor " + t.getName() + " " + beginRange + " " + endRange);
  36. }
  37.  
  38.  
  39. public void run()
  40. {
  41. for (int i = beginRange; i < endRange; i++){
  42.  
  43.  
  44. if (searchFor == i){
  45.  
  46. System.out.println("found at " +this.getName()+ " and the number is "+ i);
  47. }
  48.  
  49. }
  50. }
  51. }

But this code is not compiling. How to correct the code?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 84
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: implementing runnable instead of extends Thread

 
0
  #2
Apr 25th, 2009
Since Finder is no longer a subclass of thread, you cannot store a Thread reference to it. Use Thread thread0 = new Thread(new Finder(target, 0, 249));
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: grumpty is an unknown quantity at this point 
Solved Threads: 0
grumpty grumpty is offline Offline
Newbie Poster

Re: implementing runnable instead of extends Thread

 
0
  #3
Apr 25th, 2009
Originally Posted by nmaillet View Post
Since Finder is no longer a subclass of thread, you cannot store a Thread reference to it. Use Thread thread0 = new Thread(new Finder(target, 0, 249));
Thanks a lot! that fixed it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: grumpty is an unknown quantity at this point 
Solved Threads: 0
grumpty grumpty is offline Offline
Newbie Poster

Re: implementing runnable instead of extends Thread

 
0
  #4
Apr 25th, 2009
I made a mistake I executed the old program by mistake.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 4
Reputation: grumpty is an unknown quantity at this point 
Solved Threads: 0
grumpty grumpty is offline Offline
Newbie Poster

Re: implementing runnable instead of extends Thread

 
0
  #5
Apr 25th, 2009
  1. import java.lang.Math;
  2. import java.lang.Thread;
  3. public class NumberFinder
  4. {
  5. public static void main(String args[])
  6. {
  7. int target = (int) (Math.random()*1000);
  8. System.out.println("The number is " + target);
  9. Thread t = Thread.currentThread();
  10. Thread thread0 = new Thread(new Finder(target, 0, 250));
  11. Thread thread1 = new Thread(new Finder(target, 251,500 ));
  12. Thread thread2 = new Thread(new Finder(target,501, 750));
  13. Thread thread3 = new Thread(new Finder(target,751, 1000));
  14. thread0.start();
  15. thread1.start();
  16. thread2.start();
  17. thread3.start();
  18. }
  19. }
  20.  
  21. class Finder implements Runnable
  22. {
  23.  
  24. int searchFor;
  25. int beginRange;
  26. int endRange;
  27.  
  28.  
  29.  
  30. public Finder(int searchFor, int beginRange, int endRange)
  31. { Thread t = Thread.currentThread();
  32. t.searchFor = searchFor;
  33. t.beginRange = beginRange;
  34. t.endRange = endRange;
  35. System.out.println ("in constructor " + t.getName() + " " + beginRange + " " + endRange);
  36. }
  37.  
  38.  
  39. public void run()
  40. {
  41. for (int i = beginRange; i < endRange; i++){
  42.  
  43.  
  44. if (searchFor == i){
  45.  
  46. System.out.println("found at " +t.getName()+ " and the number is "+ i);
  47. }
  48.  
  49. }
  50. }
  51. }










  1. C:\Users\>javac NumberFinder.java
  2. NumberFinder.java:32: cannot find symbol
  3. symbol : variable searchFor
  4. location: class java.lang.Thread
  5. t.searchFor = searchFor;
  6. ^
  7. NumberFinder.java:33: cannot find symbol
  8. symbol : variable beginRange
  9. location: class java.lang.Thread
  10. t.beginRange = beginRange;
  11. ^
  12. NumberFinder.java:34: cannot find symbol
  13. symbol : variable endRange
  14. location: class java.lang.Thread
  15. t.endRange = endRange;
  16. ^
  17. NumberFinder.java:46: cannot find symbol
  18. symbol : variable t
  19. location: class Finder
  20. System.out.println("found at " +t.getName()+ " and the number is
  21. "+ i);
  22. ^
  23. 4 errors

Can somebody help me out here?
Last edited by grumpty; Apr 25th, 2009 at 4:09 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: implementing runnable instead of extends Thread

 
0
  #6
Apr 25th, 2009
In method main write
  1. Finder thread0 = new Finder(target, 0, 249);//4x change type of Thread->Finder
In constructor Finder 4x change
  1. this.searchFor = searchFor; // change t. -> this.
Replace
  1. Thread t = new Thread(this);//Thread.currentThread();
Write own method to start thread.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: implementing runnable instead of extends Thread

 
0
  #7
Apr 25th, 2009
Of course kind of view presented by nmaillet is good too.
Use Thread thread0 = new Thread(new Finder(target, 0, 249));
In both cases you need
In constructor Finder 4x change
this.searchFor = searchFor; // change t. -> this.
Inside main method line
  1. Thread t = Thread.currentThread();
is no needed.
Inside Finder constructor you don't need the line
  1. Thread t = Thread.currentThread();
In both println methods use simply static metod
  1. ... + Thread.currentThread().getName() + ...
to reflect actual (not stored) name of thread.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 723 | Replies: 6
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC