Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

How to dectect the existence applet instance from the same class?

 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an applet that runs in a thread. Somehow, when i refresh my browser, another instance of applet is initialized before the old one is killed and destroyed. I am thinking to put a simple checking mechanism in applet init method which will look for the applet instance from the same class. If the old instance still exists, wait until the old one is killed before the new instance is initialized. Can i do that?
Thanks,
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You possibly could, but you might end up waiting a long time as you have no guarantees about when garbage collection will occur. One thing that might help is to set a boolean value to tell the old applet thread to end under certain conditions - such as when a new applet thread is created.
You might also want to track whether another instance was created using a static boolean or int variable. If the boolean is true or the int is greater than zerio, then try to stop any other of these threads by setting their stop switch (the aforementioned boolean value).
You might also need to create a static data structure that stores references to already created applet instances.
Don't forget to synchronize everything properly.
Good Luck.
 
SoonAnn Lim
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thank you very much for the reply. I have solved this problem. The method i used is very close to your suggestion. So i think i am doing the right thing. I create a static counter in the applet which is incremented in the init() and decremented in stop(). It works very fine. However, i need to synchronize the applet as you mentioned. I forget that part at all.
Thank you very much..
Regards,
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic