Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timers

 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have used two timers in my system but since i have implemented this two timers, i am getting random error messages thrown from any part of the systemn this didnt happen before the timer were implemented. So i suspecting that at some point both timers are interfering with eachother and therefore causing this problems.

Is there anyway in Java to control the two timers? i.e. dont sttart the other timer until one timer is finished? or any other ideas that might stop this problem.

Thank You
Maz
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error messages? Also, what kind of Timers are you using? javax.swing.Timer or java.util.Timer? TellTheDetails.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the error messages are the ones that i have defined within the system. For example whilst its trying to reload the data sometimes it throws No Data Found error.
I am using the java.util.Timer.

If i disable the timers, these messages disappear but it doesnt happen always only sometimes.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are getting a synchronization problem. Read concurrency in Swing. I think that if you switch to a javax.swing.Timer you will work around the synchronization problem.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i will try changing the timers. would it make a difference if the interval for each timer was different?
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


what have i done wrong? becuase this will not execute in Java Eclipse, it starts and stops immediately without any output.
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swing timers are daemon threads - they will not prevent the application from shutting down. If you run them inside your full application with its main window it will work.
 
Mayur Gosalia
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so there is nothing wrong with this code? I simply have to put my actual code in the action listener and it will work?

I was just trying this as a simple test and because it didnt work i was wondering if i did something wrong.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mayur Gosalia wrote:so there is nothing wrong with this code? I simply have to put my actual code in the action listener and it will work?

I was just trying this as a simple test and because it didnt work i was wondering if i did something wrong.


There's nothing wrong with it other than what was mentioned above.

To keep it alive:


edit: but a better example is to use a Swing Timer with Swing objects. For e.g.,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic