• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not sure how to find the issue in my program  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the group,

I have an ATM application that I have re-written many times and in different ways using threads. I have to say up front, this is my first experience with java so I am certainly a greenhorn! Also threads is a whole new adventure....I have put in many, many, many printlns into my application, and everything seems to be working. Here is what it should do:

I setup and Account, and initialize it to $0 (it is a float)

I send a BALANCE statement from a Client running on port 7777

The Server.java program is also listening on the same port and responding to the commands sent by the Client.java program.

I want to get an initial BALANCE back of $0

I want to make a $1,000 deposit

I want to get a BALANCE back of $1,000

I want to make a WITHDRAWAL of...you get the picture.

This worked in a previous assignment, but we had to separate out some of the code into other Class files, and implement threads.

My issue is, I don't know if I have a coding issue or a threads issue.

If I put in printlns right down to the Account.java app, I can see that when I do a DEPOSIT it deposits $1000, the issue is it just keeps depositing $1,000 and the goes on and on. I can tell I am in a while loop that previously before I split off portions would get a NULL back from the Client and end the program (I think), but now whatever command goes first, it just sits there and runs the same command receptively!!

I feel if someone could show me a better way of diagnosing my issue I could finish this.

thanks

spm
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There isn't enough information here, and I'm afraid a forum like this one is not a good venue for "How do I look for my problem"? To even begin, we would have to know more about your question that you're liable to be able to explain concisely, and putting lots of code here will, after you get it done, illustrate why it's difficult to deal with that as volunteer question-askers.

You've got trace statements; you're going to have to look at those critically. Half of debugging is starting off with the *conviction* that the bug is THERE IN YOUR CODE, not in a library, not a mistake in the language, not something that is going to go away if only you run the program often enough.

Good luck, and sorry I (we?) can't be of more help.

rc
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ralph makes some very good points. However, that doesn't mean all is lost. We may still be able to help. If you can provide an SSCCE that reproduces your problem, then somebody can probably help you figure out what's wrong. Getting it to reproduce can be tricky with multithreaded code. Even a small change (e.g., removal of superfluous stuff to get it down to a manageable size for a forum) can change how the threads interact. Still, if you can get it to happen say, one time in 5 or 10, then that may be enough.

I'll even stick my neck out and say that the problem you describe sounds like thread T1 is updating a variable, but T2 is not seeing the updates, and is continuing to work off of the old value, which is caused because you didn't make proper use of synchronization or volatile (or, once you understand the low-level stuff, java.util.concurrent.*).
 
spm Martino
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hear what both of you are saying and thanks for responding...I tried to write an SSCCE before but it took forever or I could not replicate the problem. I suspect the threading issue also, but being new to Java, I don't know if it is my bad programming, or the thread hammering me. If I send a DEPOSIT command of say DEPOSIT(1000), and println it, I see it add 1000 to the balance, and then forever continue to add 1000 to balance until I CTRL-C the program...I have tried just starting 1 thread (even though I think java is starting its own without the one I start), doesn't resolve the issue.. If I go to the constructor of ever .java program involved and put in a getBalance(), deposit(1000), withdrawal(500), I get the correct results on the client side, I just don't get the results running it through the application threads, or perhaps my coding issues.


Jeff Verdegan wrote:Ralph makes some very good points. However, that doesn't mean all is lost. We may still be able to help. If you can provide an SSCCE that reproduces your problem, then somebody can probably help you figure out what's wrong. Getting it to reproduce can be tricky with multithreaded code. Even a small change (e.g., removal of superfluous stuff to get it down to a manageable size for a forum) can change how the threads interact. Still, if you can get it to happen say, one time in 5 or 10, then that may be enough.

I'll even stick my neck out and say that the problem you describe sounds like thread T1 is updating a variable, but T2 is not seeing the updates, and is continuing to work off of the old value, which is caused because you didn't make proper use of synchronization or volatile (or, once you understand the low-level stuff, java.util.concurrent.*).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
spm Martino wrote:I suspect the threading issue also, but being new to Java, I don't know if it is my bad programming, or the thread hammering me.


Well, if your program is not behaving according to requirements, then it's definitely due to an error on your part. :-)

If I send a DEPOSIT command of say DEPOSIT(1000), and println it, I see it add 1000 to the balance, and then forever continue to add 1000 to balance until I CTRL-C the program...


So, if something else is supposed to see that 1000 DEP and stop now that BAL is 1000, then either a) That something else is in another thread, and as I mentioned, you have not properly used syncing or volatile to ensure the other thread sees it, or b) You have made some other more mundane error, totally unrelated to multithreading, such as having a local variable that hides a member variable. That's as far as I'm willing to speculate based on a description of the problem, absent an SSCCE.

I have tried just starting 1 thread (even though I think java is starting its own without the one I start), doesn't resolve the issue..


The JVM will start at least one, thread that you needn't worry about, and probably several. Those are its own bookkeeping threads for stuff like GC, and will not affect the running of your program, other than that how they get scheduled may happen to make threading-related bugs in your code easier or harder to reproduce and track down.

If I go to the constructor of ever .java program involved and put in a getBalance(), deposit(1000), withdrawal(500), I get the correct results on the client side, I just don't get the results running it through the application threads, or perhaps my coding issues.


I'm not sure what you mean by the threads "or" your coding issues. It's definitely a bug in your code. The question (or one question) is whether your coding bugs are related to multithreading or not. Is that what you meant?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to make one random guess. It is not that likely to hit the mark, because we still have little information.

The most likely place for a loop in the code that receives the deposit is where it is reading the message that comes in. It reads until it has a full buffer or reaches a delimiter or something, and then does something with the result.

Is that within a loop? Under what conditions does the loop continue? Are you terminating the loop correctly?

It also likely loops back to wait for the next message. Does that loop clear out the previous message correctly?

Failing either of those, I suggest you start cutting back on what your server program does. Have it receive ONE message, do something correctly with it, and then exit. THEN add looping so that it receives a message, performs an action, and then waits for the next message. If you're stumbling over your steps, take smaller steps. If you still have a bug, you may end up with something that is small enough to post.

rc
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!