Example usage for java.lang Thread sleep

List of usage examples for java.lang Thread sleep

Introduction

In this page you can find the example usage for java.lang Thread sleep.

Prototype

public static native void sleep(long millis) throws InterruptedException;

Source Link

Document

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.

Usage

From source file:Main.java

public static void main(String[] arguments) {
    JPanel panel = new JPanel(new BorderLayout());
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);/*from   w w w .  j a va  2  s.c  o  m*/
    frame.setBounds(20, 20, 200, 200);
    frame.setVisible(true);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setIndeterminate(true);
    progressBar.setVisible(false);
    JButton loadButton = new JButton("Load memberlist");
    loadButton.setEnabled(true);
    loadButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    progressBar.setVisible(true);
                    // do my stuff here...
                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    progressBar.setVisible(false);
                }
            }).start();
        }
    });
    JPanel container = new JPanel(new FlowLayout());
    container.add(loadButton);
    container.add(progressBar);
    panel.add(container);
}

From source file:Main.java

public static void main(String[] args) throws UnsupportedEncodingException {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel panel = new JPanel(new BorderLayout());
    JPanel buttons = new JPanel();
    JScrollPane pane = new JScrollPane(buttons);
    pane.getViewport().addChangeListener(e -> {
        System.out.println("Change in " + e.getSource());
        System.out.println("Vertical visible? " + pane.getVerticalScrollBar().isVisible());
        System.out.println("Horizontal visible? " + pane.getHorizontalScrollBar().isVisible());
    });//from   w  ww  .j  av a  2s .c o  m
    panel.add(pane);
    frame.setContentPane(panel);
    frame.setSize(300, 200);
    frame.setVisible(true);
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override
        protected Void doInBackground() throws Exception {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(800);
                buttons.add(new JButton("Hello " + i));
                buttons.revalidate();
            }
            return null;
        }
    };
    worker.execute();
}

From source file:ProgressBarUpdateAnotherThread.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    bar.setBounds(10, 10, 200, 32);//  w  w w  . j  a v  a2s . c om
    shell.open();
    final int maximum = bar.getMaximum();
    new Thread() {
        public void run() {
            for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
                try {
                    Thread.sleep(100);
                } catch (Throwable th) {
                }
                if (display.isDisposed())
                    return;
                display.asyncExec(new Runnable() {
                    public void run() {
                        if (bar.isDisposed())
                            return;
                        bar.setSelection(i[0]);
                    }
                });
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:siia.monitoring.jmx.JmxDemo.java

public static void main(String[] args) throws InterruptedException {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("context.xml", JmxDemo.class);
    MessageChannel channel = context.getBean("channel", MessageChannel.class);
    for (int i = 0; i < 1000; i++) {
        channel.send(MessageBuilder.withPayload(i + "").build());
        Thread.sleep(3000);
    }/*from   w  ww .  java 2s  .co m*/
}

From source file:org.mshariq.cxf.brave.Server.java

public static void main(String args[]) throws Exception {
    ApplicationContext appctxt = new ClassPathXmlApplicationContext(
            Server.class.getResource("/context.xml").toString());

    System.out.println("Server ready...");

    Thread.sleep(5 * 6000 * 1000);
    System.out.println("Server exiting");
    System.exit(0);//from   w w w.j  ava2  s . c  om
}

From source file:com.apress.prospringintegration.monitoring.MonitoringExample.java

public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "classpath:monitoring/monitoring.xml");

    ProcessMessage processMessage = context.getBean("processMessage", ProcessMessage.class);
    for (int i = 0; i < 10; i++) {
        processMessage.processMessage("Process");
        processMessage.checkMessage("Check");
    }/*w ww. j  av  a2  s.c o m*/

    while (true) {
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            //do nothing
        }
        context.stop();
    }
}

From source file:batch.processing.usage.chunk.hibernate.BatchChunkHibernateProcessingUsage.java

/**
 * @param args the command line arguments
 *///from w w  w. ja  v a 2  s  .  c  om
