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: Fill array concurrently

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fill array concurrently

    I am trying to populate an array with random numbers in parallel.

    When I build it using Eclipse I get the error: "There is an error in the required project - Threads"

    Not very helpful! I'm at a loss as to what is wrong, any help would be appreciated.

     
    public class PopulateArray extends Thread {
     
    	private int f[];
    	private int a,b;
    	private int x;
     
    	public PopulateArray(int list[],int a, int b, int x){
    		f = list; this.a = a; this.b = b; this.x = x;
    	}
     
    	@Override
    	public void run() {
    		for(int j = a; j < b; j++) {
    			f[j] = x;
    		}
     
    	}
     
    }
     
    public class ShuffleResult {
     
    	public static void main(String[] args) {
    		//Populate array
    		int list[] = new int[100];
     
    		Thread t = new PopulateArray(list, 0 , 50 , (int)Math.random()* 100);
    		Thread t1 = new PopulateArray(list, 50 , list.length , (int)Math.random()* 100);
     
    		t.start(); t1.start();
    		try{
    			t.join(); t1.join();
    		}
    		catch(InterruptedException e){ }
     
    		//Print array
    		for(int i : list){
    			System.out.print(list[i] + " ");
     
    		}
    	}
     
    }

    If i proceed with the build the output is 100 zero's so obviously PopulateArray isn't doing anything?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Fill array concurrently

    Did you remember to import the threads class?

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Fill array concurrently

    Seems like it was an issue with the java project within Eclipse, I created a new package and it complies fine now!

Similar Threads

  1. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  2. How do I fix my program so I can use the keyboard to type in the numbers?
    By rocafella5007 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 29th, 2009, 02:39 PM
  3. [SOLVED] Java program to sort arrays containing dates
    By scottyadam in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:08 PM