| Sign In/My Account | View Cart |
10 Reasons We Need Java 3.0
Pages: 1, 2
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 Thread class itself.
The stop(), suspend(), and resume() methods are all rightly deprecated because they have the potential to leave objects in inconsistent states. They should be removed from the Thread class 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 doubles and longs is 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.
|
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.
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.
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.
|
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 File class 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 PrintStream class is a disaster. It should be removed. System.out and System.err can be PrintWriters instead. (Sun originally planned to make this change in Java 1.1, but decided it would break too much existing code.)
The readUTF() and writeUTF() methods in DataInputStream and DataOutputStream don'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 renamed readString() and writeString().
The Reader and Writer classes desperately need a getCharacterSet() 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, and Writer classes should have their own internal buffers, rather than requiring them to be chained to a BufferedInputStream/ 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.
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.
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.
Showing messages 1 through 138 of 138.
"Traditional approach"
void addNumber(int[] arr,int n)
{
for(int i=0;i<arr.length;i++)
{
arr[i]+=n;
}
}
"Functional" approach:
functional int[] addNumber(int[] arr,int n)
{
int[] arret=new int[arr.length];
for(int i=0;i<arr.length;i++)
{
arret[i]=arr[i]+n;
}
return arret;
}
</pre>
Another: easier app-launcher
10, 9 & 4. This would be suicide for a programming language, from a commercial and OSS library viewpoint, thus is not a smart suggestion.
8. Primitives are needed for efficent use of the CPU and memory, especially on slow or heavily loaded commercial machines. Putting huge extra load on the Garbage Collection, memory resources and the method call layer is not a sane suggestion. It is also not smart for the compiler to pick the data-type given this could cause unpredictable value overflow/underflow or performance problems if the wrong data-type was picked. Enums are a great idea (for new APIs or methods), but not where you have combined flag field values (an API design flaw).
7. There should be an application visible JVM to enable 4 byte characters/strings, with a system property to signal this feature, so that the JVM and application can switch to 4 byte character optimised libraries and convert constant character/string values, when classes are loaded. It would be stupid to enforce the change in byte-code because it would not be commercially viable.
6. stop() is critical for killing hanging or badly coded threads e.g. threads which ignore interrupt(), so must be retained in some form so that limited resources can be freed. Doubles and longs are a JVM concern, if you don't like the atomic behaviour put them in a synchronized object. Anyhow I dislike floating point data-types like doubles because they are so vunerable to decimal rounding errors and lack rounding rules, unlike BigDecimal, not a good thing when money is involved!
5. XML is suited for some uses, however it is very slow to parse, can be huge and has to be read from the start! I work with data from an XML based system and have seen quite serious performance problems with it, because of huge XML data size (in databases (uncompressed) and across networks (compressed)) and high marshalling/unmarshallings costs! Use of other formats, like CSV, is much more efficient in databases and networks, and they tend to compress better too. IMHO use of XML for settings files is useful in some cases, however I can often do better using standard properties and a Properties sub-class supporting heirachical text propeties e.g. store.9999.tills=2, store.9999.name=obvious
3. ArrayList is broken, because like many other java.* classes it has too many private properties, this makes it useless for some applications which require ordered lists e.g. some Swing lists applications! I would like to see all Lists include a locally optimised sort method and an ordered add method, because the Collections.class methods are often far too costly!
2. IO should be improved and this is happinging already, however I don't want File changed, tieing access to physical files would cause all kinds of resource and locking issues, you really don't want to go there!
1. Shows that you are naive about ClassLoader security and the need to isolate dynamic class compilation and loading for application like web servers etc.
I like some of your books, however they are not perfect, some of the IO books are flawed e.g. copying files with java.io streams and a byte, instead of java.nio FileChannels, yuck, I REALLY hate cruft like that, at the very least use a large byte array rather than a single byte!