This content has been marked as final. Show 6 replies
Are you sure you show all relevant code? I think that, as it is, the sleeping thread does nothing.
You are right.
To get the delay I used :
instead of :
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
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.1 person found this helpful
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.
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
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).1 person found this helpful
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.
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.