943,908 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1306
  • Java RSS
Apr 24th, 2009
0

implementing runnable instead of extends Thread

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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

Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grumpty is offline Offline
4 posts
since Apr 2009
Apr 25th, 2009
0

Re: implementing runnable instead of extends Thread

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));
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Apr 25th, 2009
0

Re: implementing runnable instead of extends Thread

Click to Expand / Collapse  Quote originally posted by nmaillet ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grumpty is offline Offline
4 posts
since Apr 2009
Apr 25th, 2009
0

Re: implementing runnable instead of extends Thread

I made a mistake I executed the old program by mistake.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grumpty is offline Offline
4 posts
since Apr 2009
Apr 25th, 2009
0

Re: implementing runnable instead of extends Thread

Java Syntax (Toggle Plain Text)
  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. }










Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grumpty is offline Offline
4 posts
since Apr 2009
Apr 25th, 2009
0

Re: implementing runnable instead of extends Thread

In method main write
java Syntax (Toggle Plain Text)
  1. Finder thread0 = new Finder(target, 0, 249);//4x change type of Thread->Finder
In constructor Finder 4x change
java Syntax (Toggle Plain Text)
  1. this.searchFor = searchFor; // change t. -> this.
Replace
java Syntax (Toggle Plain Text)
  1. Thread t = new Thread(this);//Thread.currentThread();
Write own method to start thread.
java Syntax (Toggle Plain Text)
  1. //
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Apr 25th, 2009
0

Re: implementing runnable instead of extends Thread

Of course kind of view presented by nmaillet is good too.
Quote ...
Use Thread thread0 = new Thread(new Finder(target, 0, 249));
In both cases you need
Quote ...
In constructor Finder 4x change
this.searchFor = searchFor; // change t. -> this.
Inside main method line
java Syntax (Toggle Plain Text)
  1. Thread t = Thread.currentThread();
is no needed.
Inside Finder constructor you don't need the line
java Syntax (Toggle Plain Text)
  1. Thread t = Thread.currentThread();
In both println methods use simply static metod
java Syntax (Toggle Plain Text)
  1. ... + Thread.currentThread().getName() + ...
to reflect actual (not stored) name of thread.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: About learning spring,strut,hibernate?
Next Thread in Java Forum Timeline: Writing data to a file from an applet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC