java不让按钮超过边界_java - 在JAVA中使用并行求解时超出边界异常 - SO中文参考 - www.soinside.com...

我正在尝试并行解决多个优化实例,并创建了一个玩具示例。当我用单个线程解决每个实例时,它可以正常工作。当我开始增加线程数时,某些实例会收到内存不足的异常。我想知道是否有人可以给我提示如何解决此问题。

我尽力使示例尽可能简单。import java.util.Iterator;

import java.util.Vector;

public class ConcurrentExample {

public static int fixed;

public static int nbWarehouses;

public static int nbStores;

public static void main(String[] args) throws IloException, InterruptedException {

int nProblems = 20;

final Vector v = new Vector();

for (int i = 1; i <= nProblems; ++i) v.add(i);

final Iterator it = v.iterator();

int nTHREADS = 2; //numofCores

Thread[] threads = new Thread[nTHREADS ];

for (int t = 0; t < nTHREADS ; ++t) {

threads[t] = new Thread(new Runnable() {

public void run() {

while (true) {

int i;

synchronized (it) {

if ( !it.hasNext() ) return;

i = it.next();

}

fixed = 400 + i;

nbWarehouses = 400 ;

nbStores = 800 + i;

int[] capacity = new int[nbWarehouses];

int[][] supplyCost= new int[nbStores][nbWarehouses];

for(int w=0; w< nbWarehouses ; w++) {

capacity[w] = (nbStores/ nbWarehouses) + (w % ( nbStores/ nbWarehouses));

for(int s=0; s< nbStores ; s++) {

supplyCost[s][w] = 1 + ( ( s + 10 * w) % 100 );

}

}

}

}

}

);

}

for (int t = 0; t < nTHREADS; ++t) { threads[t].start(); }

for (int t = 0; t < nTHREADS; ++t) { threads[t].join(); }

}

}

当我使用两个线程时,出现以下错误:Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 801

at concurrent$1.run(concurrent.java:36)

at java.lang.Thread.run(Thread.java:748)

设置NTHREADS=4时,出现以下错误。Exception in thread "Thread-1" Exception in thread "Thread-0" Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 802

at concurrent$1.run(concurrent.java:36)

at java.lang.Thread.run(Thread.java:748)

java.lang.ArrayIndexOutOfBoundsException: 801

at concurrent$1.run(concurrent.java:36)

at java.lang.Thread.run(Thread.java:748)

java.lang.ArrayIndexOutOfBoundsException: 803

at concurrent$1.run(concurrent.java:36)

at java.lang.Thread.run(Thread.java:748)

编辑:一非常重要的事情是我必须将变量保持为全局定义。这只是一个玩具例子。在实际模型中,我有静态变量,可以在迭代解决方案方法中通过多种方法来访问它们。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值