CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Dec 2010
    Posts
    6

    java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Hello,
    I'm confronted to the following problem.
    An app I'm writing crashed due to the following exception (which by the book should never happen).

    Code:
    INFO   | jvm 2    | 2010/12/11 15:53:09 | Exception in thread "Thread-6" java.util.NoSuchElementException
    INFO   | jvm 2    | 2010/12/11 15:53:09 | 	at java.util.LinkedList.remove(Unknown Source)
    INFO   | jvm 2    | 2010/12/11 15:53:09 | 	at java.util.LinkedList.removeFirst(Unknown Source)
    INFO   | jvm 2    | 2010/12/11 15:53:09 | 	at java.util.LinkedList.poll(Unknown Source)
    INFO   | jvm 2    | 2010/12/11 15:53:09 | 	at my.package.RecorderImpl.run(RecorderImpl.java:95)
    INFO   | jvm 2    | 2010/12/11 15:53:09 | 	at java.lang.Thread.run(Unknown Source)
    Any idea what could have caused this?

    My code is very basic:
    Code:
    	public void run() {
    		Logger logger = Logger.getLogger("logger name");
    		while(true){			
    			try {
    				Thread.sleep(100); // waiting 100 millisecs
    			} catch (InterruptedException e) {
    				logger.error("async thread interrupted!", e);
    			} 
    			while (!this.quotes.isEmpty()) {
    				writeQuote(this.quotes.poll());
    			}	
    		}
    	}
    At other times, the app crashed due to OutOfMemoryError, so I'm wondering if that is not just a random Exception due to the same cause.
    Any help would be welcome.
    Thanks,
    Bruno

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    My code is very basic:
    No, what you have shown is very basic but that isn't all of your code.

    Presumably you are creating a thread to run this code and given that the error message says the exception is in Thread-6, I guess you are creating more than one thread. If you are accessing a LinkedList from multiple threads you need to provide some sort of synchronisation.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Dec 2010
    Posts
    6

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Hello keang,
    Thanks for your feedback.

    You are right, that's not all my code.
    Here is what it looks like as far as the list is handled.
    I didn't think I needed to synchronise add and poll, as add adds one more so it should not pose poll a problem (except that I realise now that it may do some not unitary operations which may cause incoherence). The other thing is: I thought the LinkedList implementation was synchronised which, on double checking the Javadoc, isn't....

    Should I synchronise recordQuote?
    Provided I remove the constraints of having to use one single connection, can I remove the synchronised on he writeQuote method?

    I may as well use a synchronised implementation, shouldn't I?

    Thanks again for your help.
    Bruno

    Code:
    [...]
    import java.util.LinkedList;
    import java.util.Queue;
    [...]
    
    
    public class RecorderImpl implements Runnable {
    	
    [...]
    
    
    	protected Queue<Quote> quotes = new LinkedList<Quote>();
    	
    	protected static Thread tradeRecorderThread; 
    	
    	public RecorderImpl(){
    	    	tradeRecorderThread = new Thread(this);
    	    	tradeRecorderThread.start();
    	}
    	
    
    	public void recordQuote(Quote quote){
    		quotes.add(quote);
    	}
    	
    	/**
    	 * method to write a quote in the database. Synchronised so that it can be invoked without risking a concurrency problem as their is only one db connection.
    	 * @param quote
    	 */
    	synchronized protected void writeQuote(Quote quote) {
    		Logger logger = 
    		try{
    			String insertQuery = "insert into t_quote etc.";
    			PreparedStatement psInsert = connection.prepareStatement(insertQuery);
    			psInsert.setInt(etc.)
    
    			psInsert.execute();
    		}
    		catch(Exception e){
    			logger.error("recording of quote failed", e);
    		}
    	}
    
    	@Override
    	public void run() {
    		Logger logger = Logger.getLogger("logger name");
    		while(true){			
    			try {
    				Thread.sleep(100); // waiting 100 millisecs
    			} catch (InterruptedException e) {
    				logger.error("async thread interrupted!", e);
    			} 
    			while (!this.quotes.isEmpty()) {
    				writeQuote(this.quotes.poll());
    			}	
    		}
    	}
    }

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    The other thing is: I thought the LinkedList implementation was synchronised which, on double checking the Javadoc, isn't....
    Yes, you must synchronise the list, the easiest way is as the JavaDocs suggest ie

    Code:
    Queue<Quote> quotes = Collections.synchronizedList(new LinkedList<Quote>());
    Should I synchronise recordQuote?
    No, see above.

    Provided I remove the constraints of having to use one single connection, can I remove the synchronised on he writeQuote method?
    I wasn't aware there was a potential concurrency issue with DB Connection that required it to be synchronized.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  5. #5
    Join Date
    Dec 2010
    Posts
    6

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    I tried the following
    Code:
    Queue<Quote> quotes = Collections.synchronizedList(new LinkedList<Quote>());
    with a proper cast in fact
    Code:
    Queue<Quote> quotes = (Queue<Quote>)Collections.synchronizedList(new LinkedList<Quote>());
    But it generates a ClassCastException at run time.
    I solved the problem using:
    Code:
    protected ConcurrentLinkedQueue<Quote> quotes = new ConcurrentLinkedQueue<Quote>();
    which seems to work.

    DB connections don't necessarily require synchronisation but you can't invoke two PreparedStatement on the same connection at the same time, this causes havoc (or it used to on older versions of JDBC at least... I'm back to development after many years without writing a line of code. Need to adapt to the changes which occurred in between...).
    As I was lazy I had opened only one connection for all my objects. I changed that part of the code so that each object now has its own DB connection. It's much cleaner. No need to synchronise writing to the DB.

  6. #6
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    But it generates a ClassCastException at run time.
    Oh yes, sorry, I wrote out the line without really thinking about it. There is no synchronized wrapper for the Queue interface.

    You are quite right about using a ConcurrentLinkedQueue.

    DB connections don't necessarily require synchronisation but you can't invoke two PreparedStatement on the same connection at the same time, this causes havoc (or it used to on older versions of JDBC at least... ).
    It may well still do, it's not something I've tried.

    BTW why are you creating a PreparedStatement and then throwing the prepared statement away after using it. The point of using a PreparedStatement is it can be created in advance, the DB can pre-compile the statement and then you just set the parameterised values when you need to reuse it. The precompilation step makes it more efficient than a standard statement but there's no advantage if you don't reuse them.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  7. #7
    Join Date
    Dec 2010
    Posts
    6

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Quote Originally Posted by keang View Post

    BTW why are you creating a PreparedStatement and then throwing the prepared statement away after using it. The point of using a PreparedStatement is it can be created in advance, the DB can pre-compile the statement and then you just set the parameterised values when you need to reuse it. The precompilation step makes it more efficient than a standard statement but there's no advantage if you don't reuse them.
    Thanks for spotting this! It just didn't occur to me that I could reuse the statement, as I was concentrating on the queue thing!
    Btw, even if I don't reuse it, I prefer to use PreparedStatement when there are a lot of arguments to pass, it makes the code cleaner than having to build the query string by hand, and taking care of all the proper formats (which may depend on the DB settings) for the different values you pass to your statement.

    Unlucky strike with my ConcurrentLinkedList...
    Code:
    INFO   | jvm 1    | 2010/12/14 23:00:56 | Exception in thread "Thread-5" java.lang.NullPointerException
    INFO   | jvm 1    | 2010/12/14 23:00:56 | 	at java.util.concurrent.ConcurrentLinkedQueue.offer(Unknown Source)
    INFO   | jvm 1    | 2010/12/14 23:00:56 | 	at java.util.concurrent.ConcurrentLinkedQueue.add(Unknown Source)
    INFO   | jvm 1    | 2010/12/14 23:00:56 | 	at my.app.RecorderImpl.recordQuote(RecorderImpl.java:50)
    INFO   | jvm 1    | 2010/12/14 23:00:56 | 	at my.app.CallingClass.recordQuote(CallingClass.java:118)
    INFO   | jvm 1    | 2010/12/14 23:00:56 | 	at my.app.CallingClass.run(CallingClass.java:166)
    INFO   | jvm 1    | 2010/12/14 23:00:56 | 	at java.lang.Thread.run(Unknown Source)
    I removed all synchronized modifiers thinking that now my list would take care of the issue but it seems not to... what do you think?

  8. #8
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    The JavaDocs say you get a NullPointerException if the element is null, are you sure the quote you are trying to set is not null?

    If this isn't the case please can you post the current code.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  9. #9
    Join Date
    Dec 2010
    Posts
    6

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Hi keang,
    Quote Originally Posted by keang View Post
    The JavaDocs say you get a NullPointerException if the element is null, are you sure the quote you are trying to set is not null?

    If this isn't the case please can you post the current code.
    I'm certain none of my quotes are null. However as a matter of precaution I changed the content of method to:

    Code:
    	public void recordQuote(Quote quote){
    		if (quote == null){
    			throw new NullPointerException("null quote received");
    		}
    		quotes.add(quote);
    	}
    which should tell me if a quote is null.

    Unfortunately the error does not seem reproducible.
    I do have, however, with the same application and in another context, a regular OutOfMemoryError. That is not good sign and I need to track where I could have some leaking resources.
    Any suggestion about a good tool to track how the memory is used during run time?
    Thanks for your help.

  10. #10
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Netbeans has a good profiler and is free. They used to be a separate downloads but they may be bundled these days.

    Eclipse also has a free profiler but I didn't had much success with it. Mind you that's probably because I struggled to set it up.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  11. #11
    Join Date
    Feb 2008
    Posts
    966

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Another free tool is VisualVM.

  12. #12
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Another free tool is VisualVM.
    Thanks, I'd not spotted that they had finally added a front end to the development tools. Is it any good?
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  13. #13
    Join Date
    Feb 2008
    Posts
    966

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Yeah, you can have all kinds of nice graphs and charts that make managers drool. They are even helpful to developers too . It is definitely one of the best free tools out there for profiling a Java virtual machine, at least that I have found.

  14. #14
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Yeah, you can have all kinds of nice graphs and charts that make managers drool. They are even helpful to developers too


    It is definitely one of the best free tools out there for profiling a Java virtual machine, at least that I have found.
    Excellent, I'll give it a go.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  15. #15
    Join Date
    Dec 2010
    Posts
    6

    Re: java.util.NoSuchElementException thrown from java.util.LinkedList.poll

    Excellent! I'll give it a go too.

    By the way keang, after many attempts, I found that the best class to use for queues in a multithread context is the set of BlockingQueues. For that matter, I used LinkedBlockingQueue in my code and so far it works wonders. No need to synchronize and it is possible to put the thread in wait mode (using the take() method) until new items are posted to the queue (using add() or put() methods).

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured