HTML Code:Hi I am using this code in order to run multiple treads. Among 30 thread created only tow of them fail due to NullPointerException. As it could be seen I pass ASEvaluator and aPopulation[i] to my Runnable via its contractor. I want to isolate these in each thread. The problem is when one thread change the value of one of these parameters other threads see the changes. Any idea?
package weka.attributeSelection; public class antibodyEvaluator implements Runnable { private Antibody antibodyToEvaluate; private SubsetEvaluator ASEval = new WrapperSubsetEval(); private String threadName; public antibodyEvaluator(String name, SubsetEvaluator ASEvaluator, Antibody candidate) { this.threadName = name; this.ASEval = ASEvaluator; this.antibodyToEvaluate = candidate; } public void run() { try{ antibodyToEvaluate.evaluate(ASEval); antibodyToEvaluate.setFitness(antibodyToEvaluate.getAffinity(0)+Math.abs(antibodyToEvaluate.getAffinity(1))+antibodyToEvaluate.getAffinity(3)); System.out.printf("\n %s is successful.\n", threadName); } catch(NullPointerException ne) { System.out.printf("\nThread %s failed due to null pointer exception\n", this.threadName); }catch (Exception e) { // TODO Auto-generated catch block System.out.printf("\nError in evaluating %s.\n" + e.getMessage() , threadName); } } public Antibody getAntibody() { return antibodyToEvaluate; } } private void evaluatePopulation (SubsetEvaluator ASEvaluator, Antibody [] aPopulation) throws Exception { antibodyEvaluator[] evaluationTask = new antibodyEvaluator[aPopulation.length]; ExecutorService threadExecutor = Executors.newFixedThreadPool(2); for (int i=0;i<aPopulation.length ;i++) { evaluationTask[i] = new antibodyEvaluator("ET"+Integer.toString(i), ASEvaluator, aPopulation[i]); threadExecutor.execute(evaluationTask[i]); } threadExecutor.shutdown(); //Wait until all threads finish while (!threadExecutor.isTerminated()) {} }