[jboss-cvs] JBossAS SVN: r58167 - in trunk/j2se/src/main: javax/management javax/management/timer org/jboss/mx/util

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Nov 6 17:37:49 EST 2006


Author: genman
Date: 2006-11-06 17:37:18 -0500 (Mon, 06 Nov 2006)
New Revision: 58167

Removed:
   trunk/j2se/src/main/org/jboss/mx/util/ThreadPool.java
Modified:
   trunk/j2se/src/main/javax/management/MBeanServerFactory.java
   trunk/j2se/src/main/javax/management/timer/Timer.java
   trunk/j2se/src/main/org/jboss/mx/util/RunnableScheduler.java
   trunk/j2se/src/main/org/jboss/mx/util/SchedulableRunnable.java
Log:
JBAS-2719  
Rather than try to fix the thread name, refactored RunnableScheduler to use the
existing org.jboss.util.timeout.TimeoutFactory class.  This also has the
advantage of removing a lot of code and being able to (potentially) specify the
JBoss thread pool behavior.  I'm not sure these classes are in use anymore
because JDK 1.5 has the Timer class built in.


Modified: trunk/j2se/src/main/javax/management/MBeanServerFactory.java
===================================================================
--- trunk/j2se/src/main/javax/management/MBeanServerFactory.java	2006-11-06 22:35:20 UTC (rev 58166)
+++ trunk/j2se/src/main/javax/management/MBeanServerFactory.java	2006-11-06 22:37:18 UTC (rev 58167)
@@ -133,8 +133,8 @@
 
          try
          {
-            Method m = server.getClass().getMethod("releaseServer", null);
-            m.invoke(server, null);
+            Method m = server.getClass().getMethod("releaseServer", (Class [])null);
+            m.invoke(server, (Object[])null);
          }
          catch (Exception ignored)
          {

Modified: trunk/j2se/src/main/javax/management/timer/Timer.java
===================================================================
--- trunk/j2se/src/main/javax/management/timer/Timer.java	2006-11-06 22:35:20 UTC (rev 58166)
+++ trunk/j2se/src/main/javax/management/timer/Timer.java	2006-11-06 22:37:18 UTC (rev 58167)
@@ -32,6 +32,8 @@
 import javax.management.NotificationBroadcasterSupport;
 import javax.management.ObjectName;
 
+import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
+import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
 import org.jboss.logging.Logger;
 import org.jboss.mx.util.RunnableScheduler;
 import org.jboss.mx.util.SchedulableRunnable;
@@ -78,10 +80,10 @@
    // Attributes ----------------------------------------------------
 
    /** The next notification id. */
-   int nextId = 0;
+   private SynchronizedInt nextId = new SynchronizedInt(0);
 
    /** The next notification sequence number. */
-   long sequenceNumber = 0;
+   private SynchronizedLong sequenceNumber = new SynchronizedLong(0);
 
    /** The send past events attribute. */
    boolean sendPastNotifications = false;
@@ -149,13 +151,13 @@
       throws IllegalArgumentException
    {
       // Generate the next id.
-      int newId = 0;
-      newId = ++nextId;
-      Integer id = new Integer(newId);
+      Integer id = new Integer(nextId.increment());
 
       // Validate and create the registration.
       RegisteredNotification rn =
          new RegisteredNotification(id, type, message, userData, date, period, nbOccurences, fixedRate);
+	  if (log.isTraceEnabled())
+		  log.trace("new " + rn);
 
       // Add the registration.
       synchronized(notifications)
@@ -313,29 +315,23 @@
     }
 
     // The spec says to reset the identifiers, seems like a bad idea to me
-    synchronized (this)
-    {
-       nextId = 0;
-    }
+    nextId.set(0);
   }
 
   public void removeNotification(Integer id)
     throws InstanceNotFoundException
   {
 
-     log.debug("removeNotification: " + objectName + ",id=" + id);
+	  if (log.isTraceEnabled())
+		 log.trace("removeNotification: " + objectName + ",id=" + id);
 
     // Check if there is a notification.
     synchronized(notifications)
     {
-       RegisteredNotification rn = (RegisteredNotification) notifications.get(id);
+       RegisteredNotification rn = (RegisteredNotification) notifications.remove(id);
        if (rn == null)
-         throw new InstanceNotFoundException("No notification id : " +
-                                          id.toString());
-
-       // Remove the notification
+         throw new InstanceNotFoundException("No notification id: " + id);
        rn.setScheduler(null);
-       notifications.remove(id);
     }
   }
 
