Results 1 to 4 of 4
- 07-17-2011, 07:48 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Change a variable outside TimerTask after it is done?
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?Java Code:boolean done = false; Timer t = new Timer(); t.schedule(new TimerTask() { @Override public void run() { //task } },0, 1000); done = true;Last edited by a_programmer; 07-17-2011 at 08:05 PM.
- 07-17-2011, 08:21 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,427
- Blog Entries
- 7
- Rep Power
- 17
Make that boolean variable a member variable instead of a local variable. Or, (this is a bit of a hack) do this:
kind regards,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);
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-19-2011, 06:15 PM #3
Banned
- Join Date
- Feb 2011
- Posts
- 65
- Rep Power
- 0
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 }
- 07-20-2011, 03:04 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Why does change one variable also change another?
By yma16 in forum New To JavaReplies: 1Last Post: 04-17-2011, 03:59 PM -
thread + timertask
By anra in forum Threads and SynchronizationReplies: 0Last Post: 03-08-2011, 10:05 AM -
Why wont my boolean variable change?
By equal in forum New To JavaReplies: 8Last Post: 02-22-2011, 10:43 PM -
can i listen to variable change in diffrent class
By gloomy1991 in forum New To JavaReplies: 2Last Post: 01-08-2011, 01:50 AM -
TimerTask with a Date_Time
By kunta in forum Sun Java Wireless ToolkitReplies: 1Last Post: 05-31-2007, 10:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks