File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Applet freezing.. Problem with thread? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Applet freezing.. Problem with thread?" Watch "Applet freezing.. Problem with thread?" New topic

Applet freezing.. Problem with thread?

Jon Mattson

Joined: Feb 07, 2006
Posts: 19
Well I've made this really simple game where you need to shoot terrorists.
You can try it here

The problem is it freezes at random times. Sometimes not at all. I suspect it's something wrong with the way i use threads but I haven't been able to find the problem.

Any other suggestions to make it work better are appriciated.

Here is my code:

[ February 07, 2006: Message edited by: Jon Mattson ]
Jon Mattson

Joined: Feb 07, 2006
Posts: 19
I've uploaded a new version here

I've been trying to solve this problem by placing System.out.println statements in different parts of the program. Remember you might have to run the applet a few times before it freezes and don't use the retry button to try again. Just refresh the page.

Here's the code again with some small modifications like adding the println statements. I'm sorry I have to post the whole code but I don't really know where the problem is so I post it all. I put bold on all println statements and some lines of code that got to do with what happens when hp reaches 0, theese stop or start threads.

Here is the last lines in console after a freeze. It always ends in Ter1dead:true
You can also try the applet yourself and check the console the println statements are there.

Those I took when the applet had freezed.

Help will be appriciated

[ February 08, 2006: Message edited by: Jon Mattson ]

[ February 08, 2006: Message edited by: Jon Mattson ]
[ February 08, 2006: Message edited by: Jon Mattson ]
Jeremy Tartaglia
Ranch Hand

Joined: Mar 11, 2004
Posts: 62
There are a few things which concern me. Why do you have your run methods as synchronized? They should only be called once, so there's no need to have them synchronized. Plus, synchronization causes the JVM to lock objects, which could cause some problems. I stay away from synchronized until I notice deadlocks occuring.

Another is the volatile Thread property. Why? Volatile is used when the property will be changed by an outside source, ie: not in the program. A hardware register changes, like the VBlank bit on a video card, or the address where the hardware timer stores the current time. Those are all volatile.

I don't think those are the problems, but I'd start there.
Jon Mattson

Joined: Feb 07, 2006
Posts: 19
Sorry haven't been able to post been busy in school.

I uploaded yet another version of the applet. Now I somehow managed to get the retry button work and I removed the volatile and synchronized statements. This freezing problem is annoying cuz it seems to happen pretty randomly. But now there seems to be less of it. Didn't manage to make it freeze in the web browser but it did freeze in Eclipse. I still hope someone could find something in my code that could cause freezing.

Now I'm planning to make a hiscore system to that game which displays the scores on a webpage. I've been searching on google about it but haven't found that much info about it. I read somewhere about someone who used a php page which the applet updated with some command. Could someone lead me to the right direction about this issue? To a tutorial or class I should use. :roll:
I agree. Here's the link:
subject: Applet freezing.. Problem with thread?
It's not a secret anymore!