File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why the Runnable cant be resolved

 
Sara Brown
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What im trying to do is do the quicksort operation using concurrent thread. but for now im trying to make up a simple sorting using only 1 main thread n 1 child thread. Below is my program.


public class MyRun implements Runnable{

int[] array = new int[5];

MyRun(int value){
for(int count = 0; count < 5; count++)
array[count] = value;
}

//run() method to do operation
public void run(){
System.out.println("run()");
for(int count = 0; count < 5; count++)
System.out.println(array[count]);
}
}

--------------------------------------------------------------------
import java.util.concurrent.*;
public class satu {
public static void main(String[]args){
int[] array = {4,2,3,1,5};

int lowest = array[0], highest = array[0];
int pivot;
Runnable task;

ExecutorService executor = Executors.newFixedThreadPool(1);

for(int index = 1; index < array.length; index++){
//mission: find highest n lowest
if(lowest > array[index])
lowest = array[index];
else if (highest < array[index])
highest = array[index];
}
System.out.println("highest: " + highest);
System.out.println("lowest: " + lowest);

pivot = array[lowest + (highest - lowest)/2];

for(int count = 0; count < array.length; count++)
task = new MyRun(array[count]);
Thread thread = new Thread(task);
executor.execute(thread);

executor.shutdown();
while(!executor.isTerminated());
}
}


But the program says the task(which ive been bold) is undefined and the Runnable cant be resolved. Why is that? can somebody help me on my programming, please
 
Anayonkar Shivalkar
Bartender
Posts: 1557
5
Eclipse IDE Java Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi asparagus white,

Welcome to CodeRanch!

Please UseCodeTags. You can still edit your post.

I haven't checked all lines of your code, but just line relevant to bold one (the problematic line).

The error message you might have gotten is quite self explanatory. All you are doing is declaring a Runnable reference without initializing it, then initializing it in for loop, and outside the loop, you are using same reference (whose initialization in for loop is out of scope now) to create a new Thread object.

Please put all your relevant logic in proper loops and it should work (at least syntactically).

I hope this helps.
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, since you are using ExecutorService, there is no need for you to create a Thread object. You can just pass the Runnable to the execute method.
 
Sara Brown
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i came up with this:



and the other class would be:




It runs perfectly but my run() won't execute. what other mistakes i made??
 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1101
10
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where is your executor.execute(task) call?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic