thread priority question...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Padhu Vinirs

    thread priority question...

    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().st art();
    }

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

    public void printWorkerStat us() {
    while ( t1.isAlive() || t2.isAlive() ) {
    t1.printStatus( ); // this print never happens
    t2.printStatus( ); // this print never happens
    Thread.sleep(40 *1000);
    }
    }
    }


  • Marek Puchalski

    #2
    Re: thread priority question...

    Padhu Vinirs wrote:
    [color=blue]
    > public void printWorkerStat us() {
    > while ( t1.isAlive() || t2.isAlive() ) {
    > t1.printStatus( ); // this print never happens
    > t2.printStatus( ); // this print never happens
    > Thread.sleep(40 *1000);
    > }[/color]

    This code will not compile. Thread.sleep method throws an
    InterruptedExce ption 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

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

    Comment

    • Padhu Vinirs

      #3
      Re: thread priority question...


      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

      "Marek Puchalski" <marpuch+news@g mail.com> wrote in message
      news:dumiqo$1kq a$1@node3.news. atman.pl...[color=blue]
      > Padhu Vinirs wrote:
      >[color=green]
      >> public void printWorkerStat us() {
      >> while ( t1.isAlive() || t2.isAlive() ) {
      >> t1.printStatus( ); // this print never happens
      >> t2.printStatus( ); // this print never happens
      >> Thread.sleep(40 *1000);
      >> }[/color]
      >
      > This code will not compile. Thread.sleep method throws an
      > InterruptedExce ption 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
      >
      > --
      > # You can't run away. Everyone's connected.
      > # Marek Puchalski
      > # Proud linux user: 409592[/color]


      Comment

      • Marek Puchalski

        #4
        Re: thread priority question...

        Padhu Vinirs wrote:[color=blue]
        > 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...
        > }[/color]

        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.prin tln( 111 );
        }
        }

        public class MainThread
        {
        WorkerThread t1, t2;

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

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

        public void printWorkerStat us()
        {
        while ( t1.isAlive() || t2.isAlive() )
        {
        t1.printStatus( ); // this print never happens
        t2.printStatus( ); // this print never happens
        try
        {
        Thread.sleep( 40 * 1000 );
        }
        catch ( InterruptedExce ption e )
        {
        // TODO Auto-generated catch block
        e.printStackTra ce();
        }
        }
        }
        }

        and the output is like:

        print
        111
        111
        111
        111 (...)

        Hope this helps.

        Marek

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

        Comment

        • Oliver Wong

          #5
          Re: thread priority question...


          "Padhu Vinirs" <prabha_pady@co mcast.net> wrote in message
          news:sOadnXdHBf ETT5PZRVn-tA@comcast.com. ..[color=blue]
          >
          > 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[/color]

          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

          Comment

          • Dobromir Gaydarov

            #6
            Re: thread priority question...

            The MainThread is not properly coded to be a Thread - it does not
            impletement the java.lang.Runna ble 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

            "Padhu Vinirs" <prabha_pady@co mcast.net> wrote in message
            news:CuSdnTtRE9 a4WZPZRVn-qQ@comcast.com. ..[color=blue]
            > 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().st art();
            > }
            >
            > public void start() {
            > t1 = new WorkerThread();
            > t2 = new WorkerThread();
            > t1.start();
            > t2.start();
            > // even this is not printed...
            > System.out.prin tln("print");
            > printWorkerStat us();
            > }
            >
            > public void printWorkerStat us() {
            > while ( t1.isAlive() || t2.isAlive() ) {
            > t1.printStatus( ); // this print never happens
            > t2.printStatus( ); // this print never happens
            > Thread.sleep(40 *1000);
            > }
            > }
            > }
            >[/color]


            Comment

            • Padhu Vinirs

              #7
              Re: thread priority question...

              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

              "Dobromir Gaydarov" <notme@sympatic o.ca> wrote in message
              news:u7XPf.3149 $xM2.237984@new s20.bellglobal. com...[color=blue]
              > The MainThread is not properly coded to be a Thread - it does not
              > impletement the java.lang.Runna ble 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
              >
              > "Padhu Vinirs" <prabha_pady@co mcast.net> wrote in message
              > news:CuSdnTtRE9 a4WZPZRVn-qQ@comcast.com. ..[color=green]
              >> 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().st art();
              >> }
              >>
              >> public void start() {
              >> t1 = new WorkerThread();
              >> t2 = new WorkerThread();
              >> t1.start();
              >> t2.start();
              >> // even this is not printed...
              >> System.out.prin tln("print");
              >> printWorkerStat us();
              >> }
              >>
              >> public void printWorkerStat us() {
              >> while ( t1.isAlive() || t2.isAlive() ) {
              >> t1.printStatus( ); // this print never happens
              >> t2.printStatus( ); // this print never happens
              >> Thread.sleep(40 *1000);
              >> }
              >> }
              >> }
              >>[/color]
              >
              >[/color]


              Comment

              Working...