6 Replies Latest reply: May 13, 2008 4:33 PM by 807591 RSS

    concurrent API - new Thread (new Runnable()) usage

    807591
      Hi,

      I would like to use the new JDK 5 concurrent API.

      All I do in my code is the following:
      public void handleNotification(Notification arg0, Object arg1) {
                
                // we will start a new thread b/c call to doSomething() can take a very long time  
                Thread thread = new Thread(new Runnable() {
                     public void run() {
                               myClass.doSomething();
                          }     
                     }
                     
                });
                thread.start();
                
           }
      How would the above code look different using the concurrent API.

      Any help is much appreciated
        • 1. Re: concurrent API - new Thread (new Runnable()) usage
          807591
          The concurrent API has lots of features. I suggest you take the tutorial: [http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html]
          • 2. Re: concurrent API - new Thread (new Runnable()) usage
            807591
            Ok, after a quick read, I think the following could be done.
            public void handleNotification(Notification arg0, Object arg1) {
                      
                      // we will start a new thread b/c doSomething() can take a long time
                      ExecutorService pool = Executors.newCachedThreadPool();
                      pool.execute(new Runnable() {
                           public void run() {
                                doSomething();               
                           }
                           
                      });
                 }
            Because I am recreateing the pool each time, i don't see the benefit.
            B/c my class is a singleton MBean, I think the following would be more appropriate
            public class MyNotificationListenerMBean implements NotificationListener {
            
                    protected ExecutorService pool;
            
                 public MyNotificationListenerMBean() {
                      pool = Executors.newCachedThreadPool();
                 }
            
                    public void handleNotification(Notification arg0, Object arg1) {          
                      pool.execute(new MyRunnableClass());                    
                 }
            
                    public class MyRunnableClass implements Runnable {
            
                      public MyRunnableClass() {               
                      }
            
                      public void run() {
                           doSomething();               
                      }
                      
                      private void doSomething() {
                                 .....
                      }
                      
                 }
            }     
            Comments or suggestions?

            Edited by: tsc on May 13, 2008 10:53 AM
            • 3. Re: concurrent API - new Thread (new Runnable()) usage
              807591
              tsc wrote:
              Comments or suggestions?
              How about describing your problem? What is your context? What do you hope multi-threading will achieve? What problems remain? What are you looking for?
              • 4. Re: concurrent API - new Thread (new Runnable()) usage
                807591
                When the user updates a property on an MBean, doSomething() via handleNoticication() will be called on all listener classes. Because this method can take too long, its ideal to do the work a new thread. The new thread does not need to be monitored.

                I was going to do it the old way (my first post) but then thought of looking into the 1.5 concurrency API and hence the question on the forum.
                • 5. Re: concurrent API - new Thread (new Runnable()) usage
                  807591
                  That sounds straightforward (assume you've got the thread-safety in hand). You're right, you need to make your thread pool long lived. Creating it in a method is too short. I usually create my thread pools are part of larger "app" objects, so that they can be long-lived and widely available. If the pool is part of a MBean, will you create many beans? What is the lifetime of a bean? I don't think you want the pools to proliferate.
                  • 6. Re: concurrent API - new Thread (new Runnable()) usage
                    807591
                    My MBean is a singleton which is created once when the app. starts up.

                    Thanks for your help.