@@ -344,7 +340,8 @@
   {
     boolean found = false;
 
-     log.debug("removeNotifications: " + objectName + ",type=" + type);
+	  if (log.isTraceEnabled())
+	 	log.trace("removeNotifications: " + objectName + ",type=" + type);
 
     // Loop through the notifications removing the passed type.
     synchronized(notifications)
@@ -370,7 +367,7 @@
 
    public void setSendPastNotifications(boolean value)
    {
-      log.debug("setSendPastNotifications: " + objectName + ",value=" + value);
+      log.trace("setSendPastNotifications: " + objectName + ",value=" + value);
       sendPastNotifications = value;
    }
 
@@ -381,7 +378,7 @@
          return;
       active = true;
 
-      log.debug("start: " + objectName + " at " + new Date());
+	  log.debug("start: " + objectName);
 
       // Perform the initial sends, for past notifications send missed events
       // otherwise ignore them
@@ -410,7 +407,7 @@
     if (active == false)
       return;
 
-     log.debug("stop: " + objectName + ",now=" + new Date());
+	 log.debug("stop: " + objectName);
 
     // Stop the threads
     active = false;
@@ -457,6 +454,7 @@
    */
   private void sendNotifications(RegisteredNotification rn)
   {
+     boolean trace = log.isTraceEnabled();
      // Keep going until we have done all outstanding notifications.
      // The loop ends when not active, or there are no outstanding
      // notifications.
@@ -470,13 +468,9 @@
         // Yes, unless start and not sending past notifications.
         if (rn.sendType != SEND_NO)
         {
-           long seq = 0;
-           synchronized (this)
-           {
-              seq = ++sequenceNumber;
-           }
-
-           log.debug("sendNotification: " + rn);
+           long seq = sequenceNumber.increment();
+		   if (trace)
+			   log.trace("sendNotification: " + rn);
            TimerNotification tn = new TimerNotification(rn.type, objectName,
               seq, rn.nextDate, rn.message, rn.id);
            tn.setUserData(rn.userData);
@@ -492,7 +486,8 @@
            {
               synchronized (notifications)
               {
-                 log.debug("remove: " + rn);
+				 if (trace)
+                 	log.trace("remove: " + rn);
                  notifications.remove(rn.id);
               }
            }
@@ -594,11 +589,7 @@
          this.period = period;
          this.occurences = occurences;
          this.fixedRate = fixedRate;
-
          this.nextDate = this.startDate;
-
-         String msgStr = "new " + this.toString();
-         log.debug(msgStr);
       }
 
       // Public --------------------------------------------------------
@@ -647,7 +638,7 @@
 
       public String toString()
       {
-         return " RegisteredNotification: [timer=" + objectName + ",id=" + id + ",startDate=" + new Date(startDate) +
+         return "RegisteredNotification[timer=" + objectName + ",id=" + id + ",startDate=" + new Date(startDate) +
                 ",period=" + period + ",occurences=" + occurences + ",fixedRate=" + fixedRate +
                 ",nextDate=" + new Date(nextDate) + "]";
       }

Modified: trunk/j2se/src/main/org/jboss/mx/util/RunnableScheduler.java
===================================================================
--- trunk/j2se/src/main/org/jboss/mx/util/RunnableScheduler.java	2006-11-06 22:35:20 UTC (rev 58166)
+++ trunk/j2se/src/main/org/jboss/mx/util/RunnableScheduler.java	2006-11-06 22:37:18 UTC (rev 58167)
@@ -24,6 +24,10 @@
 import java.util.Iterator;
 import java.util.TreeSet;
 
+import org.jboss.util.threadpool.ThreadPool;
+import org.jboss.util.timeout.TimeoutFactory;
+import org.jboss.util.timeout.Timeout;
+
 /**
  * A runnable scheduler.<p>
  * 
@@ -37,7 +41,6 @@
  * @version $Revision$
  */
 public class RunnableScheduler
-   implements Runnable
 {
 
    // Attributes ----------------------------------------------------
@@ -45,85 +48,44 @@
    /**
     * The runnables to schedule
     */
-   private TreeSet runnables = new TreeSet();
+   private TimeoutFactory factory;
 
    /**
-    * The thread pool used to process the runnables.
+    * Constructs a new runnable scheduler.
     */
-   private ThreadPool threadPool;
+   public RunnableScheduler()
+   {
+	   this.factory = new TimeoutFactory();
+   }
 
    /**
-    * The controller thread.
-    */
-   private Thread controller = null;
-
-   // Static --------------------------------------------------------
-
-   // Constructors --------------------------------------------------
-
-   /**
     * Constructs a new runnable scheduler.
     */
-   public RunnableScheduler()
+   public RunnableScheduler(ThreadPool threadPool)
    {
+	   this.factory = new TimeoutFactory(threadPool);
    }
 
    /**
-    * Start the scheduler
+    * Starts the scheduler.
     */
-   public synchronized void start()
+   public void start()
    {
-      //log.debug("start");
-      if (controller != null)
-         return;
-      controller = new Thread(this);
-      controller.setDaemon(true);
-      controller.start();
    }
 
    /**
-    * Stop the scheduler
+    * Stops the scheduler, cancels all submitted jobs.
     */
    public synchronized void stop()
    {
-      //log.debug("stop");
-      if (controller == null)
-         return;
-      controller.interrupt();
-      controller = null;
+	  factory.cancel();
    }
 
    /**
-    * Run the scheduler
+    * Runs the scheduler.
     */
    public void run()
    {
-      // Start the threadpool
-      threadPool = new ThreadPool();
-      threadPool.setActive(true);
-      try
-      {
-         // Do outstanding work until stopped
-         while (true)
-         {
-            try
-            {
-               runOutstanding();
-               waitOutstanding();
-            }
-            catch (InterruptedException weAreDone)
-            {
-               //log.debug("interupted");
-               break;
-            }
-         }
-      }
-      finally
-      {
-         // Stop the thread pool
-         threadPool.setActive(false);
-         threadPool = null;
-      }
    }
 
    // Public --------------------------------------------------------
@@ -141,85 +103,13 @@
     *
     * @param runnable the runnable to add
     */
-   synchronized void add(SchedulableRunnable runnable)
+   Timeout add(SchedulableRunnable runnable)
    {
-      runnables.add(runnable);
-      notifyAll();
+      return factory.schedule(runnable.getNextRun(), runnable);
    }
 
-   /**
-    * Remove a schedulable runnable
-    *
-    * @param runnable the runnable to add
-    */
-   synchronized void remove(SchedulableRunnable runnable)
-   {
-      runnables.remove(runnable);
-   }
-
-   /**
-    * Check whether the scheduler contains a runnable
-    *
-    * @param runnable the runnable to check
-    * @return true when the runnable is present, false otherwise
-    */
-   synchronized boolean contains(SchedulableRunnable runnable)
-   {
-      return runnables.contains(runnable);
-   }
-
    // Private -------------------------------------------------------
 
-   /**
-    * Run all outstanding runnables, they are in date order
-    */
-   private synchronized void runOutstanding()
-   {
-      long current = System.currentTimeMillis();
-      Iterator iterator = runnables.iterator();
-      while (iterator.hasNext())
-      {
-         SchedulableRunnable next = (SchedulableRunnable) iterator.next();
-         if (next.getNextRun() <= current)
-         {
-            //log.debug("runOutstanding: " + next);
-            iterator.remove();
-            threadPool.run(next);
-         }
-         else
-         {
-            //log.debug("runOutstanding: break");
-            break;
-         }
-      }
-   }
-
-   /**
-    * Wait for the next outstanding runnable
-    */
-   private synchronized void waitOutstanding()
-      throws InterruptedException
-   {
-      // There is nothing to run
-      if (runnables.size() == 0)
-      {
-         //log.debug("waitOutstanding_1");
-         wait();
-         //log.debug("waitOutstanding_1 - wakeup");
-      }
-      else
-      {
-         // Wait until the next runnable
-         SchedulableRunnable next = (SchedulableRunnable) runnables.first();
-         long current = System.currentTimeMillis();
-         long wait = next.getNextRun() - current;
-         //log.debug("waitOutstanding_2 until: " + new Date(current + wait));
-         if (wait > 0)
-            wait(wait);
-         //log.debug("waitOutstanding_2 - wakeup");
-      }
-   }
-
    // Inner Classes -------------------------------------------------
 }
 

Modified: trunk/j2se/src/main/org/jboss/mx/util/SchedulableRunnable.java
===================================================================
--- trunk/j2se/src/main/org/jboss/mx/util/SchedulableRunnable.java	2006-11-06 22:35:20 UTC (rev 58166)
+++ trunk/j2se/src/main/org/jboss/mx/util/SchedulableRunnable.java	2006-11-06 22:37:18 UTC (rev 58167)
@@ -22,6 +22,7 @@
 package org.jboss.mx.util;
 
 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
+import org.jboss.util.timeout.Timeout;
 
 /**
  * A schedulable runnable.<p>
@@ -39,16 +40,11 @@
  * @version $Revision$
  */
 public abstract class SchedulableRunnable
-   implements Comparable, Runnable
+   implements Runnable
 {
    // Attributes ----------------------------------------------------
 
    /**
-    * A unique identifier
-    */
-   private long id;
-
-   /**
     * The next run timestamp
     */
    private SynchronizedLong nextRun = new SynchronizedLong(0);
@@ -68,13 +64,13 @@
     */
    private boolean reschedule;
 
-   // Static --------------------------------------------------------
-
    /**
-    * The next unique identifier
+    * Handle for canceling ourselves.
     */
-   private static long nextId = 0;
+   private Timeout timeout;
 
+   // Static --------------------------------------------------------
+
    // Constructors --------------------------------------------------
 
    /**
@@ -82,7 +78,6 @@
     */
    public SchedulableRunnable()
    {
-      this.id = getNextId();
    }
 
    // Public --------------------------------------------------------
@@ -108,8 +103,8 @@
    public synchronized void setNextRun(long nextRun)
    {
       // Remove from scheduler
-      if (scheduler != null)
-         scheduler.remove(this);
+      if (timeout != null)
+         timeout.cancel();
 
       // Set the new run time
       this.nextRun.set(nextRun);
@@ -119,7 +114,7 @@
       if (running == false && scheduler != null)
       {
          //log.debug("add to scheduler: " + this);
-         scheduler.add(this);
+         timeout = scheduler.add(this);
       }
       else
       {
@@ -144,8 +139,8 @@
       RunnableScheduler result = this.scheduler;
 
       // Remove from previous scheduler
-      if (this.scheduler != null)
-         this.scheduler.remove(this);
+      if (this.timeout != null)
+         timeout.cancel();
 
       // Set the new state
       this.scheduler = scheduler;
@@ -157,7 +152,7 @@
       // If we are not running, add it to the scheduler otherwise
       // remember we want adding
       else if (running == false)
-         scheduler.add(this);
+         timeout = scheduler.add(this);
       else
          reschedule = true;
 
@@ -191,31 +186,8 @@
       }
    }
 
-   // Runnable Implementation ---------------------------------------
-
-   public int compareTo(Object o)
-   {
-       SchedulableRunnable other = (SchedulableRunnable) o;
-       long temp = this.nextRun.get() - other.nextRun.get();
-       if (temp < 0)
-          return -1;
-       if (temp > 0)
-          return +1;
-       temp = this.id - other.id;
-       if (temp < 0)
-          return -1;
-       if (temp > 0)
-          return +1;
-       return 0;
-   }
-
    // Object Overrides ----------------------------------------------
 
-   public boolean equals(Object obj)
-   {
-      return (compareTo(obj) == 0);
-   }
-
    // Protected -----------------------------------------------------
 
    // Package -------------------------------------------------------
@@ -237,18 +209,10 @@
    {
       running = false;
       if (reschedule == true)
-         scheduler.add(this);
+         timeout = scheduler.add(this);
       reschedule = false;
    }
 
-   /**
-    * Get the next identifier
-    */
-   private static synchronized long getNextId()
-   {
-      return nextId++;
-   }
-
    // Inner Classes -------------------------------------------------
 }
 

Deleted: trunk/j2se/src/main/org/jboss/mx/util/ThreadPool.java
===================================================================
--- trunk/j2se/src/main/org/jboss/mx/util/ThreadPool.java	2006-11-06 22:35:20 UTC (rev 58166)
+++ trunk/j2se/src/main/org/jboss/mx/util/ThreadPool.java	2006-11-06 22:37:18 UTC (rev 58167)
@@ -1,278 +0,0 @@
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2005, JBoss Inc., and individual contributors as indicated
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
-*/
-package org.jboss.mx.util;
-
-import java.util.Stack;
-
-/**
- * A simple thread pool. Idle threads are cached for future use.
- * The cache grows until it reaches a maximum size (default 10).
- * When there is nothing in the cache, a new thread is created.
- * By default the threads are daemon threads.
- *
- * <a href="mailto:rickard.oberg at telkel.com">Rickard \u00d6berg</a>
- * <a href="mailto:adrian.brock at happeningtimes.com">Adrian Brock</a>
- * @version $Revision$
- */
-public class ThreadPool
-{
-  // Constants -----------------------------------------------------
-
-  // Attributes ----------------------------------------------------
-  private static int counter = 0;
-
-  /**
-   * Stack of idle threads cached for future use.
-   */
-  private Stack pool = new Stack();
-
-  /**
-   * Maximum number of idle threads cached in this pool.
-   */
-  private int maxSize = 10;
-
-  /**
-   * Is the thread pool active
-   */
-  private boolean active = false;
-
-  /**
-   * Whether the threads are daemon threads.
-   */
-  private boolean daemon = true;
-
-  // Static --------------------------------------------------------
-
-  // Constructors --------------------------------------------------
-
-  /**
-   * Create a new pool.
-   */
-  public ThreadPool()
-  {
-  }
-
-  /**
-   * Create a new pool with an activity status
-   *
-   * @param active true for active, false otherwise
-   */
-  public ThreadPool(boolean active)
-  {
-     this.active = active;
-  }
-
-  // Public --------------------------------------------------------
-
-  /**
-   * Set the maximum number of idle threads cached in this pool.
-   *
-   * @param size the new maximum size.
-   */
-  public void setMaximumSize(int size)
-  {
-    maxSize = size;
-  }
-
-  /**
-   * Get the maximum number of idle threads cached in this pool.
-   *
-   * @return the maximum size
-   */
-  public int getMaximumSize()
-  {
-    return maxSize;
-  }
-
-  /**
-   * Set the activity status of the pool. Setting the pool to
-   * inactive, clears the pool.
-   *
-   * @param status pass true for active, false otherwise.
-   */
-  public void setActive(boolean status)
-  {
-    active = status;
-    if (active == false)
-      while (pool.size() > 0)
-        ((Worker)pool.pop()).die();
-  }
-
-  /**
-   * Get the activity status of the pool.
-   *
-   * @return true for an active pool, false otherwise.
-   */
-  public boolean isActive()
-  {
-    return active;
-  }
-
-  /**
-   * Set whether new threads are daemon threads.
-   *
-   * @param value pass true for daemon threads, false otherwise.
-   */
-  public void setDaemonThreads(boolean value)
-  {
-    daemon = value;
-  }
-
-  /**
-   * Get whether new threads are daemon threads.
-   *
-   * @return true for daemon threads, false otherwise.
-   */
-  public boolean getDaemonThreads()
-  {
-    return daemon;
-  }
-
-  /**
-   * Do some work.
-   * This will either create a new thread to do the work, or
-   * use an existing idle cached thread.
-   *
-   * @param work the work to perform.
-   */
-   public synchronized void run(Runnable work)
-   {
-     if (pool.size() == 0)
-       new Worker(work);
-     else 
-     {
-       Worker worker = (Worker) pool.pop();
-       worker.run(work);
-     }
-   }
-
-   // Private -------------------------------------------------------
-
-   /**
-    * Put an idle worker thread back in the pool of cached idle threads.
-    * This is called from the worker thread itself. When the cache is
-    * full, the thread is discarded.
-    *
-    * @param worker the worker to return.
-    */
-   private synchronized void returnWorker(Worker worker)
-   {
-     if (pool.size() < maxSize)
-       pool.push(worker);
-     else
-       worker.die();   
-   }
-
-   // Inner classes -------------------------------------------------
-
-  /**
-   * A worker thread runs a worker.
-   */
-  class Worker extends Thread
-  {
-    /**
-     * Flags that this worker may continue to work.
-     */
-    boolean running = true;
-
-    /**
-     * Work to do, of <code>null</code> if no work to do.
-     */
-    Runnable work;
-
-    /**
-    * Create a new Worker to do some work.
-    *
-    * @param work the work to perform
-    */
-    Worker(Runnable work)
-    {
-	   // give it a thread so we can figure out what this thread is in debugging
-	   super("ThreadPoolWorker["+(++counter)+"]");
-      this.work = work;
-      setDaemon(daemon);
-      start();
-    }
-
-    /**
-     * Tell this worker to die.
-     */
-    public synchronized void die()
-    {
-      running = false;
-      this.notify();
-    }
-
-    /**
-     * Give this Worker some work to do.
-     *
-     * @param the work to perform.
-     * @throws IllegalStateException If this worker already
-     *         has work to do.
-     */
-    public synchronized void run(Runnable work)
-    {
-      if (this.work != null)
-        throw new IllegalStateException("Worker already has work to do.");
-      this.work = work;
-      this.notify();
-    }
-
-    /**
-     * The worker loop.
-     */
-    public void run()
-    {
-      while (active && running)
-      {
-        // If work is available then execute it
-        if (work != null)
-        {
-          try
-          {
-            work.run();
-          }
-          catch (Exception ignored) {}
-
-          // Clear work
-          work = null;
-        }
-                
-        // Return to pool of cached idle threads
-        returnWorker(this);
-
-        // Wait for more work to become available
-        synchronized (this)
-        {
-          while (running && work == null)
-          {
-            try
-            {
-              this.wait();
-            }
-            catch (InterruptedException ignored) {}
-          }
-        }
-      }
-    }
-  }
-}




More information about the jboss-cvs-commits mailing list