This discussion is archived
6 Replies Latest reply: Feb 17, 2013 1:41 PM by baftos RSS

Is the main program seen as a separate thread ?

KonTiki Newbie
Currently Being Moderated
I wanted to stop a GUI based program opening too fast because the splash screen wasn't seen for long enough.

So I tried to use the Thread and Runnable classes within the GUI constructor.
By trying different things, I got it to do as I wanted : a delay of 10 seconds before showing the program's GUI.
But I don't understand why main is not seen as a separate thread and thus continue to make itself visible no matter what the delayThread status may be.
// Constructor:
// ---------------
public Hotel (int numberIn)
    {
         // Create the time delay thread so that users may enjoy the splash !
         delayThread = new Thread(this);
         delayThread.run();
         // Initialise the number of rooms and the GuestList :        
        noOfRooms =  numberIn;
        guests  = new GuestList(noOfRooms); 
        // Graphics settings:      
        setTitle("Hotel");     
        setLayout(new FlowLayout());
       // etc. etc.
       // etc. etc.
       setVisible(true);
   }


    // A run method to delay the program opening:
     
    public void run()
    {     
         try
         {
              delayThread.sleep(10000);
         }
         catch(InterruptedException ie)
         {
              ie.printStackTrace();
         }
       
    }
Now, maybe a better way to do this would be to move the setVisible statement from the constructor to the run method.
Maybe there is an even better mechanism for producing a delay in a program's opening.
But I'm still curious as to why the main thread was delayed by a delay in what is really an entirely separate thread.
Any ideas ?

Edited by: KonTiki on 16-Feb-2013 13:10

Edited by: KonTiki on 16-Feb-2013 13:19

Edited by: KonTiki on 16-Feb-2013 13:20

Edited by: KonTiki on 16-Feb-2013 13:21

Edited by: KonTiki on 17-Feb-2013 09:16
  • 1. Re: Is the main program seen as a separate thread ?
    baftos Expert
    Currently Being Moderated
    Are you sure you show all relevant code? I think that, as it is, the sleeping thread does nothing.
  • 2. Re: Is the main program seen as a separate thread ?
    KonTiki Newbie
    Currently Being Moderated
    You are right.

    To get the delay I used :

    delayThread.run();      

    instead of :

    delayThread.start();      

    I've corrected this in the code segment above.

    I'd thought that using .start() on the delayThread was effectively the same as .run() . . .
    But it seems .run() produces a delay while .start() does not . . .

    Curious and curiouser.
    Seems to make no difference whether setVisible is placed in the constructor or in the run method.

    Edited by: KonTiki on 17-Feb-2013 09:21
  • 3. Re: Is the main program seen as a separate thread ?
    DrClap Expert
    Currently Being Moderated
    Well, first of all calling a Thread object's run() method just runs the code in the same thread. It doesn't start a new thread -- that's what calling the start() method does.

    And the Thread.sleep() method is a static method, meaning "Make *this* thread sleep for X milliseconds". So calling it on another Thread object doesn't cause that thread to sleep, it causes the current thread to sleep.

    I suggest you should go through the concurrency tutorial from Oracle because you seem to be programming by guesswork here.
  • 4. Re: Is the main program seen as a separate thread ?
    KonTiki Newbie
    Currently Being Moderated
    Ok. Thanks.

    That explains why I got the delay effect with .run() and not with .start().

    I agree that I need to read more on threads & concurrency as my present book focuses on interactions between 2 or more threads; or else on using Timer classes to handle animation sequences.

    I take it that there is no other way to induce a delay in the constructor than by use of .run() & .sleep(..) ?

    Edited by: KonTiki on 17-Feb-2013 12:11
  • 5. Re: Is the main program seen as a separate thread ?
    baftos Expert
    Currently Being Moderated
    I guess you editted your original post to put run() there instead of start() or my first answer was an oversight (if you had run() all along).
    Don't do this, if you did it. It's not my ego, but the whole thing makes no sense if someone reads it now. I blame Oracle that editting is allowed after someone answers.

    If you want a 10 seconds delay, you don't need a thread and thread.run(). Simply Thread.sleep(10000) would do it.
  • 6. Re: Is the main program seen as a separate thread ?
    KonTiki Newbie
    Currently Being Moderated
    Yes.

    Thanks for that.

    Maybe Oracle themselves ought publish THE introductory book on Java.
    I know the tutorials are there online but my eyes prefer a book.

    Thanks to all contributors.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points