Synchronized Queue : Concurrent « Collections Data Structure « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Collections Data Structure » ConcurrentScreenshots 
Synchronized Queue
   

/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 
 * Project: OpenSubsystems
 
 * $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 
 * This program 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 General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */


import java.util.LinkedList;
import java.util.List;

/**
 * Class that implement unlimited queue, that is synchronized. It means that
 * the consumer of the objects from the queue waits/is blocked in the get 
 * method until there is an object available.
 *
 @version $Id: SynchronizedQueue.java,v 1.4 2007/01/07 06:14:00 bastafidli Exp $
 @author Miro Halas
 * @code.reviewer Miro Halas
 * @code.reviewed Initial revision
 */
public class SynchronizedQueue
{
   // Attributes ///////////////////////////////////////////////////////////////
   
   /**
    * Cache of object produced by producer and consumed by consumer.
    */
   protected List m_lstObjects;

   // Constructors /////////////////////////////////////////////////////////////
   
   /**
    * Constructor for Synchronized Queue Object.
    */
   public SynchronizedQueue(
   )
   {
      super();

      m_lstObjects = new LinkedList();
   }

   // Logic ////////////////////////////////////////////////////////////////////
   
   /**
    * Destructor for Synchronized Queue. It is called when no other
    * object holds reference to it.
    *
    @exception Throwable - default destructor exception
    */
   protected void finalize(
   throws Throwable
   {
      // Explicitely remove this just to help garbage collector
      m_lstObjects.clear();
      m_lstObjects = null;

      super.finalize();
   }

   /**
    * Get the object from the beginning of the queue
    *
    @return Object - object from the queue, if the thread is blocked in this
    *                  function and you call interrupt method, an InterruptedException
    *                  will be thrown.
    @exception InterruptedException - if the thread is blocked in this
    *                                   function and you call interrupt method,
    *                                   an InterruptedException will be thrown.
    */
   public synchronized Object get(
   throws InterruptedException
   {
      Object objReturn = null;

      if (m_lstObjects.isEmpty())
      {
         // There is no object in the queue, go to sleep
         try
         {
            wait();
         }
         catch (InterruptedException ieException)
         {
            // Somebody woke us up, that means all threads waiting on this
            // object competed for the lock and this one won and the object is
            // locked again
            // The thread can be woken up in two conditions, producer put new
            // object into the queue or somebody called interrupt - to interrupt
            // the wait - in this case rethrow an exception
            if (m_lstObjects.isEmpty())
            {
               throw ieException;
            }
         }
      }

      // Remove the first object in the queue
      objReturn = m_lstObjects.remove(0);

      return objReturn;
   }

   /**
    * Put the object to the end of the queue.
    *
    @param objNew - new object, can be null
    */
   public synchronized void put(
      Object objNew
   )
   {
      m_lstObjects.add(objNew);
      // New object in the queue, notify others
      notifyAll();
   }

   /**
    * Test if the queue is empty.
    *
    @return boolean - true if the queue is empty
    */
   public synchronized boolean isEmpty(
   )
   {
      return m_lstObjects.isEmpty();
   }
}

   
    
    
  
Related examples in the same category
1.A version of Hashtable supporting concurrency for both retrievals and updates
2.A version of Hashtable that supports mostly-concurrent reading, but exclusive writing
3.Concurrent Doubly LinkedList
4.Returns the parent of the specified URI.
5.A daemon thread that continuously dequeues Runnable instances from a queue and executes them.
6.Lazy Loading Reference
7.Utility class that provides a lazy initialization object wrapper.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.