Results 1 to 4 of 4
  1. #1
    a_programmer is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

    Default Change a variable outside TimerTask after it is done?

    Java Code:
    boolean done = false;
    Timer t = new Timer();
    t.schedule(new TimerTask() {
           @Override public void run() {
                //task
            }
        },0, 1000);
    done = true;
    The boolean done will be changed to true instantly, while the timer task is still executing. I can't change it from within the timer task because it would have to be declared final. How can I change the done to true from inside, or after the timer reaches a certain point?
    Last edited by a_programmer; 07-17-2011 at 08:05 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Make that boolean variable a member variable instead of a local variable. Or, (this is a bit of a hack) do this:

    Java Code:
    final boolean done= new boolean[1];
    Timer t= new Timer();
    t.schedule(new TimerTask() {
       public void run() {
          // stuff here ...
          done[0]= true;
       }
       }, 0, 1000);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Jhovarie is offline Banned
    Join Date
    Feb 2011
    Posts
    65
    Rep Power
    0

    Default

    try this one

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     *
     * @author Jhovarie A. Guiang
     */
    public class NewJFrame extends javax.swing.JFrame {
        
    static int count = 0;
    static int variable = 3;
    javax.swing.Timer timer;
        /** Creates new form NewJFrame */
        public NewJFrame() {
            initComponents();
             ActionListener act = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    count++;
                    if(count == 10){
                        timer.stop();
                     variable = 12345;
                     System.out.println();
                     System.out.print("Timer is Done variable is updated");
                    }
                     System.out.println("Steep:"+count+") The value of variable is "+variable);
                }
            };
              timer = new javax.swing.Timer(1000,act);
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jButton1.setText("Start");
            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(layout.createSequentialGroup()
                    .addGap(107, 107, 107)
                    .addComponent(jButton1)
                    .addContainerGap(137, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(57, 57, 57)
                    .addComponent(jButton1)
                    .addContainerGap(71, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    timer.start();
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify
        private javax.swing.JButton jButton1;
        // End of variables declaration
    }

  4. #4
    a_programmer is offline Member
    Join Date
    Jun 2011
    Posts
    19
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 04-17-2011, 03:59 PM
  2. thread + timertask
    By anra in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-08-2011, 10:05 AM
  3. Why wont my boolean variable change?
    By equal in forum New To Java
    Replies: 8
    Last Post: 02-22-2011, 10:43 PM
  4. can i listen to variable change in diffrent class
    By gloomy1991 in forum New To Java
    Replies: 2
    Last Post: 01-08-2011, 01:50 AM
  5. TimerTask with a Date_Time
    By kunta in forum Sun Java Wireless Toolkit
    Replies: 1
    Last Post: 05-31-2007, 10:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •