• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pause run between two lines of a program  RSS feed

 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to pause program run for 5 secs between two lines of a program. I have tried this: Thread.sleep(4000); But it is not working, it gives some pause before I would like to close the program.
I would like to delay the processing in some case. My program have a GUI with frames.

How can I correct it?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.sleep(4000) will pause the current thread for 4 seconds. Without seeing your code I have no idea why it is not behaving as you were expecting.

Are there multiple threads in your program? If you call Thread.sleep() in one Thread it will have no affect on any other Threads.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a GUI program, then it makes a difference whether it's the event thread which is sleeping or some other thread. I would recommend reading the Swing threading tutorial to see the correct way to handle threads in a GUI program.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only 1 thread in the program. I think I should pause dispatch thread.

What should I write in the actionPerformed function? What will it do?


 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kovacs Akos wrote:There is only 1 thread in the program.

The fact that you don't create any new threads does not mean that there is only one.
For example this program prints 6 on my machine.
The threads are:
Signal Dispatcher
Reference Handler
Finalizer
main
Monitor Ctrl-Break
Attach Listener

This program prints 10.
The threads are:
AWT-EventQueue
AWT-Shutdown
Reference Handler
Signal Dispatcher
Monitor Ctrl
AWT-Windows
main
Attach Listener
Finalizer
Java2D Disposer

So, as you can see, creating and displaying an empty JFrame created 4 new threads even though they were not created explicitly.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I see, but which thread should I pause, to pause the GUI?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kovacs Akos wrote:Yes, I see, but which thread should I pause, to pause the GUI?

What do you mean by pause the GUI? If you don't want to allow an user to take any action just disable components that should not be used.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Kovacs Akos wrote:Yes, I see, but which thread should I pause, to pause the GUI?

What do you mean by pause the GUI? If you don't want to allow an user to take any action just disable components that should not be used.


I only would like to "freeze" it for a few seconds.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to freeze your GUI?
If you want to make it unusable for some period of time just disable whatever you want, wait some time and enable those components again.
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Why would you want to freeze your GUI?
If you want to make it unusable for some period of time just disable whatever you want, wait some time and enable those components again.


I would like to make the actual jframe visible before the automatic frame change. I would like to use it in a "tester function".
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use Thread.sleep in the event loop thread (such as in an Action Listener) this will cause your GUI to hang. Nothing else will happen until your event handler returns.

Is this what is happening?
 
Kovacs Akos
Ranch Hand
Posts: 87
1
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what should I use in this code instead of the "do your reoccuring task ":
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!