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

// Sort1.java

import java.util.Scanner; //import the class which allows you to scan for input

public class Sort1 implements Runnable {
  
  public int x;  
  
  public void run() 
  {
    int hold;
    Scanner kb = new Scanner (System.in);

	int num1, num2, num3, num4, num5; //data entry

	//Read the numbers as they are entered
	System.out.print("Enter the first number:");
	System.out.flush(); // flush the buffer of all current input
	num1 = kb.nextInt(); // use the scanner to get input and call it num1

	System.out.print("Enter the second number:");
	System.out.flush();
	num2 = kb.nextInt();// use the scanner to get input and call it num2

	System.out.print("Enter the third number:");
	System.out.flush();
	num3 = kb.nextInt();// use the scanner to get input and call it num3

	System.out.print("Enter the fourth number:");
	System.out.flush();
	num4 = kb.nextInt();// use the scanner to get input and call it num4

	System.out.print("Enter the fifth number:");
	System.out.flush();
	num5 = kb.nextInt();// use the scanner to get input and call it num5

if (Thread.currentThread() == t1) {

	while (num1 < num2) {  /* compare the two neighbors num1 and num2 */

		hold = num1;         

		num1 = num2;

		num2 = hold;

	System.out.print(Thread.currentThread().getName() + " value is " + x);
  		}
    }
    else if (Thread.currentThread() == t2) {
      
	while (num2 < num3) {  /* compare the two neighbors num2 and num3 */

		hold = num2;         

		num2 = num3;

		num3 = hold;

	System.out.print(Thread.currentThread().getName() + " value is " + x);
	}

    }
    else if (Thread.currentThread() == t3) {
      
	while (num3 < num4) {  /* compare the two neighbors num3 and num4 */

		hold = num3;         

		num3 = num4;

		num4 = hold;

	System.out.print(Thread.currentThread().getName() + " value is " + x);
	}

    }

 else if (Thread.currentThread() == t4) {
      
	while (num4 < num5) {  /* compare the two neighbors num4 and num5 */

		hold = num4;         

		num4 = num5;

		num5 = hold;

	System.out.print(Thread.currentThread().getName() + " value is " + x);
	}

    }
  
    else {
      
	System.out.print("The sort has finished:" + num1 + num2 + num3 + num4 + num5);
  
    }
  }
}
// TestSort1.java

public class TestSort1 {
  public static void main( String []argv )
  {
	 Sort1 s = new Sort1();
     Thread t1 = new Thread(s);
     Thread t2 = new Thread(s);
     Thread t3 = new Thread(s);
     Thread t4 = new Thread(s);
     Thread t5 = new Thread(s);
     
     t1.start();
     t2.start();
     t3.start();
     t4.start();
     t5.start();
  }
}

You've created the variables in TestSort1 and try to use them in Sort1. Doesn't work that way.

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.

yeh cheers that works fine now nice one

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.