Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Thread behaviour

Posted on 2005-04-11
5
Medium Priority
?
252 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:broodblik
5 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 13751583
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
0
 
LVL 15

Expert Comment

by:JakobA
ID: 13751754
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
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 13751847
>> name1 MGU = new name2 ();

You mean: name2 MGU = new name2 () ;

>> Must the thread class be defined outside the name1 class?

Should not be a problem whether you keep it inside or outside. While keeping it inside as a non-static, non-public class, you mandate that somebody must need an object of the outer class to create an object of the inner-class, and it can be done only in the current package.
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 13760401
>> 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.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13760418
But it won't make any difference to it's operation...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question