multithreaded java bubble sort

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

Join Date: May 2007
Posts: 4
Reputation: the juice is an unknown quantity at this point 
Solved Threads: 0
the juice the juice is offline Offline
Newbie Poster

multithreaded java bubble sort

 
0
  #1
Mar 9th, 2008
hey i am doing an assignment and it is to create a multithreaded bubble sort but i am having problems initialising and assigning jobs to the threads. i have included the classes and the errors i am getting and was wondering if someone could tell me what is going wrong. CHEERS

ERRORS

C:\Documents and Settings\rich\Desktop\OOA\Thread Assignment>javac Sort1.java
Sort1.java:37: cannot find symbol
symbol : variable t1
location: class Sort1
if (Thread.currentThread() == t1) {
^
Sort1.java:50: cannot find symbol
symbol : variable t2
location: class Sort1
else if (Thread.currentThread() == t2) {
^
Sort1.java:64: cannot find symbol
symbol : variable t3
location: class Sort1
else if (Thread.currentThread() == t3) {
^
Sort1.java:79: cannot find symbol
symbol : variable t4
location: class Sort1
else if (Thread.currentThread() == t4) {
^
4 errors




  1. // Sort1.java
  2.  
  3. import java.util.Scanner; //import the class which allows you to scan for input
  4.  
  5. public class Sort1 implements Runnable {
  6.  
  7. public int x;
  8.  
  9. public void run()
  10. {
  11. int hold;
  12. Scanner kb = new Scanner (System.in);
  13.  
  14. int num1, num2, num3, num4, num5; //data entry
  15.  
  16. //Read the numbers as they are entered
  17. System.out.print("Enter the first number:");
  18. System.out.flush(); // flush the buffer of all current input
  19. num1 = kb.nextInt(); // use the scanner to get input and call it num1
  20.  
  21. System.out.print("Enter the second number:");
  22. System.out.flush();
  23. num2 = kb.nextInt();// use the scanner to get input and call it num2
  24.  
  25. System.out.print("Enter the third number:");
  26. System.out.flush();
  27. num3 = kb.nextInt();// use the scanner to get input and call it num3
  28.  
  29. System.out.print("Enter the fourth number:");
  30. System.out.flush();
  31. num4 = kb.nextInt();// use the scanner to get input and call it num4
  32.  
  33. System.out.print("Enter the fifth number:");
  34. System.out.flush();
  35. num5 = kb.nextInt();// use the scanner to get input and call it num5
  36.  
  37. if (Thread.currentThread() == t1) {
  38.  
  39. while (num1 < num2) { /* compare the two neighbors num1 and num2 */
  40.  
  41. hold = num1;
  42.  
  43. num1 = num2;
  44.  
  45. num2 = hold;
  46.  
  47. System.out.print(Thread.currentThread().getName() + " value is " + x);
  48. }
  49. }
  50. else if (Thread.currentThread() == t2) {
  51.  
  52. while (num2 < num3) { /* compare the two neighbors num2 and num3 */
  53.  
  54. hold = num2;
  55.  
  56. num2 = num3;
  57.  
  58. num3 = hold;
  59.  
  60. System.out.print(Thread.currentThread().getName() + " value is " + x);
  61. }
  62.  
  63. }
  64. else if (Thread.currentThread() == t3) {
  65.  
  66. while (num3 < num4) { /* compare the two neighbors num3 and num4 */
  67.  
  68. hold = num3;
  69.  
  70. num3 = num4;
  71.  
  72. num4 = hold;
  73.  
  74. System.out.print(Thread.currentThread().getName() + " value is " + x);
  75. }
  76.  
  77. }
  78.  
  79. else if (Thread.currentThread() == t4) {
  80.  
  81. while (num4 < num5) { /* compare the two neighbors num4 and num5 */
  82.  
  83. hold = num4;
  84.  
  85. num4 = num5;
  86.  
  87. num5 = hold;
  88.  
  89. System.out.print(Thread.currentThread().getName() + " value is " + x);
  90. }
  91.  
  92. }
  93.  
  94. else {
  95.  
  96. System.out.print("The sort has finished:" + num1 + num2 + num3 + num4 + num5);
  97.  
  98. }
  99. }
  100. }

  1. // TestSort1.java
  2.  
  3. public class TestSort1 {
  4. public static void main( String []argv )
  5. {
  6. Sort1 s = new Sort1();
  7. Thread t1 = new Thread(s);
  8. Thread t2 = new Thread(s);
  9. Thread t3 = new Thread(s);
  10. Thread t4 = new Thread(s);
  11. Thread t5 = new Thread(s);
  12.  
  13. t1.start();
  14. t2.start();
  15. t3.start();
  16. t4.start();
  17. t5.start();
  18. }
  19. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,467
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 267
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: multithreaded java bubble sort

 
0
  #2
Mar 9th, 2008
You've created the variables in TestSort1 and try to use them in Sort1. Doesn't work that way.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 13
Reputation: shinnxennosagga is an unknown quantity at this point 
Solved Threads: 1
shinnxennosagga shinnxennosagga is offline Offline
Newbie Poster

Re: multithreaded java bubble sort

 
0
  #3
Mar 9th, 2008
I don't really get the idea of multithreading bubble sort, but Masijade is right. You might want to pass variable t1 - t5 to Sort1 so that they'r recognized in the class.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4
Reputation: the juice is an unknown quantity at this point 
Solved Threads: 0
the juice the juice is offline Offline
Newbie Poster

Re: multithreaded java bubble sort

 
0
  #4
Mar 10th, 2008
yeh cheers that works fine now nice one
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the Java Forum


Views: 1606 | Replies: 3
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC