List of Topics:
Research Breakthrough Possible @S-Logix pro@slogix.in

Office Address

Social List

How to Perform an Action Periodically Using Thread in Java?

Perform Periodic Action Using Thread in Java

Condition for Performing an Action Periodically Using Thread in Java

  • Description: To perform an action periodically in Java, a thread can be used with the `Thread.sleep()` method inside a loop to introduce delays between actions. This method pauses the thread for a specified time before executing the action again. Alternatively, `ScheduledExecutorService` offers a more efficient and flexible approach for scheduling periodic tasks. Proper exception handling and thread management are essential for ensuring smooth execution.
Sample Source Code
  • # PeriodicAction1.java
    package JavaSamples;

    import javax.swing.JOptionPane;

    public class PeriodicAction1 extends javax.swing.JFrame {

    public PeriodicAction1() {
    initComponents();
    }

    @SuppressWarnings("unchecked")
    // //GEN-BEGIN:initComponents
    private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    jTextArea1 = new javax.swing.JTextArea();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("Periodic Action With Thread");

    jTextArea1.setColumns(20);
    jTextArea1.setRows(5);
    jScrollPane1.setViewportView(jTextArea1);

    jButton1.setText("Periodic Action");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
    jButton1ActionPerformed(evt);
    }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jButton1)
    .addGap(137, 137, 137))
    .addGroup(layout.createSequentialGroup()
    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(112, 112, 112)
    .addComponent(jLabel1))
    .addGroup(layout.createSequentialGroup()
    .addGap(69, 69, 69)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap(69, Short.MAX_VALUE))
Screenshots
  • STEP 1: The user creates the GUI to perform the thread operation.
  • The user creates the GUI to perform the thread operation.

  • STEP 2: After clicking the 'Periodic Action' button, a new thread is started to perform an action (appending a message to the JTextArea) every 2 seconds for a maximum duration of 10 seconds.
  • After clicking the 'Periodic Action' button, a new thread is started to perform an action every 2 seconds.

  • STEP 3: Once the time limit is reached, a dialog box displays the total number of actions performed.
  • Once the time limit is reached, a dialog box displays the total number of actions performed.

Related Links