• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Occasionally Hangs

 
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm having an issue where the code that I have written sometimes ( seems to be about 1:30 ) hangs.   Most times it runs in milliseconds but then sometimes it just runs and takes about 30 seconds to finish.   The code is a couple hundred lines across seven classes so I didn't want to paste it all in here.  Anyone have suggestions on how to debug?  Since it is very infrequent all my normal debugging methods are kind of suspect.

Ahh forgot to say I'm executing it directly from within Eclipse if that might impact.
 
Marshal
Posts: 3862
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your code doing?

Does it access any external resources (file system, database, network, console)?  

Are you using any libraries?
 
Lloyd Hatch
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is my app to practice Java and try and utilize everything in Java 17 so I can pass that exam.  I'm trying to use a little of everything.  

It is eventually going to be a simple space trading game, but all I have right now is the initial set up, loading commodities from a JSON file, creating space stations that will buy or sell items from the commodity list.  

I'm using ArrayList, simple JSON, math and that's about it.   So yes it does access the file system to read the JSON file.  Do you think that might be where it is hanging up?
 
Marshal
Posts: 75837
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. Once a file has been found, reading it is usually fast. Try some print statements
 
Ron McLeod
Marshal
Posts: 3862
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ron McLeod wrote:What is your code doing?


Lloyd Hatch wrote:I'm trying to use a little of everything ...


Try and isolate where the problem might be by progressively commenting-out bits of code until you no loger experience the hang issue.  Since you say that this problem only shows-up around once out of every 30 times, you will of course need some automated way to run your application many times with each change.
 
Lloyd Hatch
Greenhorn
Posts: 29
1
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all ...

I ended up solving it by puting a  println in every code block.  Turned out to be a poorly formulated loop that only got called if certain circumstances were met ... thus the one in thirty.  
 
Saloon Keeper
Posts: 9344
78
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lloyd Hatch wrote:I ended up solving it by puting a  println in every code block.


That's the kind of roll-up-your-sleeves debugging that I wish more people who visit here would attempt. It's not difficult but it takes some effort.
 
Campbell Ritchie
Marshal
Posts: 75837
361
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have half a cow for working out what the problem was. And the other half for telling us. It might be worth seeing the offending loop please.
 
Master Rancher
Posts: 242
8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lloyd Hatch wrote:Thanks all ...

I ended up solving it by puting a  println in every code block.  Turned out to be a poorly formulated loop that only got called if certain circumstances were met ... thus the one in thirty.  



Thanks for a) reporting back at all and b) with an explanation what you did to find and fix the issue and c) to tell us the issue.

A cow well earned. Mooh.
 
Lloyd Hatch
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry it took me a while to get back here and post this.    My problem was the else statements were missing on the second for loop so in the case that I had a match on the buy list it was going into an endless loop.  



I said above that it was hanging for about 30 seconds, but it should have gone into an endless loop.   Does eclipse auto kill endless loops?
 
Campbell Ritchie
Marshal
Posts: 75837
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Labelled continue;?
There has to be a better way to write that loop. Also never use the same object operator == to test equality of reference types (link). Use the equals() method instead.
 
Lloyd Hatch
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The comparison itself is happening between two integer instance variables that are part of the commodity class.    I thought == was safe for primitive types like that?   Would you still recommend switching to equals()?

As for the loop I agree it is clunky.  The object is to check to make sure that the commodity that I'm selecting randomly doesn't exist on either of the two commodity lists buyList or sellList.   I had thought I could make it better by combining the two lists since they are both commodity types, and that would eliminate the second for loop, but that would still leave the continue.  Any suggestions on that?
 
Ron McLeod
Marshal
Posts: 3862
540
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lloyd Hatch wrote:... but that would still leave the continue.  Any suggestions on that?


I would start by factoring-out the two tests in to their own methods.  After that, you could use a logical OR to operate on the results of the tests.  With the logical OR, the second test would not be performed if the first one return true (item found in list).

Something like this:
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic