Results 1 to 3 of 3
  1. #1
    kosko is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default Passing params by ref

    Here is sample code.

    Parser.java:

    PHP Code:
    package org.parser.ts;
    
    import java.io.InputStream;
    
    import javax.xml.stream.XMLInputFactory;
    import javax.xml.stream.XMLStreamException;
    import javax.xml.stream.XMLStreamReader;
    
    public class Parser {
    
    	public Result parse(InputStream xmlStream) throws XMLStreamException {
    		Result result = new Result();
    		XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(xmlStream);
    		
    		while(reader.hasNext()) {
    			if(reader.isStartElement() && reader.getLocalName().equals("Person")) {
    				parsePerson(reader, result);
    			}
    			if(reader.isEndElement() && reader.getLocalName().equals("Person")) {
    				break;
    			}
    			reader.next();
    		}
    		
    		return result;
    	}
    
    	private void parsePerson(XMLStreamReader reader, Result result) throws XMLStreamException {
    		reader.next();
    		while(reader.hasNext()) {
    			if(reader.isStartElement()) { 
    				if(reader.getLocalName().equals("FirstName")) {
    					result.setFirstName(reader.getElementText());
    				} else if (reader.getLocalName().equals("LastName")) {
    					result.setLastName(reader.getElementText());
    				}
    			}
    			if(reader.isEndElement() && reader.equals("Person")) {
    				break;
    			}
    			reader.next();
    		}
    	}
    }
    and Result.java

    PHP Code:
    package org.parser.ts;
    
    public class Result {
    	private String firstName;
    	private String lastName;
    	
    	public String getFirstName() {
    		return firstName;
    	}
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    	public String getLastName() {
    		return lastName;
    	}
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    	
    }
    Is this code thread safe? Is passing 'person' (local variable) to method 'parsePerson()' by reference, as I did here, thread safe? And more important question is why? I really want to understand why it is/isn't thread safe

  2. #2
    Horcrux7 is offline Member
    Join Date
    Sep 2010
    Posts
    33
    Rep Power
    0

    Default

    Yes, this code is thread safe. Because all data are saved on the stack of the thread. There is no object that are shared between different calls of different threads.

    If you would safe the Result object as member variable in the Parser then there is only one place for all threads to safe different results.

    I hope this answer your question.

  3. #3
    kosko is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Passing a value..
    By Lagarto in forum New To Java
    Replies: 3
    Last Post: 01-23-2011, 06:58 PM
  2. passing value
    By KumbhaniMehul in forum Java Servlet
    Replies: 1
    Last Post: 04-14-2010, 10:52 AM
  3. Passing Value
    By katherine_93 in forum New To Java
    Replies: 1
    Last Post: 03-08-2010, 06:56 AM
  4. Passing value....
    By casid in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-03-2010, 12:19 PM
  5. passing something
    By dinosoep in forum Threads and Synchronization
    Replies: 2
    Last Post: 12-05-2009, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •