Java线程-如何使hough转换运行得更快
我试图在Java中实现一个hough变换来查找图像中的所有行。由于进程本身花费了太多的时间,我决定将其分散到多个线程中。然而,在这样做之后,这个过程并没有变得更快。我做错了什么?是因为Java线程-如何使hough转换运行得更快,java,android,multithreading,hough-transform,Java,Android,Multithreading,Hough Transform,我试图在Java中实现一个hough变换来查找图像中的所有行。由于进程本身花费了太多的时间,我决定将其分散到多个线程中。然而,在这样做之后,这个过程并没有变得更快。我做错了什么?是因为for循环等待input\u元素在每次迭代中完成的原因吗 用于循环 latch = new CountDownLatch(thread_arr.length); for (int i=0; i<border_que.arr.length; i++) { thread_arr[i
for
循环等待input\u元素在每次迭代中完成的原因吗
用于
循环
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());}
您实际上没有使用多个线程
线程
对象不是特殊的。在一个线程上调用方法时,包括run
,该方法仍在当前线程上执行
有一个start
方法,它(在任何现有线程上调用时)在新线程上调用run
。这是在Java中启动新线程的唯一方法。您可以像这样尝试smth,无需任何类:
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());}
latch=新的倒计时锁存器(螺纹长度);
对于(int i=0;i如何从input\u元素方法内部调用start
?执行this.start()
或rownthetathread.start()
不工作……我应该注意到您甚至没有使用Thread
(当您的代码与线程无关时,您希望它如何工作?)简单的创可贴修复:更改<代码>实现可运行的<代码> >代码>扩展线程。(这不被认为是一个伟大的实践,因为通常您希望有固定数量的线程以获得最佳性能,但这是另一个问题)@immibis通常不应该扩展线程
,只需将可运行
提供给新的线程
(或者更好:executor服务)
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());}