Beruflich Dokumente
Kultur Dokumente
T E C H
T I P S
TIPS, TECHNIQUES, AND SAMPLE CODE
of
of
of
of
of
5000
5000
5000
5000
5000
objects
objects
objects
objects
objects
took
took
took
took
took
438
203
234
188
219
ms.
ms.
ms.
ms.
ms.
These results indicate that it was a good idea to run the test
more than once because the first run was so different from the
others. Ignoring the first run, which probably incurred some
one-time startup overhead, the results range from approximately
190-235 ms to write 5000 objects to a file.
The Employee class takes advantage of the simplest flavor of
serialization by implementing the signal interface Serializable;
this indicates to the Java(tm) virtual machine* that you want to
use the default serialization mechanism. Implementing the
Serializable interface allows you to serialize the Employee
objects by passing them to the writeObject() method of
ObjectOutputStream. ObjectOutputStream automates the process of
writing the Employee class metadata and instance fields to the
stream. In other words, it does all the serialization work for you.
Though the work is automated, you might want faster results. How
do you improve the results? The answer is you need to write some
custom code. Begin by declaring that the Employee class implements
Externalizable instead of Serializable. You also need to declare
a public no-argument constructor for the Employee class.
When you declare that an object is Externalizable you assume full
responsibility for writing the object's state to the stream.
ObjectOutputStream no longer automates the process of writing your
class's metadata and instance fields to the stream. Instead, you
manipulate the stream directly using the methods readExternal and
writeExternal. Here is the code you need to add to the Employee
class:
public void readExternal(java.io.ObjectInput s)
throws ClassNotFoundException, IOException
{
lastName = s.readUTF();
firstName = s.readUTF();
ssn = s.readUTF();
salary = s.readInt();
level = s.readInt();
}
public void writeExternal(java.io.ObjectOutput s)
throws IOException
{
s.writeUTF(lastName);
s.writeUTF(firstName);
s.writeUTF(ssn);
s.writeInt(salary);
s.writeInt(level);
}
The ObjectInput and ObjectOutput interfaces extend the DataInput
and DataOutput interfaces, respectively. This gives you the
methods you need to use the stream. Through methods inherited from
DataInput and DataOutput, you can read and write native types using
methods such as readInt() and writeInt(), and read and write string
types using methods such as readUTF() and writeUTF(). (Java uses a
UTF-8 variant to encode Unicode strings, see RFC 2279 and the Java
Virtual Machine Specification for details.)
of
of
of
of
of
5000
5000
5000
5000
5000
objects
objects
objects
objects
objects
took
took
took
took
took
266
125
110
156
109
ms.
ms.
ms.
ms.
ms.
now = System.currentTimeMillis();
}
System.out.println("IgnoringTask completed");
}
}
IgnoringTask uses System.currentTimeMillis() to keep track of
elapsed time, and if an InterruptedException is thrown, it catches
the exception and goes back to finish its work. Because the
InterruptedException is not thrown from the run() method, it is not
declared as a checked exception, and clients do not have to handle
it. Try running IgnoringTask by entering the following on the
command line:
java TaskClient IgnoringTask
If you press Enter to interrupt the thread, you will see this
output:
==========================Shutting down
IgnoringTask ignoring interruption
IgnoringTask completed
IgnoringTask completed
etc.
Notice that "IgnoringTask completed" continues to be printed. As
you can see, an IgnoringTask cannot be interrupted midstream. This
is appropriate in most situations. Unfortunately, an IgnoringTask
also prevents the thread from being interrupted at all. Even after
you try to interrupt the thread, new tasks will continue to run.
You have made your thread permanently uninterruptible, and other
programmers who use your code are not likely to be happy.
What you need is some way to guarantee that tasks already in
progress will finish, but still provide some way to interrupt the
thread. The DeferringTask class provides a solution:
class DeferringTask implements Task {
public void run() {
long now = System.currentTimeMillis();
long end = now + 1000;
boolean wasInterrupted = false;
while (now < end) {
try {
Thread.sleep(end-now);
}
catch (InterruptedException ie) {
System.out.println("DeferringTask deferring interruption");
wasInterrupted = true;
}
now = System.currentTimeMillis();
}
System.out.println("DeferringTask completed");
if (wasInterrupted) {
Thread.currentThread().interrupt();
}
}
}
DeferringTask is almost exactly the same as IgnoringTask, with
| yes
| no
| yes
| no
|
|
| defer
| no*
| no
| yes*
|
|----------------------------------------------------------------|
| * for defer to work correctly, caller must check for
|
| interruption
|
---------------------------------------------------------------For a more in-depth look at interruption, refer to Chapter 9 of
Multithreaded Programming with Java Technology, by Bil Lewis and
Daniel J. Berg (for information about this book, see
http://www.sun.com/books/catalog/lewis3/).
. . . . . . . . . . . . . . . . . . . . . . .
- NOTE
The names on the JDC mailing list are used for internal Sun
Microsystems(tm) purposes only. To remove your name from the list,
see Subscribe/Unsubscribe below.
- FEEDBACK
Comments? Send your feedback on the JDC Tech Tips to:
jdc-webmaster@sun.com
- SUBSCRIBE/UNSUBSCRIBE
The JDC Tech Tips are sent to you because you elected to subscribe
when you registered as a JDC member. To unsubscribe from JDC email,
go to the following address and enter the email address you wish to
remove from the mailing list:
http://developer.java.sun.com/unsubscribe.html
To become a JDC member and subscribe to this newsletter go to:
http://java.sun.com/jdc/
- ARCHIVES
You'll find the JDC Tech Tips archives at:
http://developer.java.sun.com/developer/TechTips/index.html
- COPYRIGHT
Copyright 2000 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.
This document is protected by copyright. For more information, see:
http://developer.java.sun.com/developer/copyright.html
This issue of the JDC Tech Tips is written by Stuart Halloway,
a Java specialist at DevelopMentor (http://www.develop.com/java).
JDC Tech Tips
April 25, 2000
* As used in this document, the terms "Java virtual machine" or "JVM"
mean a virtual machine for the Java platform.