4 Replies Latest reply on Apr 19, 2012 7:31 AM by Khuong Dinh Pham

    Thread.sleep in mbean give 100% cpu

    Khuong Dinh Pham Newbie

      I have a mbean to setup the database. But if the database is not available then the mbean will sleep in 5 min and then try again. When the database is up running then the mbean will continue.

       

      When I am using Thread.sleep(300000) in the current thread or do it in a separate thread the cpu will always be 100% when the thread is sleeping. The top linux command show 100% cpu and I can also see new threads is spawn about every 5 seconds (show threads on). I know I have not created these threads

       

      Could it be that jboss find out something is idle and creating new threads?

       

      I am using jboss 4.2.3

        • 1. Re: Thread.sleep in mbean give 100% cpu
          stanton fisque Newbie

          i guess the first question is, why are you spawning a thread inside of an EE container?  you really should be using a Timer.

          • 2. Re: Thread.sleep in mbean give 100% cpu
            Khuong Dinh Pham Newbie

            As I write in the first place. I am not creating any new threads. I just sleeping on the current thread that is executing the mbean. I have also tried to create only one new thread but with same result.

             

            Maybe I can use the Timer class.

             

            my flow for the mbean:

            1. check db is running.

            2. if not wait 5 min. Then repeat point 1

            3. if db running. make a select

            4. if select = false -> shutdown

            5. if select = true -> continue with the rest

             

            How can I use the Timer to do some task and make the main thread waiting? Must not do anything when the db is down

             

            Something like this:

            boolean dbRunning = false;

            Timer timer = new Timer();

            timer.schedule(new MyTask(), 300000);

            while(!dbRunning); // Wait until the db is up running

            timer.cancel();

            ...

            ...

             

            class MyTask extends TimerTask {

               public void Run() {

                if (db == running) {

                   dbRunning = true;

                }

              }

            }

            • 3. Re: Thread.sleep in mbean give 100% cpu
              stanton fisque Newbie

              yeah, the entire Thread api is a no-no in a EE contained environment.

               

              that being said, what is it you are trying to really achieve.  i think there might be a subtle misunderstanding of the EE architecture as a whole.

               

              you might be able to achieve the goal in a way that does not violate EE platform semantics.

              • 4. Re: Thread.sleep in mbean give 100% cpu
                Khuong Dinh Pham Newbie

                Problem is now solved. No problem with using Thread with ExecuteService in MBean. The problem was another jboss thread which has causing the high cpu load.