簡體   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