Link to home
Start Free TrialLog in
Avatar of broodblik
broodblik

asked on

Thread behaviour

Hi, I use threads in a program which structure is defined as follow:

class name1
{
    .....
    .....
    name1 MGU = new name2 ();
    MGU.start();    
    .....
    class name2 extends thread
    {
        ......
    }

}

now what will happen if the thread stalled?
My whole program stalled.
Must the thread class be defined outside the name1 class? or can I assume that there are a bug in my name1 class like follows.

class name1
{
    .....
    .....
    name1 MGU = new name2 ();
    MGU.start();    
    .....
}
class name2 extends thread
{
    ......
}

thanks allot
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

moving it outsde the class will make no difference...

I guess you either have a race condition, or you wait() for something that is never notify() ed ;-)

Tim
Avatar of JakobA
JakobA

In the thread do you ever instruct it to yeld so other threads may also get some time on the CPU. Javas thread scheduling is primarily cooperative so if you do no, then that one started thread wil sit on the CPU forever.

in http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html look at methods:
   sleep( long millis);
   yield( );

regards JakobA
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> Must the thread class be defined outside the name1 class?
If you define it inside as non-static, you'll get 2 'this' pointers accessible from the name2 methods :
    'this' which is your current name2 object
    'name1.this' which is your current name1 object
I suggest you to define it outside if you don't need to use the name1 current object. You can also define it inside as a static class if name2 don't have static methods.
But it won't make any difference to it's operation...