Tuesday, January 19, 2016

Pause execution in Java - sleep() and wait()

You may use the following two methods to pause the execution of the current thread when needs to wait for a required condition.

1. Thread.sleep(<milliseconds>)


       Public class PauseDemo {
             private String[] collectInput() {
                   InputFrame input = new InputFrame();
                   input.run();
                 
                   while (!input.submitted && input.isVisible()) {
                         try {
                                //Make the current thread sleep for one second
                                Thread.sleep(1000);
                         } catch (InterruptedException e) {
                                e.printStackTrace();
                        }
                   }

                   String[] result = new String[3];
                   result[0] = input.getName();
                   result[1] = input.getAddress();
                   result[2] = input.getPhone();

                   return result;
             }

            private class InputFrame extends JFrame implements Runnable {
                  boolean submitted = false;

                  JTextField nameField = null;
                  JTextField addressField = null;
                  JTextField phoneField = null;

                  public InputFrame() {
                         JLabel nameLabel = new JLabel("Name: ");
                         JLabel addressLabel = new JLabel("Address: ");
                         JLabel phoneLabel = new JLabel("Phone: ");

                         nameField = new JTextField(9);
                         addressField = new TextField(30);
                         phoneField = new JTextField(10);

                         JButton submit = new JButton("Submit");
                         submit.addMouseListener(new MouseAdapter() {
                               public void mouseClicked(MouseEvent e) {
                                      submitted = true;
                                      InputFrame.this.setVisible(false);
                               }
                         });

                         setLayout(new GridLayout(3, 2);
                         add(nameLabel);
                         add(nameField);
                         add(addressLabel);
                         add(addressField);
                         add(phoneLabel);
                         add(phoneField);
                  }

                  public String getName() {return nameField.getText(); }
                  public String getAddress() {return addressField.getText(); }
                  public String getPhone() {return phoneField.getText(); }

                  public void run() {
                         pack();
                         setSize(800, 600);
                         setLocation(400, 300);
                         setVisible(true);
                  }
           }

       }

2. class wait(<milliseconds>)


In the above case, replacing Thread.sleep(1000) with PauseDemo.class.wait(1000) will also work.

3. TimeUnit

//sleep one second
TimeUnit.SECONDS.sleep(1);

//sleep two minute
TimeUnit.MINUTES.sleep(2);

The good thing about this approach is that it is very easy to know the time it is going to sleep without having to convert from milliseconds.

---------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.




No comments:

Post a Comment