繁体   English   中英

如何使用Thread.sleep在for循环中添加延迟?

[英]How to add delay in the for loop using Thread.sleep?

我需要在for循环中的每个ith迭代之后放置一个延迟。 问题是我可能无法使用Timer ,因为我需要使用参数ck ,这取决于先前的迭代。

我尝试使用Thread.sleep(DELAY) ,但这只是延迟了整个for循环的执行。

    double c = 3;
    int k = 1;  

    for (int j = 0; j<sumJ; j++)
    {
        for (int i=0; i<sumI; i++)
        {
            //...calculations based on values of c and k 
            // received in previous iterations
            try {
                Thread.sleep(100);
            } catch(InterruptedException ie) {}
        }
    }

好的,这是一个使用计时器的非常简单的示例。 请注意,这是我们正在使用的javax.swing.Timer ,这一点很重要,因为它确保工作在事件线程中完成,在事件线程中应该对组件进行所有更改。

基本上,我们创建表并显示它,然后仍然在主线程中准备数据。 最后,我们启动计时器,它每半秒触发一次操作事件,该事件将填充我们准备的数据中的下一个值。

请注意,表格填满后,您可以使用GUI进行操作,调整大小,选择单元格等。

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.Timer;

class SimpleTest implements ActionListener{

    public static final int ROWS = 10;
    public static final int COLS = 10;
    public static final int DELAY = 500;

    private Integer[][] data = null;
    private int currCol;
    private int currRow;
    private JTable tableToFill = null;
    private Timer theTimer = null;

    /**
     * Calculates the data that should be displayed in the table.
     * In this case it's a simple multiplication table.
     */
    public void fillData() {
        data = new Integer[ROWS][COLS];
        for ( int row = 0; row < ROWS; row++ ) {
            for ( int col = 0; col < COLS; col++) {
                data[row][col] = (row + 1) * (col + 1);
            }
        }
    }

    /**
     * Creates a GUI table to fill up.
     * @return The table object created.
     */
    public JTable setUpJTable() {
        tableToFill = new JTable(ROWS,COLS);
        return tableToFill;
    }

    /**
     * Starts the timer that fills the values in the GUI table
     */
    public void startTimer() {
        if ( tableToFill != null && data != null ) {
            currCol = 0;
            currRow = 0;
            theTimer = new Timer( DELAY, this);
            theTimer.start();
        }
    }

    /**
     * Called by the timer to set the value in the table.
     * 
     * @param evt Action event (not used)
     */
    public void actionPerformed(ActionEvent evt) {
        synchronized (this) {
            if ( currRow < ROWS ) {
                tableToFill.setValueAt(data[currRow][currCol], currRow, currCol);
                currCol++;
                if ( currCol == COLS ) {
                    currCol = 0;
                    currRow++;
                }

            } else {
                theTimer.stop();
            }
        }
    }

    public static void main(String[] args) {

        SimpleTest tableFiller = new SimpleTest();
        JTable table = tableFiller.setUpJTable();

        // Display the table

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(table.getTableHeader(), BorderLayout.PAGE_START);
        frame.getContentPane().add(table, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);

        // Fill the data, then start displaying it gradually

        tableFiller.fillData();
        tableFiller.startTimer();
    }


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM