Re: JavaMemoryModel: Race-proof mutable objects and synchronized constructors

From: Allen Holub (allen@holub.com)
Date: Sat Dec 02 2000 - 14:35:23 EST


At 11:00 PM 12/1/00 Friday, Joseph Bowbeer wrote:
>Do you have a code sample?
>
>Are you just creating a thread or are you starting it, too?

Starting it too. Here's a stripped down version of the Thread_pool class
(the entire thing is part of the threading package in "Taming Java
Threads," full source on my web site <http://www.holub.com>). I've removed
the nonessential stuff, but I think the structure is still clear, and
hopefully I haven't broken anything while simplifying.

-Allen

public final class Thread_pool extends ThreadGroup
{
         private final Blocking_queue pool = new Blocking_queue(); //
blocks on dequeue from empty queue
         private volatile int pool_size = 0;
         private volatile int initial_size;
         private Object pooled_threads_all_running = new Object();
         //...

         private final class Pooled_thread extends Thread
         {
                 public Pooled_thread()
                 { super( Thread_pool.this, "T" + thread_id );
                 }

                 public void run()
                 { synchronized( Thread_pool.this )
                         { ++pool_size;
                                 if( pooled_threads_all_running != null &&
initial_size == pool_size )
                                 { synchronized(
pooled_threads_all_running )
                                         {
pooled_threads_all_running.notify();
                                         }
                                 }
                         }

                         try
                         { while( !isInterrupted() )
                                 { try
                                         { Runnable command =
(Runnable) pool.dequeue();
                                                 command.run();
                                         }
                                         catch( InterruptedException e)
                                         { //...
                                         }
                                 }
                         }
                         finally
                         { synchronized( Thread_pool.this )
                                 { --pool_size;
                                 }
                         }
                 }
         }

         public Thread_pool( int initial_thread_count )
         { super( "Thread_pool" + group_number++ );
                 synchronized( this )
                 { initial_size = initial_thread_count;
                         for(int i = initial_thread_count; --i >= 0 ;)
                                 new Pooled_thread().start();
                 }

                 synchronized( pooled_threads_all_running )
                 { try
                         { pooled_threads_all_running.wait();
                                 pooled_threads_all_running = null;
                         }
                         catch(InterruptedException e){}
                 }
         }
         //...
}

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:29 EDT