Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why my void run method cannot be execute?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why my void run method cannot be execute?

    public class MyRun implements Runnable{
     
    	int[] array = new int[5];
    	int index;		
     
    	MyRun(int value, int index){
    		this.index = index;
    		array[index] = 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]);
    	}
    }
    and the main thread:
    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;		
     
    		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];
    		}		
     
    		pivot = array[lowest + (highest - lowest)/2];					
     
    		for(int count = 0; count < array.length; count++){
    			Runnable task = new MyRun(array[count],count);			
    		}		
     
    		executor.shutdown();
    		while(!executor.isTerminated());
    		System.out.println("Finish");
    	}
    }

    Why my run() cant be execute? Please, please do help me..


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Why my void run method cannot be execute?

    ...when are you calling the run() method?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Why my void run method cannot be execute?

    You should submit the Runnable object to the ExecutorService, for example:

    executor.submit(task)

Similar Threads

  1. Struts2 without execute method
    By tcstcs in forum Web Frameworks
    Replies: 3
    Last Post: May 29th, 2013, 07:24 AM
  2. [Newbie] Method will not execute.
    By parkBENch in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 19th, 2012, 06:33 AM
  3. So Lost with void method
    By tripline in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 27th, 2011, 01:45 PM
  4. Reverse character using void method
    By bgwilf in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 8th, 2010, 07:25 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM

Tags for this Thread