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 6 of 6

Thread: java concurrency problem

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java concurrency problem

    Hi ! I want to make a simple math operations on a vector(array) using two cores of my CPU. The program doesn't work correctly. Please explain me how to solve my problem.

    public class MyRunnable implements Runnable {
     
        private int startIndex;
        private int endIndex;
        private float[] tab;
     
        public MyRunnable(int startIndex, int endIndex, float[] tab)
        {
            this.startIndex = startIndex;
            this.endIndex = endIndex;
            this.tab = tab;
        }
     
        @Override
        public void run() 
        {
            for(int i = startIndex; i < endIndex; i++)
            {
                tab[i] = i * 2;
            }
     
            System.out.println("Finished");
        }
    }


    public class Test {
     
        public static void main(String[] args) {
     
            int size = 10;
            int n_threads = 2;
            float tab[] = new float[size];
     
            for(int i = 0; i < size; i++)
            {
                tab[i] = i;
            }
     
            for(int i = 0; i < size; i++)
            {
                System.out.println(tab[i]);
            }
     
            Runnable r1 = new MyRunnable(0, size / n_threads, tab );
            Runnable r2 = new MyRunnable(size / n_threads, size, tab );
     
            Thread t1 = new Thread(r1);
            Thread t2 = new Thread(r2);
     
            t1.start();
            t2.start();
     
            for(int i = 0; i < size; i++)
            {
                System.out.println(tab[i]);
            }
       }
     
    }


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Location
    toronto
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: java concurrency problem

    doesn't work what??

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java concurrency problem

    What does your code attempt to do?

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java concurrency problem

    I want to multiply an array using two threads (which should be done using two cores of a C2D CPU).


    The program prints, for example: "0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, Finished, 0.0, 2.0, 4.0, 6.0, 8.0, 5.0, 6.0, 7.0, 8.0, 9.0, Finished" or something similar. I except a result like this:
    0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, Finished, Finished, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0 and that make sense.

    Maybe there should be:

    t1.start();
    t2.start();
    t1.join();
    t2.join();
    Last edited by Nick7; July 31st, 2011 at 05:01 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java concurrency problem

    What happens when you add the join() calls?
    The threads only have to do 10 loops. The first thread could finish before the second thread was started.
    If you want overlaps between the two threads, try looping a lot more times.

    Add some printlns in the run to show when the threads start.
    Last edited by Norm; July 31st, 2011 at 04:58 PM.

  6. #6
    Junior Member
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: java concurrency problem

    Ok, I added println to the run:
    public void run() 
        {
            System.out.println(Thread.currentThread());
     
            for(int i = startIndex; i < endIndex; i++)
            {
                tab[i] = i * 2;
            }
     
            System.out.println("Finished");
        }

    With join() calls the program prints: 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, Thread[Thread-0,5,main], Finished, Thread[Thread-1,5,main], Finished, 0.0, 2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0 and that make sense.

Similar Threads

  1. Java split problem..
    By arch in forum Java SE APIs
    Replies: 5
    Last Post: August 11th, 2011, 07:48 AM
  2. Minesweeper java problem
    By bluez_exe in forum Paid Java Projects
    Replies: 1
    Last Post: March 3rd, 2010, 08:55 PM
  3. Election Java Problem
    By ronz in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 13th, 2009, 04:30 PM
  4. java problem
    By Mj Shine in forum Java Theory & Questions
    Replies: 1
    Last Post: August 14th, 2009, 12:24 AM
  5. Java session problem
    By Padmaja in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 5th, 2009, 09:06 PM