10 Reasons We Need Java 3.0
Pages: 1, 2
6. Fix threads.
Java was the first major language to integrate multithreading as a fundamental feature rather than a special purpose add-on library. Thus, it's not surprising that its designers made a few mistakes and missteps in this area. All of these need to be fixed:
As Sun's Joshua Bloch wrote, "Thread groups are best viewed as an unsuccessful experiment, and you may simply ignore their existence." (Effective Java, Addison-Wesley, 2001) They don't provide the security they were intended to provide, and the minor functionality they do provide can easily be moved into the
Threadclass itself.The
stop(),suspend(), andresume()methods are all rightly deprecated because they have the potential to leave objects in inconsistent states. They should be removed from theThreadclass completely.The
destroy()method isn't implemented. It just clutters the API. Get rid of it.It's become widely known that the Java memory model is broken with respect to "the semantics of threads, locks, volatile variables, and data races." Indeed, an expert group has been formed within the JCP to fix this, but not a lot has been heard from it since it was constituted a year ago. Without doubt, this is a hard problem; but maybe removing concern for upwards compatibility can help fix it.
The non-atomic nature of
doublesandlongsis a sop thrown to architectures that can't efficiently do 64-bit operations. That's not nearly as much an issue today as it used to be, however, and few VMs ever took advantage of it. If these types aren't made into objects, then they need to be as atomic as the other single-byte types.Finally, we should seriously consider the possibility that monitors can be decoupled from objects so that an object can have multiple monitors for different operations. I'm not a thread expert (and I generally embarrass myself whenever I pretend to be one), but I've heard a lot of arguments from both sides on this point, most of which have gone right over my head. If we're redesigning Java threads, maybe we can move this discussion from boozy barroom chats to a serious discussion and figure out if there's some way to reconcile the two sides.
These changes are going to be tricky, and they're going to require changes at all three levels -- the API, the language specification, and the virtual machine. But they are important, if Java is to remain efficient and reliable on the multiprocessor systems of tomorrow.
5. Convert file formats to XML.
|
Related Series
XML Basics for Java Developers, Part 5
XML Basics for Java Developers, Part 4
XML Basics for Java Developers, Part 3
XML Basics for Java Developers, Part 2
XML Basics for Java Developers, Part 1 |
The Java community is already using XML for latter-day file formats like Servlet config files and Ant build files. XML is clean, easy to edit, easy to parse, and easy to debug. It is rightly the first choice of most programmers when designing new file formats. Of course, XML wasn't invented until a couple of years after Java was released. Thus, Java has a number of non-XML file formats that should be ported to XML. Among others, these include JAR manifest files, properties files, and serialized objects. All of these can and should be replaced with well-formed XML.
Serializing objects with XML is perhaps the most surprising suggestion, since serialized objects are binary data and XML is text; however, most data inside objects are just text and numbers at the lowest level; and all of this is well-supported by XML. The limited true binary data inside Java objects can easily be Base-64 encoded. Perhaps most surprisingly, the resulting format should be both smaller and faster than today's binary serialization. Numerous developers have already invented custom XML formats for object serialization, and pretty much all of them have proved more efficient than Java's binary format. The fact is, contrary to popular belief, binary formats are not necessarily smaller or faster than the corresponding text formats, and serialized Java objects are a
particularly poorly-optimized binary format. Sun has already implemented an XML-based serialization format for JavaBeans in Java 1.4 in the java.beans.XMLEncoder and java.beans.XMLDecoder classes. Now it just needs to go a step further to cover all serializable objects.
4. Ditch the AWT.
Two GUI APIs is one too many. Most Java developers have chosen to standardize their work on Swing. I agree with them. It's time to merge the Component and JComponent classes, the Frame and JFrame classes, the Menu and JMenu classes, and so forth. In some cases, the classes would come from Swing (JPasswordField, JTable). In others, from the AWT (Font, Color, etc.) Still others (Frame, JFrame) would be merged, typically pulling in most of the code from Swing but retaining the more obvious AWT name. Overall, this would be a huge simplification for GUI development in Java and noticeably cut down on Java's bulk.
As long as we're at it, it's time to get rid of the legacies of the Java 1.0 event model. There's no reason for every component to have a series of confusing handleEvent(), mouseDown(), keyDown(), action(), and similar methods. If they're still being used behind the scenes as part of the infrastructure, at least make them non-public; but I suspect they can be eliminated completely without too much effort.
3. Rationalize the collections.
Java's current collections API is a hodgepodge of different designs implemented at different times. Some classes are thread-safe (Hashtable, Vector). Some aren't (LinkedList, HashMap). Some collections return null when a missing element is requested. Others throw an exception. Let's settle on some standard idioms and metaphors, and
design all the classes to fit them, rather the other way around. Probably the easiest way to do this would be to eliminate Vector and Hashtable completely. An ArrayList can do anything a Vector can do and a HashMap can replace a Hashtable.
2. Redesign I/O.
|
Related Reading
Java I/O |
The original Java developers were Unix programmers, Windows users, and Mac dilettantes. The I/O APIs they invented were more than a little Unix-centric in both obvious and not-so-obvious ways, and really didn't port very well. For instance, initially they assumed that the file system had a single root. This is true on Unix, but false on Windows and the Mac. Both the new and old I/O APIs still assume that the complete contents of a file can be accessed as a stream (true on Windows and Unix but false on the Mac).
Some of the problems, especially with regard to internationalization, were fixed in Java 1.1, with the introduction of the Reader and Writer classes and their subclasses. Java 1.2 fixed some of the more glaring inadequacies in the file system API. Still more were fixed in Java 1.4 with the new I/O APIs.
The job isn't done yet. For instance, even in Java 1.4 there still isn't a reliable means to copy or move a file -- pretty basic operations, I think you'll agree. To date, attempts to design a better file-system API have foundered on the need to be upwards-compatible with the atrocious Java 1.0 I/O classes. The time has come to reconsider everything in java.io. Some of the more urgently needed changes are:
The
Fileclass needs to represent a real file on the file system rather than a file name. It should provide full access to the file's metadata, support various naming conventions, allow for operations on the file itself, such as copying, moving, and deleting, and in general, recognize that a file is more than just a bucket of bytes.The
PrintStreamclass is a disaster. It should be removed.System.outandSystem.errcan bePrintWritersinstead. (Sun originally planned to make this change in Java 1.1, but decided it would break too much existing code.)The
readUTF()andwriteUTF()methods inDataInputStreamandDataOutputStreamdon't actually support UTF-8. What they support is 90% real UTF, 10% meat-byproduct. There's nothing actually wrong with the formats they support, except that this causes problems for inexperienced users who use them to read and write UTF-8, and then wonder why their code breaks when exchanging data with conformant software from other languages. These methods should be renamedreadString()andwriteString().The
ReaderandWriterclasses desperately need agetCharacterSet()method that can help determine which characters the writer can safely output.Encodings should be identified with IANA-registered names like ISO-8859-1 and UTF-16 instead of Java class names like 8859_1 and UTF16.
Buffering I/O is one of the most important performance optimizations a program can make. It should be turned on by default. The base
InputStream,OutputStream,Reader, andWriterclasses should have their own internal buffers, rather than requiring them to be chained to aBufferedInputStream/BufferedOutputStream/BufferedReader/BufferedWriter. Filters can check whether the stream they're chained to is buffered before deciding whether or not to use their own buffers.
1. Redesign class loading from scratch, this time with human interface factors in mind.
No single topic is as confusing to new users as the class path. I get almost daily e-mail from novice readers asking me to explain the "Exception in thread 'main' java.lang.NoClassDefFoundError: HelloWorld" error messages they keep seeing. I've been writing Java for seven years and I'm still occasionally baffled by class loader issues. (Pop quiz: When is class A that implements interface B not an instance of interface B? When A and B are loaded by two different class loaders. I lost half a day to that one just last week, and after I mentioned my problem on a mailing list, one talented programmer friend told me he lost two weeks to the exact same bug.)
I'll freely admit that I don't know how the class loader should be fixed. It's clearly one of the trickier areas of Java. I do know that the current system is far too difficult. There has to be a better way.
Summing Up
This top-ten list is just a beginning. There are lots of other areas where Java could be improved, if we allow ourselves to throw off the straitjacket of upwards compatibility: replacing integer constants with type-safe enums, removing confusing and unnecessary classes like StringTokenizer and StreamTokenizer, making Cloneable a true mixin interface or perhaps eliminating it completely, renaming the Math.log() method the Math.ln() method, adding support for true design by contract, eliminating checked exceptions (as Bruce Eckel has advocated), limiting objects to a single thread as in Eiffel, and much more.
We can argue about exactly which changes are necessary, and which ones may cause more harm than good. But one thing's for sure: if Java fails to change, if it refuses to correct its well-known problems, there are other languages waiting in the wings written by some very sharp programmers who have learned from Java's mistakes and are eager for the opportunity to replace Java in the same way Java replaced earlier flawed languages. Java must not be forever handicapped by mistakes made seven years ago in Java 1.0. There comes a point where we need to throw off the chains of backwards compatibility and move boldly into the future.
Elliotte Rusty Harold is a noted writer and programmer, both on and off the Internet. His previous books include "Java Network Programming", Third Edition, "XML in a Nutshell", Third Edition, and "Java I/O", all from O'Reilly.
Return to ONJava.com.
-
Commercial concerns must not be ignored!
2006-08-28 05:19:39 Infernoz [View]
-
why we need Serialization ?
2005-03-15 04:47:16 senthil_ak [View]
- Trackback from http://volition.vee.net/archives/000446.html
sick of listening
2004-10-28 05:11:54 [View]
- Trackback from http://www.opposable-thumbs.net/archives/2002/07/31/java_pop_quiz.html
Java Pop Quiz
2004-08-07 22:52:19 [View]
-
No
2004-07-20 21:41:48 grlea [View]
-
Almost forgot... Connection.close() throws SQLException :-(
2004-01-14 11:36:22 anonymous2 [View]
-
Fix String handling once and for all...
2004-01-14 10:42:34 anonymous2 [View]
-
Top Three New Features
2004-01-08 06:00:30 anonymous2 [View]
- Trackback from http://www.lyranthe.org/diary/archives/000786.html
Longhorn Developer Preview
2004-01-08 05:46:45 [View]
-
Multiple return values
2004-01-08 05:06:06 anonymous2 [View]
-
Garbage Collector
2004-01-08 02:44:33 anonymous2 [View]
-
Please don't slow down Java more that it already is!
2004-01-08 02:14:39 anonymous2 [View]
-
backwards compatibility, etc.
2004-01-07 10:09:40 anonymous2 [View]
-
Thread support bothers me
2003-10-17 07:39:19 anonymous2 [View]
-
Benefits
2003-08-27 07:37:30 anonymous2 [View]
-
That's the 10 Reasons That I Should Throw Away Java Now, To Embrace C?
2003-06-30 08:42:11 anonymous2 [View]
-
minor hitches/adjustments
2003-06-17 15:44:07 anonymous2 [View]
-
Perfect way to clean up:
2003-05-12 08:19:17 anonymous2 [View]
-
As long as we have JNI we need Thread.stop()
2003-05-01 07:01:00 anonymous2 [View]
-
Too good to be true.
2003-04-28 01:12:12 anonymous2 [View]
-
For more discussion
2003-03-19 01:41:01 anonymous2 [View]
-
Nice Article
2002-12-18 13:04:47 anonymous2 [View]
-
Functional Methods
2002-12-16 19:14:24 anonymous2 [View]
-
Perfect world
2002-10-24 15:03:33 anonymous2 [View]
-
Forgot a biggie
2002-08-27 17:39:19 piobair [View]
-
ClassLoader problem
2002-08-15 06:08:58 aanno [View]
-
Strange.. , If these are the general problems...
2002-08-13 07:41:28 chaosb [View]
-
This is one of the worst articles I've read in some time
2002-08-12 11:12:45 jacyg [View]
-
java first language with builtin thread-support?
2002-08-09 10:17:00 wolfgangr [View]
-
How it could be done
2002-08-08 12:11:57 vahur [View]
-
I agree, when's it coming out?
2002-08-07 12:37:19 cbare [View]
-
About Threads
2002-08-07 11:55:51 dfitzgeraldca [View]
-
What Mind-boggling Ignorance!
2002-08-07 07:24:16 toadskin [View]
-
Backward Compatibility and ClassLoaders
2002-08-07 06:16:06 as@bjss.co.uk [View]
-
make Object inherit Array and not visa-versa
2002-08-07 03:23:22 muchandr [View]
-
WE NEED AN UNSIGNED TYPE
2002-08-07 01:42:53 st8 [View]
-
Cannot agree will all ten reasons
2002-08-06 23:14:56 arocha_mo [View]
-
Ditch Swing no AWT
2002-08-06 22:23:11 nirpaz [View]
-
Leave log() as log()
2002-08-06 17:11:10 dgrant3 [View]
-
get rid of public constructors
2002-08-06 16:28:49 friendless [View]
-
Nuke finalizers?
2002-08-06 14:28:55 bezdar [View]
-
Reimplement WeakHashMap and "ObjectPool" in VM kernel
2002-08-06 14:11:04 bezdar [View]
-
Three more reasons for Java 3.0
2002-08-06 13:51:40 rayburns [View]
-
In real life?
2002-08-06 13:20:25 AGILA [View]
-
extensions ?
2002-08-06 11:54:01 jefu1 [View]
-
How about multiple jvm shared core api
2002-08-06 10:46:58 bobd [View]
-
Most important, but perhaps not Java 3
2002-08-06 10:31:27 mockturtleneck [View]
-
sounds very reasonable
2002-08-06 10:08:45 demagogue [View]
-
Interface naming collisions
2002-08-06 09:30:26 nielsen [View]
-
Case sensitivity
2002-08-06 09:07:43 jmpiazza [View]
-
Another thing
2002-08-06 09:04:16 st0rm [View]
-
I agree with *almost* everything
2002-08-06 09:01:15 st0rm [View]
-
Yes - clean up thread locks
2002-08-06 08:56:03 wmshub [View]
-
Better scalability
2002-08-06 08:12:09 rdorta [View]
-
add READ-WRITE parameters to methods
2002-08-06 08:02:22 mirek1 [View]
-
We need Generics and Built-in type safe enums
2002-08-06 07:53:41 dkaz [View]
-
Idea about constructor naming
2002-08-06 07:20:04 mcmilwj [View]
-
Agreed...just don't change the name
2002-08-06 07:14:08 problem1 [View]
-
Clean up syntax
2002-08-06 07:02:46 asalwin [View]
-
Reason #2: I/O
2002-08-06 06:56:00 asalwin [View]
-
Include some of the good parts of C++
2002-08-06 06:44:11 grs19689 [View]
-
Steal Objective-C's selectors, use inner classes far less.
2002-08-06 06:34:33 ewatkeys [View]
-
Agree on Some points but not all
2002-08-06 06:24:14 snowwolf [View]
-
11 - Add Properties as a language feature
2002-08-06 06:01:32 powersusa [View]
-
Obsolete code **can** be phased out
2002-08-06 05:32:22 David Collier-Brown |
[View]
-
Documentation in XML
2002-08-05 18:37:03 deanwampler [View]
-
a bunch of amateur ideas...
2002-08-05 08:20:29 morbid [View]
-
thread issue
2002-08-05 06:32:11 bobthedatabaseboy [View]
-
Agreed
2002-08-05 06:28:07 uncledave [View]
-
Learn from C#/.Net as they did from java
2002-08-05 02:01:53 joostdev [View]
-
Numero Uno: Add multilanguage support for JVM
2002-08-04 21:20:13 oreallyy [View]
-
byte primitives should be UNSIGNED
2002-08-04 18:37:57 bchapman [View]
-
Disagree in a few points
2002-08-04 09:58:08 EugeneKuleshov [View]
-
method byte-code size
2002-08-02 23:56:13 marc_recht [View]
-
fix naming...
2002-08-02 23:07:30 bchoi [View]
-
primitive types are awesome
2002-08-02 18:05:07 codemonkey [View]
-
Primitive types are needed!
2002-08-02 10:12:15 petilon [View]
-
Completely Agreed
2002-08-02 09:59:57 thanneru [View]
-
I am not convinced
2002-08-02 08:08:37 anonym_cvs [View]
-
Swing & Generics
2002-08-02 07:26:11 ianfairman [View]
-
File Systems
2002-08-02 06:34:19 phidias [View]
-
link to Eckel's checked exception article
2002-08-02 05:48:26 fwierzbicki [View]
-
Good ideas!
2002-08-02 03:26:25 mkluempe [View]
-
expression language
2002-08-02 01:41:00 danbunea [View]
-
Mandatory 'this.'
2002-08-01 22:34:42 bayard [View]
-
Couldn't it...
2002-08-01 13:13:28 sethadam1 [View]
-
Another: easier app-launcher
2002-08-01 12:48:18 Chris Adamson |
[View]
-
Really good ideas
2002-08-01 11:45:28 cybarlow [View]
-
Interesting
2002-08-01 05:51:21 bungle [View]