6 Replies - 271 Views - Last Post: 24 December 2013 - 10:50 PM Rate Topic: -----

#1 rocket88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-December 13

question on repeating timers, and calling doClick from one

Posted 24 December 2013 - 03:20 PM

If I set up a repeating timer, with one ActionListener, as shown below, can the ActionListener be re-entered before the prior invocation has finished?

In the below code, with a 50ms repeat interval, suppose the ActionListener takes longer than 50ms, will another instance of the ActionListener code be invoked while the prior one is still busy?

Also, if I call a doClick method on a gui button to simulate a user click, does the doClick return before the clicking code finishes or does it wait?

// this code is executed one time only from main
 
ActionListener al = new ActionListener() {
    Override
    public void actionPerformed(ActionEvent arg0) {
    	// poll stdin, if not ready do nothing
    	// otherwise read, interpret, and make calls to doClick 
    	// to simulate button clicks by user
    }
};
 
 
Timer tm = new Timer(1000,al); // Timer(TimeInMilliSeconds, ActionListener) 1000ms = 1s 
tm.setInitialDelay(1000);
tm.setDelay(50);
tm.start();



Is This A Good Question/Topic? 0
  • +

Replies To: question on repeating timers, and calling doClick from one

#2 x68zeppelin80x  Icon User is offline

  • D.I.C Addict

Reputation: 130
  • View blog
  • Posts: 576
  • Joined: 07-March 09

Re: question on repeating timers, and calling doClick from one

Posted 24 December 2013 - 03:56 PM

Take a gander at this thread that was posted here 3 1/2 years ago: Need Help With Timer Using Action Listener.

This post has been edited by x68zeppelin80x: 24 December 2013 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: question on repeating timers, and calling doClick from one

Posted 24 December 2013 - 04:03 PM

ActionListeners all run on the same thread- the EventDispatchingThread. So multiple Timers are pointless in this case. Also, why not just use a Scanner and it's hasNext() methods? Seems easier than constantly polling for input. I'd adopt an event driven model here, not a constant lookup model.
Was This Post Helpful? 0
  • +
  • -

#4 rocket88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-December 13

Re: question on repeating timers, and calling doClick from one

Posted 24 December 2013 - 04:58 PM

View Postmacosxnerd101, on 24 December 2013 - 04:03 PM, said:

ActionListeners all run on the same thread- the EventDispatchingThread. So multiple Timers are pointless in this case. Also, why not just use a Scanner and it's hasNext() methods? Seems easier than constantly polling for input. I'd adopt an event driven model here, not a constant lookup model.


Thanks for the quick reply.

So.... from your answer, it would seem that timer N+1 cannot run concurrently with timer N. So, it must then be the case that doClick is not synchronous. I have evidence that a prior doClick action might still be active when the next timer goes off. So, it would seem that a call to doClick does not wait until the clicking action is complete before it returns to the caller.

Does that make sense?

I realize that polling is frowned upon, but it's the easiest way for me. I don't know what a Scanner is, and I know very little java. I have written a program in another language and I launch myprogram (pipe) javaprogram. I write to stdout in myprogram and inside this rather large java program I just poll stdin, since it's not used for anything else, and when something comes in from the other program, I interpret that and call various doClick methods. My program has complete keyboard and mouse focus and is running a fullscreen window. The java program becomes a slave, but it knows how to do some rather complex things, such as send commands to a DVR over the network. I'm just adding a different interface.

I know this is a hack, but a useful one and it's only for my own personal use. I am just trying to get a better handle on how timers work in this case.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: question on repeating timers, and calling doClick from one

Posted 24 December 2013 - 05:45 PM

I'm out right now and will respond some more when I'm not on my phone. It sounds to me that you'd be better dealing with networking here and letting your programs interact via sockets than with piping.

Edit: Back at my computer. So everything GUI related runs on the EventDispatchingThread. That includes JButton clicks, Timer ActionEvents, etc. So nothing runs concurrently, truly.

Quote

I realize that polling is frowned upon, but it's the easiest way for me. I don't know what a Scanner is, and I know very little java. I have written a program in another language and I launch myprogram (pipe) javaprogram.

Why not just write these additional features into your first program? It seems easier than picking up Java for this task. The Java GUI is also thick and hard to deal with, if you don't have a firm grasp on the basics. Though substantial prior programming experience, especially in language encouraging OOP, might help with the learning curve.
Was This Post Helpful? 0
  • +
  • -

#6 rocket88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-December 13

Re: question on repeating timers, and calling doClick from one

Posted 24 December 2013 - 10:45 PM

View Postmacosxnerd101, on 24 December 2013 - 05:45 PM, said:

Edit: Back at my computer. So everything GUI related runs on the EventDispatchingThread. That includes JButton clicks, Timer ActionEvents, etc. So nothing runs concurrently, truly.


That's puzzling. My program was causing crashes on the DVR until I guessed there could be timing problems. So, I sandwiched the action code for a button click with a static global flag and logged the state of that flag in my ActionListener. Every so often it would indicate a button click was still active.

As a workaround, I kept this global flag and used it to dismiss my timer code - just waiting for the first timer where the flag was cleared. That stopped all the problems. But I'm a bit obsessive and I like to know what's really going on.

Quote

Why not just write these additional features into your first program? It seems easier than picking up Java for this task. The Java GUI is also thick and hard to deal with, if you don't have a firm grasp on the basics. Though substantial prior programming experience, especially in language encouraging OOP, might help with the learning curve.


The problem is that the code to drive this dvr (a tivo) has incredible complexity and is virtually impossible to write on my own. It took the tivo community months to reverse-engineer the protocols, and to date there are only a handful of programs that can do this. One example was written in perl, and when I tried to use that, I ran into encryption key issues.

Trust me, if I could have written this in my language of choice (tcl/tk) I would have done so.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10397
  • View blog
  • Posts: 38,465
  • Joined: 27-December 08

Re: question on repeating timers, and calling doClick from one

Posted 24 December 2013 - 10:50 PM

Are you familiar with the TiVo TOS:
http://www.tivo.com/legal/terms

Quote

When using our products and services, you may not do any of the following (except to the extent permitted by applicable law): (1) modify, reverse engineer, decompile, or otherwise attempt to derive the source code, structure, design, or method of operation of software that we provide to you, (2) modify, disassemble, or otherwise tamper with any TiVo hardware, (3) disguise the place of your residence or the location of your use, or (4) attempt to circumvent technological measures or gain unauthorized access through hacking, password mining or any other means.


See under their Restrictions section.

As such, per our TOS, we cannot and will not help you with this project. I am closing this thread.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1