| You are receiving this e-mail because you elected to receive e-mail from Sun Microsystems, Inc. To update your communications preferences, please see the link at the bottom of this message. We respect your privacy and post our privacy policy prominently on our Web site http://sun.com/privacy/ |
![]() |
||||||
|
||||||
| In this Issue | ||
Here you'll get tips on using core Java technologies and APIs, such as those in Java 2 Platform, Standard Edition (J2SE). This issue covers: » Using InetAddress for Host Name Lookup and Host Reachability » Locks These tips were developed using the Java 2 Platform Standard Edition Development Kit 5.0 (JDK 5.0). You can download JDK 5.0 at http://java.sun.com/j2se/1.5.0/download.jsp. This issue of the Core Java Technologies Tech Tips is written by John Zukowski, president of JZ Ventures, Inc. See the Subscribe/Unsubscribe note at the end of this newsletter to subscribe to Tech Tips that focus on technologies and products in other Java platforms. For more Java technology content, visit these sites: java.sun.com - The latest Java platform releases, tutorials, and newsletters. java.net - A web forum where enthusiasts of Java technology can collaborate and build solutions together. java.com - Hot games, cool apps -- Experience the power of Java technology. |
||
| USING INETADDRESS FOR HOST NAME LOOKUP AND HOST REACHABILITY | ||
The
The
Here's an example that demonstrates the use of the
import java.net.*;
public class Lookup {
public static void main(String args[]) {
for (String name: args) {
try {
InetAddress address = InetAddress.getByName(name);
System.out.println("Name: " + address.getHostName());
System.out.println("Addr: " + address.getHostAddress());
} catch (UnknownHostException e) {
System.err.println("Unable to lookup " + name);
}
}
}
}
The
> java Lookup sun.com yahoo.com 66.94.234.13
Name: sun.com
Addr: 209.249.116.195
Name: yahoo.com
Addr: 216.109.112.135
Name: w2.rc.vip.scd.yahoo.com
Addr: 66.94.234.13
Some host names resolve to multiple IP addresses. Instead of
using the
Also, the
The
import java.net.*;
public class LookupAll {
public static void main(String args[]) {
for (String name: args) {
try {
InetAddress address[] =
InetAddress.getAllByName(name);
for (InetAddress each: address) {
System.out.println("Name: " + each.getHostName());
System.out.println("Addr: " +
each.getHostAddress());
System.out.println("Canonical: " +
each.getCanonicalHostName());
}
System.out.println("----");
} catch (UnknownHostException e) {
System.err.println("Unable to lookup " + name);
}
}
}
}
One such host with multiple addresses is google.com. The following example includes google.com and yahoo.com as arguments. The example better demonstrates how the reverse lookup of the IP address for yahoo.com resolves to a different host name.
>> java LookupAll yahoo.com google.com
Name: yahoo.com
Addr: 216.109.112.135
Canonical: w2.rc.vip.dcn.yahoo.com
Name: yahoo.com
Addr: 66.94.234.13
Canonical: w2.rc.vip.scd.yahoo.com
----
Name: google.com
Addr: 216.239.39.99
Canonical: 216.239.39.99
Name: google.com
Addr: 216.239.57.99
Canonical: 216.239.57.99
Name: google.com
Addr: 216.239.37.99
Canonical: 216.239.37.99
----
The
With J2SE 5.0, you can also use
The
For most people, the first version of
The second version of
The following program,
import java.io.*;
import java.net.*;
public class LookupReach {
public static void main(String args[]) {
for (String name: args) {
try {
InetAddress address = InetAddress.getByName(name);
System.out.println("Name: " + address.getHostName());
System.out.println("Addr: " +
address.getHostAddress());
System.out.println("Reach: " +
address.isReachable(3000));
} catch (UnknownHostException e) {
System.err.println("Unable to lookup " + name);
} catch (IOException e) {
System.err.println("Unable to reach " + name);
}
}
}
}
Try running
> java LookupReach yahoo.com sun.com google.com web.mit.edu
Name: yahoo.com
Addr: 216.109.112.135
Reach: false
Name: sun.com
Addr: 209.249.116.195
Reach: false
Name: google.com
Addr: 216.239.37.99
Reach: false
Name: web.mit.edu
Addr: 18.7.22.69
Reach: true
When you use the
If you aren't sure which version a name represents, you can
check if the One last thing worth mentioning: name lookups and reverse lookups can be expensive operations. The system uses a cache mechanism for both performance and security reasons. Once you look up a host, you always know you'll get the same one, and the results can't change between lookups. Back to Top |
||
| LOCKS | ||
|
One of the popular features of the J2SE 5.0 libraries is the
addition of concurrency utilities. Provided as part of JSR 166, the utilities provide
advanced concurrency programming capabilities that take
developers beyond the
The foundation of the package is the
Lock l = ...;
l.lock();
try {
// protected resource access
} finally {
l.unlock();
}
When the Lock class is used in this way, it works similarly to a typical synchronized lock:
synchronized(lockVariable) {
// protected resource access
}
Similar but not the same. If you don't get the
Another method provided by the Lock interface that acquires
a lock is
The typical usage pattern for the
Lock l = new ReentrantLock();
try {
l.lockInterruptibly();
try {
// protected resource access
} finally {
l.unlock();
}
} catch (InterruptedException e) {
System.err.println("Interrupted wait");
}
Here, the inner
A word of caution: calling
Another method provided by the Lock interface,
Lock lock = ...;
if (lock.tryLock()) {
try {
// protected resource access
} finally {
lock.unlock();
}
} else {
// alternative operation
}
This is like going to the copy machine, seeing that someone is busy with a long copy job, and moving on to a slower but available machine for the task.
The second version of the
For instance,
The 1 second = 1 thousand milliseconds 1 second = 1 million microseconds 1 second = 1 billion nanoseconds Although you've seen how to use the Lock interface, you haven't seen how to actually create a Lock. The prior examples all show: Lock lock = ...; The java.util.concurrent.locks package includes three implementations of the Lock interface:
The first implementation,
For
Lock l = new ReentrantLock();
l.lock();
try {
// protected resource access
} finally {
l.unlock();
}
Use a read-write lock when reads are long and frequent, and writes are infrequent. Your access to a protected object would then use separate locks acquired as shown here: ReadWriteLock rwl = new ReentrantReadWriteLock(); Lock readLock = rwl.readLock(); Lock writeLock = rwl.writeLock();
You then use the locks in the same manner as the
The only implementation of the For more information on the Lock support, see the package description Back to Top |
||
|
Comments? Send your feedback on the Tech Tips: http://developers.sun.com/contact/feedback.jsp?category=sdn
Subscribe to the following newsletters for the latest information about technologies and products in other Java platforms:
ARCHIVES: You'll find the Core Java Technologies Tech Tips archives at: http://java.sun.com/developer/JDCTechTips/index.html IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies: http://www.sun.com/share/text/termsofuse.html http://www.sun.com/privacy/ http://developer.java.sun.com/berkeley_license.html © 2005 Sun Microsystems, Inc. All Rights Reserved. For information on Sun's trademarks see: http://sun.com/suntrademarks Java, J2EE, J2SE, J2ME, and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Sun Microsystems, Inc. 10 Network Circle, MPK10-209 Menlo Park, CA 94025 US |