Java 在循环中创建多个线程对象
我怎样才能解决这个问题。如何创建多个线程对象Java 在循环中创建多个线程对象,java,arrays,multithreading,nullpointerexception,thread-safety,Java,Arrays,Multithreading,Nullpointerexception,Thread Safety,我怎样才能解决这个问题。如何创建多个线程对象 public void filterControl (int threadCount) { this.rowCount = img.getHeight() / threadCount; Thread[] t = null; for (int i=0; i<threadCount; i++) { t[i] = new Thread(new Runnable(){ //there is a NullPoin
public void filterControl (int threadCount) {
this.rowCount = img.getHeight() / threadCount;
Thread[] t = null;
for (int i=0; i<threadCount; i++) {
t[i] = new Thread(new Runnable(){ //there is a NullPointerException
@Override
public void run() {
filtering(startRow, rowCount);
}
});
t[i].run();
}
}
public void filter控件(int threadCount){
this.rowCount=img.getHeight()/threadCount;
线程[]t=null;
对于(int i=0;i您的NullPointerException
来自您将数组初始化为null
将其初始化为:
Thread[] t = new Thread[threadCount];
这将初始化其所有元素为null
(对象的默认值),但数组本身为非null
实例,总共有threadCount
元素插槽
注意
您不能通过调用run
来启动线程
,该线程将在调用线程内执行run
方法
使用t[i].start();
。在尝试使用“数组”之前创建一个数组
试着替换
Thread[] t = null;
到
请解释一下否决票?
Thread[] t = new Thread[threadCount];