Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How we can isolate the variables a thread use?

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    4
    My Mood
    Dead
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How we can isolate the variables a thread use?

    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()) {}
     
          }
    Last edited by Shahram; October 12th, 2011 at 08:03 AM. Reason: Solved


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: How we can isolate the variables a thread use?

    The problem is when one thread change the value of one of these parameters other threads see the changes.
    Which variables are shared by the threads?
    Why do the threads change the contents of the passed objects?
    Can you clone the objects so each thread gets its own version.

    Can you make a small simple program that compiles and executes to demonstrate your problem?
    Last edited by Norm; September 19th, 2011 at 10:26 AM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Shahram (October 12th, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How we can isolate the variables a thread use?

    I want to isolate these in each thread.
    What do you mean isolate? Do you mean you want each thread to have its own copy - so that one thread cannot change the values used by another? If so, you will have to deep copy/clone the objects.

    Among 30 thread created
    Why so many threads? In most cases, I'd guess you are loosing a lot of efficiency by creating so many threads.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Shahram (October 12th, 2011)

Similar Threads

  1. Help with clearing variables
    By fmr in forum Other Programming Languages
    Replies: 2
    Last Post: July 18th, 2011, 10:18 AM
  2. Scope of Variables
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: June 11th, 2011, 10:22 AM
  3. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 09:13 AM
  4. variables and efficiency
    By catkinq in forum Java Theory & Questions
    Replies: 3
    Last Post: February 7th, 2010, 06:09 AM
  5. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM