Threads

HiI have this multi thread applet. When I run it, it runs fine for some time. After a couple of minutes the threads hang up. Does it have something to do with timeout. Please help me on this.ThanksJayanti
[255 byte] By [jayantis] at [2007-9-26 2:51:14]
# 1

The most frequent cause of this kind of lockup is a deadlock. This means thread 1 is waiting for thread 2 to do something while thread 2 is waiting for thread 1 to do something. Neither does anything, so they wait some more. And more, and more...

Carefully check anywhere in your program that threads wait or synchronize.

good luck

Kris

KrisP at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 2
Can you post the code
mattbunch at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 3

Its a pretty long code. There are these numerous classes implementing the Runnable interface. They do different stuff. The methods calculate the point value, for the rendering to be done in the applet.In the main applet I call the methods using threads . Each thread is activated if I click in a particular area . The threads start running if the mouseclick event occurs, and using a method the point values are calculated using pt.X and pt.Y. What I am trying to do is run many animations on a background which do things simultaneously if the events occur. I have used Thread.sleep(10); in all the classes to give the other threads a chance to do their stuff.public void getValues(){

if(mouseClick == 1 && a==0){

if(Clappersound==false){clapper.loop();Clappersound=true;}

theStar.start();

a++;

}

if(a==1 && mouseClick >=1){

try{

pt=st8.readPoint();

xVal=pt.x;yVal=pt.y;

}catch(NoSuchMethodError e){}

currentimg=l.returnLampImage();repaint();--and more.

}

In run I call this method and also put THread.currentThread().sleep(10);

This works fine for a couple of aminutes. Then the animations get jerky and then stop.

I would really appreciate it if you could help out.

Thanks

Jayanti

jayantis at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 4

When you find yourself adding arbitrary Thread.sleep (or System.gc or any other method, for that matter) calls to your program to attempt to get it to work, you're grasping at straws. Get a good book on multithreaded programming Java such as Concurrent Programming in Java by Doug Lea. Read it and understand the situation. When you understand the situation, you will likely be able to solve the problem easily.

schapel at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 5

Hello Jayanti,

did you check the output on Java Console of your browser? In Netscape it's under Communicator/Tools. It provides also a dump of all threads actually running. In order to recognize you threads you should name it using different names (a parameter on constructing).

Hope it will help you.

Martin

edosoft at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 6

Hi,

for running permanent animations, I tend to avoid sleep() and use yield() instead: yield() involves less overhead and therefore be used more frequently. This also reduces yerkiness in the animation, as available processor time becomes more constant. To avoid locking up the processor completely, use a low priority on the drawing tasks. I find, that on an NT-system, I can have 100% CPU usage while my GUI still works responsively.

One reason for your problem could be, that your Gui locks up, because user-input cannot be processed in time. This can lead to inconsistent states of global variables or delays in sychronised methods. Lost user input on top of that.

Last thing: Do try to re-start an existing thread, or do you keep them running or do you create new ones all the time? A re-start does not work, i.e. if a thread has returned from from run(), it should not be used again even if you still have a reference to it.

Hope this gives you some ideas!

Phenningsen at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 7

Hi

Thanks all for your inputs. I had better read up about multithreading first , as u say.

In my program in the mouseclick event I use a method in the thread class to make a boolean true. Only if this is true, the computing of x,y values take place . In my getValues() in the applet, I start the threads the first time if the mouseclick event occurs. The threads are now in the run. So every time there is click the boolean becomes true and the computing starts. At the end of the method the boolean is again set false. Should this boolean be a volatile variable? Is it not OK to keep the threads in run, all the time. It is very irritating problem.

Jayanti

jayantis at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 8

> So every time

> there is click the boolean becomes true and the

> computing starts. At the end of the method the

> boolean is again set false.

I'm not sure to understand what you mean, but if what you want is to have some threads waiting for an event, and then start working when that event occurs, ans then waiting again until the next event and so on, you should have a look at Object.wait() and Object.notify(). When a worker thread calls wait() on an object, it is paused until another thread (probably your GUI thread) calls notify() on that object.

JCG

jcgagne at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 9
I have used wait() and notifyAll(). My animations work better when the page is reloaded. Is there a way to ensure that all images are loaded before animation starts?Thanks.
jayantis at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 10
Isn't that what java.awt.MediaTracker is for?
schapel at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...
# 11
Thanks everbodyyield() and MediaTracker solved most of my problems.Jayanti. Only a slight disturbance is seen when I play 2 sound files simultaneously.
jayantis at 2007-6-29 10:38:24 > top of Java-index,Core,Core APIs...