Programming Java threads in the real world, Part 9

More threads in an object-oriented world: Synchronous dispatchers, active objects, detangling console I/O

1 2 3 4 Page 4
Page 4 of 4
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
static public class Test extends Thread
    {
private String message;
private DataOutputStream data =
                                new DataOutputStream( Console.out() );
public Test( String message )
        {   this.message = message;
        }
public void run()
        {   try
            {   Random indeterminate_time = new Random();
                for(int count = 2; --count >= 0 ;)
                {   for( int i = 0; i < message.length(); ++i )
                    {   Console.out().write( message.charAt(i) );
                        sleep( Math.abs(indeterminate_time.nextInt()) % 20 );
                    }
                    Console.out().println( "(" + count + ")" );
                    sleep( Math.abs(indeterminate_time.nextInt()) % 20 );
                    data.writeChars( "[" + count + "]" );
                    sleep( Math.abs(indeterminate_time.nextInt()) % 20 );
                    Console.out().write('\n');
                }
            }
            catch( Exception e )
            {   Console.out().println( e.toString() );
            }
        }
static public void main( String[] args ) throws Exception
        {
            Thread t1 = new Test( "THIS MESSAGE IS FROM THREAD ONE" );
            Thread t2 = new Test( "this message is from thread two" );
            t1.start();
            t2.start();
            t1.join();  // Wait for everything to get enqueued
            t2.join();
            Console.out().close();  // wait for everything to be printed
        }
    }
}

Wrapping things up, books, and JavaOne

So, that's it for threads. The nine parts of this series, when taken together, give you a pretty good introduction to real Java threading (as compared to the simplified version found in most books). I've covered everything from thread architectures to common problems in multithreaded systems to atomic-level synchronization classes to the architectural approach to threading discussed this month. The tools I've developed along the way provide a good foundation to a multithreaded toolbox that can make your life as a Java thread programmer much easer.

For those of you who would like to see all the material I've been discussing in one place, an expanded version of this threading series will turn into the book Taming Java Threads, to be published by Apress (see Resources).

I'll also be speaking about many of the thread-related topics I've discussed in this series at the upcoming JavaOne Worldwide Java Developer Conference on Tuesday, June 15, from 2:45 p.m.to 5:00 p.m. in Moscone Center's Hall E.

And now for something completely different...

Next month I plan to change the subject entirely, turning the discussion to object-oriented ways to implement user interfaces. Most of the RAD tools for Java programming (such as Symantec's Café, IBM's Visual Age, Microsoft's J++, and so on) produce code that isn't in the least bit object-oriented. Using these RAD tools cuts you off from many of the benefits of object-oriented design (like fast debugging and ease of maintenance). The tool-generated code is much harder to debug and maintain than that of a carefully crafted object-oriented system, and these tools really add time to the overall development cycle.

Java itself is so easy to program that by the time you've hacked up the machine-generated code to make it maintainable, you may as well have written it correctly to begin with. And if you don't take steps to make the machine-generated systems maintainable, you will loose enormous amounts of time down the road as the code has to be updated. To paraphrase Fred Brooks, author of the classic book The Mythical Man Month, there is no silver bullet that kills the werewolf of development time. Next month I'll explain this further by describing exactly what an object-oriented approach to UI design would look like. Subsequent columns will present a series of classes that will help you build true object-oriented interfaces using Java.

Learn more about this topic

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