Threads: I only want one instance.

I am starting a daemon thread from a servlet that gets loaded on startup (Tomcat 5.5). The servlet's init() method creates a thread that optimizes my Lucene indexes every 2 hours.
This is what's in the servlet's init method:
Thread optimizer = new Thread(new LuceneIndexOptimizer(conf), "Lucene Index Optimizer");
optimizer.setDaemon(true);
optimizer.setPriority(Thread.MIN_PRIORITY);
optimizer.start();     The thread looks like this:
public class LuceneIndexOptimizer implements Runnable {
     private Config conf = null;
     public LuceneIndexOptimizer(Config conf) {
          this.conf = conf;
     public void run(){
          while (true) {
               LuceneFuncs.optimize(this.conf);
               try{
                    Thread.sleep(7200000); // 2 Hours.
               catch(InterruptedException ex){
                   ex.printStackTrace();
}Now the problem is that whenever the context is reloaded, that servlet is re-initialized and a new thread gets started without the old one ever going away.
How do I make sure that only one of these threads is running at a given point in time?

Again, good information. Thanks.
At any rate, I ended up going with the ServletContextListener interface as I think it is cleaner.
How it works:
In my web.xml I have this entry:
<!--  Context Listener for Application Initialization -->
<listener>
     <listener-class>
          com.r10.rp.context.RPInit
     </listener-class>
</listener>The RPInit class:
public class RPInit implements ServletContextListener {
     public static final long serialVersionUID = 10;
     private LuceneIndexOptimizer indxOpt = null;
     public void contextInitialized(ServletContextEvent event) {
          // Set application specific config variables.
          System.out.println("com.r10.rp.config.RPConfig INFO: Setting Application Config Variables");          
          ServletContext ctx = event.getServletContext();
          RPConfig.createConfig(ctx);
          RPConfig conf = (RPConfig) ctx.getAttribute("RPConfig");          
          // Start Lucene Optimizer
          System.out.println("com.r10.rp.config.RPConfig INFO: Initializing Lucene Index Optimizer");
          this.indxOpt = new LuceneIndexOptimizer(conf);
          Thread optimizer = new Thread(this.indxOpt, "Lucene Index Optimizer");
          this.indxOpt.setOptimizer(optimizer);
          optimizer.setDaemon(true);
          optimizer.setPriority(Thread.MIN_PRIORITY);
          optimizer.start();                    
     public void contextDestroyed(ServletContextEvent event){
          this.indxOpt.stop();
}The IndexOptimizer thread:
public class LuceneIndexOptimizer implements Runnable {
     private RPConfig conf = null;
     private volatile Thread optimizer;
     public LuceneIndexOptimizer(RPConfig conf) {
          this.conf = conf;
     public void run() {
          Thread thisThread = Thread.currentThread();
          while (thisThread == this.optimizer) {
               LuceneFuncs.optimize(this.conf);
               try{
                    Thread.sleep(7200000); // 2 Hours.
               catch(InterruptedException ex){
                   ex.printStackTrace();
     public void stop() {
          Thread blah = this.optimizer;
        this.optimizer = null;
        blah.interrupt();
      * @param optimizer The optimizer to set.
     public void setOptimizer(Thread optimizer) {
          this.optimizer = optimizer;
}

Similar Messages

  • I only want one app for now

    I currrently have Adobe Creative Cloud and only want one app for now which is Photoshop .... I accidentally installed Lightroom, Bridge CC, Exchange Panel, Extendscript toolkit, Extension manager, touch app plugins from Creative Cloud and would like to uninstall these apps until I have a full understanding of the Creative Cloud. 
    I pay monthly for membership and do not want to uninstall the Creative Cloud.
    Please advise on how to uninstall these other apps.
    Thanks.

    Hi Margaret,
    Please use the cleaner tool to remove the app that you would like to uninstall:
    http://helpx.adobe.com/creative-suite/kb/cs5-cleaner-tool-installation-problems.html
    Choose the best option as per your decision.
    Regards,
    Rajshree

  • I created a watermark in LR4 and it applied to all photos I only wanted one photo marked.

    I created a watermark in LR4 and it applied to all photos I only wanted one photo marked. How can I keep it from adding the watermark to all of my photos?
    I want to be able to add them manually to each photo in a location that I chose so the photo is still visably appealing.
    Thanks!

    I think it's a feature of the Web Module, since it is assumed that you would want the watermark on all images of a web-gallery.
    But, in this context (as always is the case in Lr) you have to take into consideration of how Lr works. Lr never ever changes your originals.
    "Watermark applied" means only that there is a set of data in the Lr catalog that says something like "when uploading the web-gallery the watermark has to be applied to the uploaded JPGs".
    Nothing has been done to your originals - even though they are displayed in te Web Module with the watermark. The orginals are ever unchanged.
    If you go back to the Library Module (within the same Collection) you won't see a watermark.

  • Only want one image version in MobileMe album

    When I add a version to a MobileMe album, all of the versions are added to the album. When I delete the other versions from the MobileMe album, those versions are deleted from my library.
    I only want one version added to the MobileMe album... and the other versions to remain in my album.
    Any ideas?
    Thanks!

    Within Aperture you have Images, which are constructed on-the-fly from two files (the Master and the Version).  You get an image-format file _only_ when you export an Image.  You select the file name (usually a scheme) when you create an image-format file (that is, when you export).  Look under "Aperture→Presets→File Naming" for built-in Presets.  You can, of course, create your own or customize any provided.
    The Preset is applied to only the file newly created by your "export" command.  It is not applied to the Image in Aperture (unless you rename your Versions or your Masters).

  • TS3276 Does anyone have problems with sent messages not showing in their Mail? I have two sent folders when really I only want one. Any tips?

    Does anyone have problems with sent messages not showing in their Mail? I have two sent folders when really I only want one. Any tips?

    Not sure if this is a fix, but I tried sending myself a test email from only  the Bcc field, and lo and behold it now shows the Bcc field in all sent item previews;
    ...maybe leaving the 'To' field blank on purpose forced Mail to show it.
    Rebooted the Mail program, still there - rebooted the machine, still there. Hope this is still relevant and it works for you too - J.

  • I accidentally pressed download to the whole album but I only wanted one song??? Help please ??

    I was on iTunes on my iPhone and I accidentally pressed the download for the whole album! But I only want ONE song!! Of course it's one of those DJ songs where all 10 songs the same. ( who wants all of those) . Now my whole iTunes card is gone too! Please is there a way to fix this!! Ill never buy music on my phone again!

    Actually most of the album is paused in my download box if that helps??

  • How do you use only one plugin in Logic for multiple guitar tracks? I want to only use one instance of Pod Farm and run all my guitar tracks through that one instance.

    I want to be able to use only one instance of Pod Farm for my guitars, and have all of my guitar tracks run through it while I'm recording.

    What he said. Or here is a similar approach with a picture.
    1. Press the SEND button on every stereo (or mono) guitar track you want to send through the effect. Then select a free bus.
    2. Make sure you turn up the amount of send (in db). Holding alt while leftclicking on this circle sets the send level to 0 db wich is good.
    3. Open the mixer (Cmd+2). Please see that I here have turned the output off from the stereo guitar track. That way the only output will be the BUS, in your case the AmpTrack bus where all your guitars will pass through. If you want both the original guitar sound AND the Ampfarm effect then just set all outputs on your guitar tracks to Stereo Out, like the AmpF Bus track is set in my picture.
    4. Insert your AmpFarm plugin here. I don´t have that plug so I inserted Waves Renes Axe for demo purpose.
    Adjust the volume/effect of every guitartrack by turning the circle input shown as 2. in my picture.
    I now see that you mentioned Pod Farm, not AmpFarm. But I guess it´s the same trick.
    Have fun.
    Heyclown.
    Message was edited by: WizardSongs - Typhoos

  • Only letting one instance of my application run

    hi guys,
    My application will allow the user to interact with the database. The database will be stored on a local machine, and not on a network, so that only one person can access it at a time.
    The problem is, I believe a user can launch two instances of my application simultaneously, and therefore there are potential problems.
    So I want the user to only ever be able to launch one instance of the JFrame through which all interactions to the DB are made.
    So MyApp.java contains
    class MyApp
    public void main (String[] args)
    MyFrame mF = new MyFrame();
    }Now, is it the right idea of mine to make mF singleton? Or is it the wrong approach?
    But what is confusing me is...if at the java prompt I type
    java MyApp
    and then I type, once again
    java MyApp
    will these two be treated as two completely different running of programs? Say I had mF as a singleton. Does this mean there can exist one instance of mF in each of my two different runnings of MyApp, or are the two somehow connected, which means that if I ran MyApp a second time theer'll be some sort of error cos mF already exists and cos it is a singleton, it can't exist twice?
    Sorry if I havent made myself clear. I'm very confused myself.
    Thanks.
    AU

    My application will allow the user to interact with
    the database. The database will be stored on a local
    machine, and not on a network, so that only one
    person can access it at a time.If using jdbc then local or remote only really effect your database driver choice not your code.
    The problem is, I believe a user can launch two
    instances of my application simultaneously,Yes.
    and therefore there are potential problems. Potential but not that difficult to avoid.
    So I want the user to only ever be able to launch one
    instance of the JFrame through which all interactions
    to the DB are made.If you think about responsibilities, then data integrity is not the responsibility of the UI, its the responsibility of the DBMS.
    Now, is it the right idea of mine to make mF
    singleton? Or is it the wrong approach?A Singleton will only ensure that a jre has a single instance. If you start multiple instances of the jre then there can be one instance of the application in each.
    The best solution is to use your databases normal locking process through the JDBC api
    The simplest solution is for your program to lock either the database, the table or the record you are interested in. You can do this by creating a lock record on a seperate table. If the insert fails to create the record, somebody else is already using the dataset. If it succeeds then is available. You will need a way to forcible clear the lock if your application stops unexpectedly.

  • Regular tv shows and HD both load and I only want one!

    Seems like sometimes when I download a tv show I will get the regular version and the HD version. I only want the regular version since it's cheaper so I delete the HD version but they are still in my Available Downloads list and every time I try to delete them, they come back! And every time I buy something new...they show up and start downloading again. How can I delete these HD duplicates of shows I already have once and for all??

    Im not sure how to delete them, but there is a simple solution to not getting them at all:
    When you download an HD version of something (TV show, Movie) you get BOTH the HD version, and then you get the STANDARD version free. Sort of like a bonus to put it on your iPod (if you have one) since they won't play HD versions.
    Now, if they are still appearing in your queue to download, I would just let them download then delete the files. Either way, you paid for them.
    On that note, though, you can send the iTunes folk an email saying that you downloaded the HD by mistake and they may credit your account back. That can be done by going into your account and using the "Report Problem" service.
    blakewt

  • Stacks - Only want one photo in stack to be sent to Mobile Me

    I have tried selecting one photo out of a stack as the "picked" image, but the entire stack is sent to Mobile Me when I send the photos out. I only want the "winning" image in the stack to be sent. I am sending the images from an album, where one of the images in the stack receives the "Set Album Pick" designation.

    I don't use mobile me anymore - I kinda remember that you had to close the stacks with Aperture 2 to make only the picks upload or something like that. Maybe I'll give it a whirl a little later to re-figure it out.
    RB

  • I am trying to restore my old Macbook Pro data using Time Machine on a new Mackbook Air from external hard drive, but when I install I get two different loggin accounts and I only want one loggin account

    I am trying to restore my old Macbook Pro data using Time Machine to a new Macbook Air from an external hard drive, but when I restore I get two diferrent loggin accounts and I only wnat one loggin account to access all my data/files?

    This web site might answer all of your Time machine questions:
    http://pondini.org/OSX/Home.html
    Ciao.

  • I recently purchased an iMac and used migration assistant to get all files from my emac, but now I have three user accounts.  I only want one (perhaps two) accounts.  I want all my music, photos and word docs from old computer in one user name

    I recently purchased a mac and used a firewire to transfer my files onto my new imac with migration assistant.  Now I have 3 users.  I want one with all my main files (pictures, music, files from my old computer).
    I don't mind having an additional "family" account so my younger daughter can't mess us things, but I can't figure out how to get files from one to the next and then delete the one user name.
    Thanks,

    In order to delete other accounts you must be logged into an admin account. You cannot delete the account into which you are currently logged.
    When you started the new computer you were in the Setup Assistant. You created a new admin account before finishing the setup. You need to log into that account because it is an admin account. You can then delete the account you migrated from the old computer (and any other accounts you don't need or want.)
    Then create a temporary new user account with admin status - name it, Temp. Log out of the current account and log into the new account, Temp. Now delete the initial account you created in Setup Assistant. You now have one account, Temp. Now re-migrate your data from the old computer. When that is completed log into the migrated account, which should be an admin account, and delete the temporary account, Temp.

  • I have two iCloud accounts .mac and .me. My contacts are duplicated 2 and 3 times. I only want one account.

    Somehow, I ended up with two iCloud accounts. A .mac and a .me. I only need one but can not make the .me the primeray one (it tells me that the email is already in use). Anyways, I also have my contact records duplicated 2 and 3 times. I went to the Apple store in Beijing (I'm in China now) and they did not seem to know much about iCloud. They told me that I have to erase my contacts on the iPhone and iPad and then manually erase the duplicates. Is there another way to do it? How can I set up iCloud so that I only have one account?

    You can't delete an iCloud account.  You can, however, sign out of that account and just stop using it.  Then sign in to your second account on all devices account. 
    OT

  • "ORA-00928: missing SELECT keyword" only on one instance in RAC

    I am using oracle 9i RAC.When I run a query in one instance it is working fine but in another instance of RAC it is giving "ORA-00928: missing SELECT keyword".
    I am wondering why it happening in the same RAC.Please suggest me where is the problem.
    Thanks

    STAR_TRANSFORMATION_ENABLED=true in both instances
    My query lools like
    SELECT * FROM
         (SELECT coulmn1,col2
                   FROM tab1 wh full OUTER JOIN tab2 stg on wh.row_id = stg.row_id
                   WHERE wh.MODIFICATION_NUM IS NULL
                   OR stg.MODIFICATION_NUM IS NULL
                        OR wh.MODIFICATION_NUM <> stg.MODIFICATION_NUM) WHERE REC_STA <>'D';
    Please suggest
    Thanks

  • 2 web sites i only want one uploading to mobileme

    i run a personnel site which iweb uploads to my mobileme account with no problems. I also started a site for my daughters shop for which i used rapid-weaver, however this program is to long winded for me. The problem is in iweb i have created 2 sites no problem i can save the second to folder, but it always saves the first site as well i can delete this no problem but when i publish my site it always uploads the second. The icon on both sites is the mobileme icon how can i stop the second from uploading

    Maybe this will help. I use iWebSites to manage the multiple sites.. It lets me create multiple sites and multiple domain files.
    If you have multiple sites in one domain file here's the workflow I used to split them into individual site files with iWebSites. Be sure to make a backup copy of your original Domain.sites files before starting the splitting process and move it to another location on your HD until you're satisfied the separation process is completed and what you want.
    This lets me edit several sites and only republish the one I want.

Maybe you are looking for

  • Unable to update apps in AppWorld

    I recently received notifications of newer versions of apps I have installed.  After selecting the app for update, there is a download bar, but nothing ever downloads.  I have retried this over the course of several days and it never starts the downl

  • Snapshot report showing wrong instance on grid control!!!

    Hi On one of the cluster environments, Even though I run snapshot report on instance 2, report shows instance 1. This i happening on one set of cluster environment. Other cluster instances are working fine. Let me know if you have seen such behavior

  • Navigation using a list

    See http://htmldb.oracle.com/pls/otn/f?p=24317:130 I would like to have each region's link in the list re-display the same page with just that region. I would pass in a hidden parameter in the link's URL and conditionally display each region dependin

  • Change image according to selected attribute

    Hi , I know there is lot of forum post around. I still haven't managed to get that sorted. I have created a product - I have set it up and it show nice big pciture. In attributes, I have created colors : 4 images of the same product ( diffrent color

  • Using taglibs to generate html select tag

    Hello, I'm using taglibs to generate html select statements like this: <input:select ... options="<%= optionsMap %> </input:select> However I would like each option element to be assigned a "onclick" attribute. Is that possible? Thanks!