You do not say what you mean by a pause. What does your application do?
Generally, you CANNOT pause the user interface application. User interface applications are launched from the message processing loop. A message arrives, a message is sent, the loop waits for another message. The application still needs to handle things like clicking on buttons, resizing the window, closing the application, etc., so that this cycle runs continuously.
If you want your application to “pause” in the sense that the user wasn’t doing something, just gray outside any button or menu that you don’t want users to do.
If your application starts a thread in the background and wants to pause this action until it resumes, you can do it quite easily, like this.
MyThread mythread = new MyThread(); // Main thread void pause() { mythread.pause = true; } void resume() { synchronized (mythread) { mythread.pause = false; mythread.notify(); } } class MyThread extends Thread { public boolean pause = false; public void run() { while (someCondition) { synchronized (this) { if (pause) { wait(); } } doSomething(); } } }
You can also use Thread.suspend (), Thread.resume () to achieve similar ones, but they are inherently dangerous because you don’t know where this thread is when you pause it. He can open the file, be halfway through sending a message through a socket, etc. Putting a test into any cycle of controlling your flow allows you to pause the action at a time when it is safe.
locka source share