Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

thread priority question...

Thread view: 
Padhu Vinirs - 08 Mar 2006 11:46 GMT
JDK 1.4 on WinXP. I have 2 threads started from the main thread. I would
like to print some status of the child threads from the main thread
periodically. But I dont see the main thread to print out anything when the
child threads are running. The child threads do sleep after some work.
Instead should I make the child threads wait() and make the main thread
notifyAll() after printing ?

public class MainThread {
WorkerThread t1, t2;
public static void main(String[] args) {
   new MainThread().start();
}

public void start() {
   t1 = new WorkerThread();
   t2 = new WorkerThread();
   t1.start();
   t2.start();
   // even this is not printed...
   System.out.println("print");
   printWorkerStatus();
}

public void printWorkerStatus() {
   while ( t1.isAlive() || t2.isAlive() ) {
       t1.printStatus(); // this print never happens
       t2.printStatus(); // this print never happens
       Thread.sleep(40*1000);
   }
}
}
Marek Puchalski - 08 Mar 2006 12:32 GMT
> public void printWorkerStatus() {
>     while ( t1.isAlive() || t2.isAlive() ) {
>         t1.printStatus(); // this print never happens
>         t2.printStatus(); // this print never happens
>         Thread.sleep(40*1000);
>     }

This code will not compile. Thread.sleep method throws an
InterruptedException which is not handeled anywhere. Fix it. The rest of
the code seems ok. Should you have more problems, post the content of
the printStatus method.

Hope this helps

Marek

Signature

# You can't run away. Everyone's connected.
# Marek Puchalski
# Proud linux user: 409592

Padhu Vinirs - 08 Mar 2006 12:48 GMT
This was sample code I typed in here. Please assume that the exceptions are
caught etc. printStatus is very simple:

public void printStatus() {
   // some system.out of thread's state variables...
}

Thanks

-- padhu

>> public void printWorkerStatus() {
>>     while ( t1.isAlive() || t2.isAlive() ) {
[quoted text clipped - 11 lines]
>
> Marek
Marek Puchalski - 08 Mar 2006 15:32 GMT
> This was sample code I typed in here. Please assume that the exceptions are
> caught etc. printStatus is very simple:
>
> public void printStatus() {
>     // some system.out of thread's state variables...
> }

It's a pain to try to find bugs in a sample code. See this. It works fine.

class WorkerThread extends Thread
{
    public void run()
    {
        while ( true )
            ;
    }

    public void printStatus()
    {
        System.out.println( 111 );
    }
}

public class MainThread
{
    WorkerThread t1, t2;

    public static void main( String[] args )
    {
        new MainThread().start();
    }

    public void start()
    {
        t1 = new WorkerThread();
        t2 = new WorkerThread();
        t1.start();
        t2.start();
        // even this is not printed...
        System.out.println( "print" );
        printWorkerStatus();
    }

    public void printWorkerStatus()
    {
        while ( t1.isAlive() || t2.isAlive() )
        {
            t1.printStatus(); // this print never happens
            t2.printStatus(); // this print never happens
            try
            {
                Thread.sleep( 40 * 1000 );
            }
            catch ( InterruptedException e )
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

and the output is like:

print
111
111
111
111 (...)

Hope this helps.

Marek

Signature

# You can't run away. Everyone's connected.
# Marek Puchalski
# Proud linux user: 409592

Oliver Wong - 08 Mar 2006 18:14 GMT
> This was sample code I typed in here. Please assume that the exceptions
> are caught etc. printStatus is very simple:
[quoted text clipped - 4 lines]
>
> Thanks

   If I assume you've done everything correctly, then yes, your code works
perfectly. Since it's not working perfectly, perhaps the assumption that
you've done everything correctly is invalid.

   Please post an SSCCE. http://mindprod.com/jgloss/sscce.html

   - Oliver
Dobromir Gaydarov - 09 Mar 2006 14:40 GMT
The MainThread is not properly coded to be a Thread - it does not
impletement the java.lang.Runnable interface. If we assume you coded the
WorkerThread the same way your program executes sequentially and therefore
you will see your output from the main "thread" as soon as the child
"threads" complete.

Regards,
Dobromir

> JDK 1.4 on WinXP. I have 2 threads started from the main thread. I would
> like to print some status of the child threads from the main thread
[quoted text clipped - 27 lines]
> }
> }
Padhu Vinirs - 13 Mar 2006 01:38 GMT
The main thread is the starting class ( with the main method ). That is the
default thread. It doesnt need to implement Runnable. My question was
related to the main thread being able to print status of threads it started.

-- padhu

> The MainThread is not properly coded to be a Thread - it does not
> impletement the java.lang.Runnable interface. If we assume you coded the
[quoted text clipped - 36 lines]
>> }
>> }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.