Working with Java New I/O (NIO)
A project I've been working on lately has involved restricting access to a registry file that might be used by concurrent Java processes. There is a distinction between threads and processes: multiple threads function within the scope of a single process, and multiple processes function within a single computer. Concurrent access to a resource by threads can be limited with Java's synchronization features. But limiting concurrent access by processes has, until Java 1.4, been unavailable.
Java runtime environments (JREs) version 1.4 or better feature an addition to the API called "New I/O", or NIO. Developers can use NIO to restrict access to a file not just by other Java processes, but by any native processes running on the computer. It is significant that Sun has taken advantage of the file locking facilities on each operating system (Windows, Mac, Solaris, etc.) to make NIO work. Because operating systems differ in their features and behavior, it is worth noting that the file locking features in NIO may function a bit differently across operating systems.
NIO features a concept called "Channels" which offer the ability to lock an entire file, or even portions of a file. Channels do not provide any support for restricting access to a locked file by concurrent threads; rather, this must be handled by the developer using thread synchronization.
Based on my experiences, NIO offers some very powerful block I/O and file locking features, but takes a while to understand within the broad scope of the Java language. Hopefully more tutorials and supporting documentation will be provided in the near future so that new and seasoned Java developers can take advantage of its many features.