Programming Java threads in the real world, Part 6

The Observer pattern and mysteries of the AWTEventMulticaster

1 2 3 4 Page 4
Page 4 of 4
Listing 10 (Multicast_publisher.java): Using the multicaster in a publisher
01  
02  
03  
04  
05  
06  
07  
08  
09  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
import java.util.*;
import com.holub.tools.Subscriber;
import com.holub.tools.Multicaster;
public class Multicast_publisher
{
    Subscriber subscription_list; // = null
   public void subscribe( Subscriber subscriber )
    {   subscription_list =
                        Multicaster.add( subscription_list, subscriber);
    }
   public void cancel_subscription( Subscriber subscriber )
    {   subscription_list =
                    Multicaster.remove( subscription_list, subscriber );
    }
   public void publish()
    {   
        new Thread()
       {   public void run()
            {   if( subscription_list != null ) 
                    subscription_list.receive( "Hello world" );
            }
        }.start();
    }
   public void publish_sequentially()
    {
        new Thread()
        {   public void run()
            {   synchronized( subscription_list )
                {   if( subscription_list != null ) 
                        subscription_list.receive( "Hello world" );
                }
            }
        }.start();
    }
    //==================================================================
   public static class Test
    {   
       static private Multicast_publisher publisher  =
                                                new Multicast_publisher();
       static private Subscriber subscriber = 
                                new Subscriber()
                               {   public void receive(Object p)
                                    {   System.out.println( (String) p );
                                    }
                                };
       static public void main( String[] args )
        {   publisher.subscribe( subscriber );
            publisher.publish();            // Publish "Hello world" events
        }
    }
}

Wrapping up

So that's it for this month. The Observer pattern is enormously useful when you need to write code that can be used in any context. Since the publisher knows nothing about the subscribers (except that they implement the Subscriber interface), the publisher is truly reusable in the technical OO sense. AWT/Swing obviously leverages Observer in its event model, but I use this design pattern heavily in non-UI situations as well.

Next month I'll present the last of the class-related solutions to multithreading problems: a reader-writer lock that lets you control access to a shared global resource. I'll also discuss Critical Sections and Singletons. Subsequent columns will look at a few architectural solutions to threading problems such as Synchronous Dispatchers and Active Objects.

Related:
1 2 3 4 Page 4
Page 4 of 4