понедельник, 29 марта 2010 г.

The Java 7 Features Bound to Make Developers More Productive

If you've tracked each major Java release, you probably were eager to see what new packages were in each one and how you could employ them in your Java projects. Along the same lines, the next major Java SE release, Java 7, promises several new features across all packages, such as modularization, multi-language support, developer productivity tools, and performance improvement. I think programmers eventually will begin specializing in individual Java packages (i.e., java.util programmers, java.io programmers, java.lang programmers, etc.), but until then, let's explore a few of the notable new developer productivity features slated for Java 7.

New Objects Class

The new Objects class of the java.util package provides a fail-proof way for comparing two objects at runtime:
  1. The equals() method of the Objects class does a reference comparison.
  2. The deepEquals() method piggybacks on the first argument's equals() method definition.
Similarly, when both the arguments are object arrays, Array.deepEquals() is invoked on the objects. The new Objects class provides all the required static utility methods.

New Classes to Operate on File System

Java SE 7 provides classes that greatly simplify the age old integration processes of one application dropping files at a predefined shared location and other application picking them up. Java 7 provides a new class WatchService that notifies any events that take place in the file system under the watch.
The following steps create an asynchronous file-watcher service:
  1. Obtain the path from the File class.
    Path fPath = new File(filePath).toPath();


  2. Obtain a handle to the Watch service from the file system.
    dirWatcher = fPath.getFileSystem().newWatchService();


  3. Register which type of events you are interested in.
    fPath.register(dirWatcher,    
    StandardWatchEventKind.ENTRY_CREATE, 
    StandardWatchEventKind.ENTRY_DELETE, StandardWatchEventKind.ENTRY_MODIFY);


  4. Wait for the event to happen.
    try{
    WatchKey key = dirWatcher.take();
    }catch(InterruptedException ie){
    return;
    }


    The WatchKey class now has all the details of the event that occurred in the directory.

  5. Loop through Step 4 to continue receiving events.

New Classes for Concurrency Package

The Java SE team added a wide variety of new classes to Java 7 to cater to various concurrency functionalities. Most notable among them are the RecursiveAction and RecursiveTask classes, which simplify new algorithm development. Understanding the difference between heavyweight and lightweight processes will help you grasp the value of these new classes.
  • A heavyweight process gets a replica of the code, stack, and data from the parent process. You create a heavyweight process by invoking fork().
  • A lightweight process gets its own stack and shares resources and data with other threads or the parent thread. The Unix Thread API standard (POSIX) provides methods to create a thread.
Java 7 defines a new abstract class called ForkJoinTask, a lightweight process that generates a distinct stream of control flow from within a process. RecursiveAction and RecursiveTask are abstract subclasses of ForkJoinTask.
To code a recursive call, you must subclass either one of these classes and define the compute() method. The getRawResult() method returns null for RecursiveAction and returns a value for RecursiveTask. The Java 7 documentation provides a simple example for each of these classes.

Code Development Made Too Easy?

For me, the joy of being a computer scientist is spending long hours writing code for various algorithms. The problem solving keeps my mind alert, and the computations keep going in my head even in sleep. All the utilities in Java 7 take much of that joy of programming away, but they contribute to the bottom line for the companies supporting Java projects, which is what really matters for Java

Комментариев нет:

Отправить комментарий

Примечание. Отправлять комментарии могут только участники этого блога.