public static void main(String[] args) {
    try {
        Properties jobParameters = new Properties();
        jobParameters.put("nomeArquivo", "OpLm.csv");
        BatchProcess launcher = BatchApplication.createExecutionProcess("chunkHibernateExample", jobParameters);
        BatchExecution execution = launcher.start();
        while (execution.getStatus() != Status.COMPLETED) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {

            }
        }
        System.out.println(execution.toString());
    } catch (Exception ex) {
        LOG.error(ex);
    }
}

From source file:Snippet142.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Button button = new Button(shell, SWT.NONE);
    button.setSize(100, 100);//from   w w  w. j  a va2 s.  com
    button.setText("Click");
    shell.pack();
    shell.open();
    button.addListener(SWT.MouseDown, new Listener() {
        public void handleEvent(Event e) {
            System.out.println("Mouse Down (button: " + e.button + " x: " + e.x + " y: " + e.y + ")");
        }
    });
    final Point pt = display.map(shell, null, 50, 50);
    new Thread() {
        Event event;

        public void run() {
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event = new Event();
            event.type = SWT.MouseMove;
            event.x = pt.x;
            event.y = pt.y;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseDown;
            event.button = 1;
            display.post(event);
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
            }
            event.type = SWT.MouseUp;
            display.post(event);
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:com.mycompany.webcrawler.GmailAutomation.java

public static void main(String[] args) {
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    DesiredCapabilities dcaps = new DesiredCapabilities();

    dcaps.setCapability("takeScreenshot", true);
    //WebDriver driver = new PhantomJSDriver(dcaps);

    //Comment for PhantomJS
    ChromeDriver driver = new ChromeDriver();

    //Comment for PhantomJS
    //driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);

    driver.get(/*w  w  w. j  av  a2 s.c  o  m*/
            "https://accounts.google.com/ServiceLogin?sacu=1&scc=1&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&osid=1&service=mail&ss=1&ltmpl=default&rm=false#identifier");

    try {
        Thread.sleep(2000);

        driver.findElement(By.id("Email")).sendKeys("Your gmail goes here");

        Thread.sleep(2000);
        driver.findElement(By.id("next")).click();

        Thread.sleep(2000);
        driver.findElement(By.id("Passwd")).sendKeys("Your password goes here");

        Thread.sleep(2000);
        driver.findElement(By.id("signIn")).click();

        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[@class='T-I J-J5-Ji T-I-KE L3']")).click();

        Thread.sleep(2000);
        driver.findElement(By.className("vO")).sendKeys("Who you want to send the email to");

        Thread.sleep(2000);
        driver.findElement(By.className("aoT")).sendKeys("The subject");

        Thread.sleep(2000);
        driver.switchTo().activeElement().sendKeys(Keys.TAB);
        driver.switchTo().activeElement().sendKeys("The body");

        //Comment for PhantomJS
        //driver.findElement(By.xpath("//div[@class='Am Al editable LW-avf']")).sendKeys("Test Email");

        Thread.sleep(2000);
        File file = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file, new File("screenshot.jpg"));

        Thread.sleep(2000);
        driver.findElement(By.xpath("//div[@class='T-I J-J5-Ji aoO T-I-atl L3']")).click();

        //Comment for PhantomJS
        //Thread.sleep(2000);
        //driver.get("https://mail.google.com/mail/u/0/#sent");
    } catch (IOException | InterruptedException | WebDriverException e) {
        System.out.println(e.getMessage());
    }
}

From source file:io.analytica.server.plugins.processstats.socketio.TestSocketIo.java

public static void main(final String[] args) throws Exception {
    final SocketIO socket = new SocketIO("http://npiedeloup1:8090");
    // This line is cached until the connection is establisched.
    socket.connect(new EmptyIOCallback());
    Thread.sleep(2000);
    for (int i = 0; i < 50000; i++) {
        Thread.sleep(150);//from   w  w  w  .  j  av a  2  s  .c  o m
        socket.emit("ping", new JSONArray("[" + i * 10 % 360 + ", " + i % 250 + ", 3]"));
    }
    socket.disconnect();
}