File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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(){
for(int count = 0; count < 5; 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);


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

Joined: Dec 08, 2010
Posts: 1545

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.

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

Joined: Oct 16, 2003
Posts: 1101

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

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

Joined: Oct 16, 2003
Posts: 1101

where is your executor.execute(task) call?
I agree. Here's the link:
subject: Why the Runnable cant be resolved
It's not a secret anymore!