0

I am trying to implement a hough transform in Java to find all lines in an image. Since the process itself took too much time, I decided to spread it across multiple threads. However, after doing that, the process didn't become much faster. What am I doing wrong? Is the reason that the for loop is waiting for the input_elements to complete on each iteration?

for loop

    latch = new CountDownLatch(thread_arr.length);
    for (int i=0; i<border_que.arr.length; i++) {
        thread_arr[i].input_elements(border_que.arr[i][0], border_que.arr[i][1]);
    }

    try {
        latch.await();
    } catch (Exception e) {System.out.println(e.getCause());}

Thread Code

class RowAndThetaThread implements Runnable{
    private int x_0;
    private int y_0;

    public void run() {
        // find rho and theta and push them to an array
    }

    public void input_elements(int x, int y) {
        x_0 = x;
        y_0 = y;

        this.run();
    }

}
Alex K
  • 8,269
  • 9
  • 39
  • 57
SalmonKiller
  • 2,183
  • 4
  • 27
  • 56

2 Answers2

1

You aren't actually using multiple threads.

Thread objects are not special. When you call a method on one, including run, that method still executes on the current thread.

There is a start method, which (when called on any existing thread) calls run on the new thread. This is the only way to start a new thread within Java.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • 1
    How can I call `start` from inside the `input_elements` method? Doing `this.start()` or `RowAndThetaThread.start()` don't work. – SalmonKiller Jan 12 '15 at 04:56
  • ... I should have noticed that you aren't even using `Thread`. (How did you expect it to work when your code has nothing to do with threading?). Simple band-aid fix: change `implements Runnable` to `extends Thread`. (This is not considered a great practice, because usually you want to have a fixed number of threads for optimal performance, but that's another question) – user253751 Jan 12 '15 at 04:58
  • @immibis In general you shouldn't extend `Thread`, just provide the `Runnable` to a new `Thread` (or better: executor service) – Mark Rotteveel Jan 12 '15 at 09:39
1

you can try smth like this ,without any class:

latch = new CountDownLatch(thread_arr.length);
for (int i=0; i<border_que.arr.length; i++) 
  {

     //added
     static int x = border_que.arr[i][0];
     static int y = border_que.arr[i][1];

    Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {

                    //thread action here

                      int x_0 =x;
                      int y_0 =y;

                } catch (Exception e) {
                    //report thread exeception etc
                }
        }
    });

  thread.start();
  //end added

}

try {
    latch.await();
} catch (Exception e) {System.out.println(e.getCause());}
Maria Gheorghe
  • 599
  • 1
  • 5
  • 18