GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Why the Runnable cant be resolved Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Why the Runnable cant be resolved" Watch "Why the Runnable cant be resolved" New topic
Author

Why the Runnable cant be resolved

Sara Brown
Ranch Hand

Joined: Feb 22, 2012
Posts: 55
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

Joined: Dec 08, 2010
Posts: 1509
    
    5

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.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

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.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
Sara Brown
Ranch Hand

Joined: Feb 22, 2012
Posts: 55
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

Joined: Oct 16, 2003
Posts: 1085
    
  10

where is your executor.execute(task) call?
 
 
subject: Why the Runnable cant be resolved