Sie sind auf Seite 1von 66

Fast Track to Java™

2 Platform, Standard
Edition (J2SE™) java.sun.com/javaone/sf

5.0 Technology

Calvin Austin
J2SE 5.0 Specification Lead
Sun Microsystems, Inc.
1 | 2004 JavaOneSM Conference | Session TS-1952
Goal of This Presentation

Learn the key features of J2SE™ 5.0


technology in one hour!

2 | 2004 JavaOneSM Conference | Session TS-1952


Your Next Java™ Platform

“I think I’m becoming a geek, because for


the first time in five years I’m actually
excited about some technology.”

Beta1 Feedback

3 | 2004 JavaOneSM Conference | Session TS-1952


Agenda

• Introduction to “Tiger”
• Main Theme Features
• Miscellaneous Features
• Demo
• Q&A

4 | 2004 JavaOneSM Conference | Session TS-1952


“Tiger” J2SE 5.0 Technology

• Developed using the Java Community


ProcessSM services
• 15 component JSRs for major new features
• Over 160 Experts Worldwide
• Over 100 Features
• Lots of bug fixing and performance work

5 | 2004 JavaOneSM Conference | Session TS-1952


J2SE 5.0 Technology Timeline

2004 2005

Beta1 Beta2 RC FCS


2/4 5/27 Q3 04

6 | 2004 JavaOneSM Conference | Session TS-1952


“Tiger” Component JSRs

• 003 JMX™ API • 166 Concurrency Utilities


• 013 Decimal Arithmetic • 174 Monitoring and
Management
• 014 Generic Types
• 175 Metadata
• 028 SASL
• 200 Pack transfer format
• 114 JDBC™ Rowsets
• 201 Language updates
• 133 New Memory Model
• 204 Unicode surrogates
• 160 JMX Remote API
• 206 JAXP 1.3
• 163 Profiling API

7 | 2004 JavaOneSM Conference | Session TS-1952


“Tiger” Themes

• Quality
─ Compatibility, compatibility, compatibility!
• Performance and scalability
• Ease of development
• Monitoring and manageability
• Desktop client

8 | 2004 JavaOneSM Conference | Session TS-1952


“Tiger” Themes

• Quality
─ Compatibility, compatibility, compatibility!
• Performance and scalability
• Ease of development
• Monitoring and manageability
• Desktop client

9 | 2004 JavaOneSM Conference | Session TS-1952


Quality, Stability, Compatibility

• Far tighter controls on quality


• Lots and lots of bug fixing
• Compatibility is key to successful migration
• Reliability and stability are critical

10 | 2004 JavaOneSM Conference | Session TS-1952


“Tiger” Themes

• Quality
─ Compatibility, compatibility, compatibility!
• Performance and scalability
• Ease of development
• Monitoring and manageability
• Desktop Client

11 | 2004 JavaOneSM Conference | Session TS-1952


Focus on Performance

• Even more support for big heaps


─ Improved concurrent/parallel collectors

• Yet more Java HotSpot™ VM tuning


─ Chipset optimizations
─ AMD64 native compiler
─ Hyper-threading/CMT

12 | 2004 JavaOneSM Conference | Session TS-1952


More Performance!

• Faster startup time


─ Especially important for desktop applications

• Class Data Sharing, generated at install time


• java -Xshare:off to disable
java -Xshare:dump to re-generate

13 | 2004 JavaOneSM Conference | Session TS-1952


Even More Performance!

• Performance Ergonomics
─ Less reliance on command line flags
─ More support for adaptive self-tuning
• Performance tools
─ Jstat and jvmstat

jstat -class 6047


Loaded Bytes Unloaded Bytes Time
173 135.6 0 0.0 0.81l

14 | 2004 JavaOneSM Conference | Session TS-1952


Performance Ergonomics

• Server class performance for Server class


machines out of the box
─ 2 CPU and 2 GB RAM
─ Parallel Garbage Collector
─ Server Java Hotspot VM compiler
─ Max Heap ¼ Max Memory or 1GB
─ Initial Heap 1/64 Max Memory or Max Heap

15 | 2004 JavaOneSM Conference | Session TS-1952


Internal Tests

Reduction in Windows Startup Time


100

90

80

70

60
1.4.2
50 5.0build47

40

30

20

10

0
Startup minimal awt applet minimal Swing app

16 | 2004 JavaOneSM Conference | Session TS-1952


“Tiger” Themes

• Quality
─ Compatibility, compatibility, compatibility!
• Performance and scalability
• Ease of development
• Monitoring and manageability
• Desktop Client

17 | 2004 JavaOneSM Conference | Session TS-1952


Ease of Development Changes

• MetaData
• Generics
• Autoboxing
• Enhanced for loops
• Enumerated Types, Static Import
• Covariant Return Types
• Formatted Output/Input
• Varargs
• Concurrency Utilities
18 | 2004 JavaOneSM Conference | Session TS-1952
Compiler Options

• Default javac compiler wants 1.5


compatible source
• Only use -source 1.4 for backward
source compatibility
• Only use -source 1.4 -target 1.4 for
corner cases

19 | 2004 JavaOneSM Conference | Session TS-1952


Metadata

• Key new Java programming language idea


• Allows decoration of
packages/classes/methods/fields
for simpler declarative programming
• Can be retained in class file and also be
visible at runtime
• Designed for use by tools, apt tool available
in beta2

20 | 2004 JavaOneSM Conference | Session TS-1952


Metadata

• Declared using @interface

• @Retention(RUNTIME) @interface
debug {
boolean devbuild() default
false;
int counter();
}
• Developer specifies metadata using @tag
• public @debug mymethod;

21 | 2004 JavaOneSM Conference | Session TS-1952


APT Tool

• Front end to Java language Compiler


• Parses metadata with AnnotationProcessorFactory
• AnnotationProcessorFactory selects an
AnnotationProcessor
• AnnotationProcessor has a process()method
to handle metadata tags
• process() method has access to a Filer
class to create source, class or text files
• PrintWriter pw =
env.getFiler().createSourceFile("Ex1");

22 | 2004 JavaOneSM Conference | Session TS-1952


Java Web Services
Tutorial Example: JAX-RPC Server

Before
public interface HelloIF extends Remote {
public String sayHello(String s) throws
RemoteException;
}
public class HelloImpl implements HelloIF {
public String sayHello(String s) {
return “Hello “+s;
}
}

After
public class HelloImpl {
public @remote String HelloImpl(String s) {
return "Hello “+s;
}
}
23 | 2004 JavaOneSM Conference | Session TS-1952
Generics

• What happens today?


─ When creating shared algorithms that work on
different types, designers have to use the general
Object type, e.g., Collections

• What is the problem with that?


─ Developers have to explicitly cast down to
retrieve the object they need. This negates
type safety checking

24 | 2004 JavaOneSM Conference | Session TS-1952


Generics Solution

• The solution
• Allow the developer to provide a type
parameter at declaration time inside
<..>, e.g., <Integer>
• The compiler now can check for type safety
• Collections have been updated to use
Generics

25 | 2004 JavaOneSM Conference | Session TS-1952


Generics Example

Before

ArrayList list = new ArrayList();


list.add(0, new Integer(42));
int total = ((Integer)list.get(0)).intValue();

After

ArrayList<Integer> list = new


ArrayList<Integer>();list.add(0, new
Integer(42));
int total = list.get(0).intValue();

26 | 2004 JavaOneSM Conference | Session TS-1952


Writing Generics APIs

• Warning: generifying old APIs needs


careful thought
• Look at Collections API source for examples
• For portability, old Collections API is
compatible with Generic Collections API

27 | 2004 JavaOneSM Conference | Session TS-1952


Pre-Generic API

public class MoneyBox {


private Vector pot =new Vector();
public void addCoin(Object o) {
pot.add(o);
}
public Object getMoney() {
return pot.firstElement();
}
public static void main(String[] args) {
MoneyBox m= new MoneyBox();
m.addCoin("iou");
m.addCoin(new Integer(3));
System.out.println(((Integer)m.getMoney()).intVal
ue());
}
}

28 | 2004 JavaOneSM Conference | Session TS-1952


Pre-Generic Result

$ javac MoneyBox.java
$ java MoneyBox
Exception in thread "main"
java.lang.ClassCastException:
java.lang.String
at MoneyBox.main(MoneyBox.java:18)

29 | 2004 JavaOneSM Conference | Session TS-1952


Generified Version

public class MoneyBox<M> {


Vector<M> pot =new Vector<M>();
public void addCoin(M o) {
pot.add(o);
}
public M getMoney() {
return pot.firstElement();
}
public static void main(String[] args) {
MoneyBox<Integer> m= new
MoneyBox<Integer>();
m.addCoin("iou");
m.addCoin(new Integer(3));
System.out.println(m.getMoney());
}
}

30 | 2004 JavaOneSM Conference | Session TS-1952


Generic Result

$ javac MoneyBox.java
MoneyBox.java:15:
addCoin(java.lang.Integer) in
MoneyBox<java.lang.Integer> cannot be
applied to (java.lang.String)
m.addCoin("iou");
^
1 error

31 | 2004 JavaOneSM Conference | Session TS-1952


Autoboxing of Primitive Types

• Conversion to and from primitive types


to their Object equivalent

Before

list.add(0, new Integer(42));


int total = (list.get(0)).intValue();

After

list.add(0,42);
int total = list.get(0);

32 | 2004 JavaOneSM Conference | Session TS-1952


Enhanced for Loop

• Simplified iterating over collections


Before
ArrayList<Integer> list = new
ArrayList<Integer>();for (Iterator i =
list.iterator();i.hasNext();{
Integer value=(Integer)i.next();
}

After
ArrayList<Integer> list = new ArrayList<Integer>();
for (Integer i : list) { ... }

33 | 2004 JavaOneSM Conference | Session TS-1952


Enumerated Types

• Safer than emulated enumerations


with constants

public enum StopLight { red, amber, green }

StopLight t=StopLight.red;
StopLight t=1;//error
StopLight t=StopLight.blue; //error

34 | 2004 JavaOneSM Conference | Session TS-1952


Co-variant Return Types

• 1.4 Version
public class MyDialog {
MyDialog getDialog() {
return this;
}
}

public class MyFileDialog extends MyDialog{


public boolean isDirectory() {
}
MyDialog getDialog() {
return this;
}
}

35 | 2004 JavaOneSM Conference | Session TS-1952


Co-variant Return Types (2)

5.0 Version
class MyDialog {MyDialog getDialog() {
return this;
}
}
class MyFileDialog extends MyDialog{
public boolean isDirectory() {
}
// was MyDialog getDialog() {
MyFileDialog getDialog() {
return this;
}
}
MyFileDialog f = new
MyFileDialog();System.out.println("fd="+f.getDial
og().isDirectory());

36 | 2004 JavaOneSM Conference | Session TS-1952


Formatted Output

• printf functionality is added to


J2SE 5.0 technology
─ Compatible with C version
─ Helps migrate legacy code

• You can do things like:


out.printf(“name count\n”);
out.printf(“%s %5d\n”, user,total);

• Note: Use %n instead of \n for


platform-independent newline

37 | 2004 JavaOneSM Conference | Session TS-1952


Formatted Input

• Scanner API provides simplified Input


functionality

• C version
scanf(“%s”, name);
scanf(“%d”, age);

• Java Version
Scanner s=new Scanner(System.in);
String name= s.next();
int age=s.nextInt();
s.close();

38 | 2004 JavaOneSM Conference | Session TS-1952


Varargs

• Allows multiple arguments to be passed


into methods

void argtest(String ... args) {


for (int i=0;i <args.length; i++) {
}
}

//
argtest(“test”, “data”);

39 | 2004 JavaOneSM Conference | Session TS-1952


Concurrency Utilities

• Concurrency Utility Library (JSR-166)


─ Being led by Doug Lea
─ Locking APIs
─ Powerful high-level thread constructs

• Semaphores
final private Semaphore s= new Semaphore(1,
true)s.acquireUninterruptibly(); //for non-
blocking
//use s.acquire()
balance=balance+10; //protected value
s.release(); //return semaphore token

40 | 2004 JavaOneSM Conference | Session TS-1952


Concurrency Utilities (2)

• Atomic Variables
AtomicInteger balance=new
AtomicInteger(10);System.out.println(balance.getA
ndAdd(10));
• Thread Executors
class Worker implements Runnable {
public void run() {
System.out.println("New Worker");
}
}
public class ConThread {
public static void main(String args[]) {
Executor e =
Executors.newFixedThreadPool(3);
e.execute(new Worker());
}
}
41 | 2004 JavaOneSM Conference | Session TS-1952
“Tiger” Themes

• Quality
─ Compatibility, compatibility, compatibility!
• Performance and scalability
• Ease of development
• Monitoring and manageability
• Desktop Client

42 | 2004 JavaOneSM Conference | Session TS-1952


Monitoring and Manageability

• JVM™ Machine Monitoring and Management


API (JSR-174)
─ Low memory detection
─ Allow access to internal VM status
─ Heap sizes, gc info, threads, etc.
─ Industry standard SNMP and JMX based
─ Plugs into existing management consoles
• JMX API (JSR-003)
─ MBeanServer part of JRE
─ Support for remote management JSR-160
─ Works with existing J2EE™ application servers

43 | 2004 JavaOneSM Conference | Session TS-1952


SNMP Monitoring

• SNMP Out-of-the-Box
java -Dcom.sun.management.snmp.port=161
-Dcom.sun.management.snmp.trap=162
-Dcom.sun.management.snmp.acl=false -jar
Java2Demo.jar

• Low Memory Traps from JVM-MANAGEMENT-MIB


jvmLowMemoryPoolUsageNotif
jvmLowMemoryPoolCollectNotif
• Easy to configure
─ Set oid jvmMemPoolThreshold

44 | 2004 JavaOneSM Conference | Session TS-1952


JMX API Monitoring and Management

• JMX Out-of-the-Box
java -Dcom.sun.management.jmxremote.port=5001
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false -jar
Java2Demo.jar

• Low Memory Notifications


java.management.memory.threshold.exceeded

• Easy to Configure
java.lang.MemoryPool.TenuredGen.UsageThreshold=16000

45 | 2004 JavaOneSM Conference | Session TS-1952


Profiling and Diagnostics

• New Profiling API (JSR-163)


─ Allows improved performance analysis
─ New jvmti native api replaces jvmpi
─ Includes bytecode instrumentation
─ hprof updated to use jvmti
• Improved diagnostic ability
─ Stack trace API
─ Fatal Error handling
─ Serviceability agent
─ JVM internal tools, jstack, jinfo

46 | 2004 JavaOneSM Conference | Session TS-1952


StackTrace API

• Generating stack traces has been awkward


if no console available
New Stack Trace APIs address this

System.out.println(Thread.getAllStackTraces());
StackTraceElement
e[]=Thread.currentThread().getStackTrace();
for (int i=0; i<e.length; i++) {
System.out.println(e[i]);
}

47 | 2004 JavaOneSM Conference | Session TS-1952


Fatal Error Handler

• Start JVM machine with OnError option


-XX:OnError=”command”
optional %p used as process id

-XX:OnError="pmap %p"
-XX:OnError="gcore %p; dbx - %p"
-XX:OnError=”gdb %p”
-XX:OnError="cat hs_err_pid%p.log | mailx -s help
calvin.austin@sun.com"

48 | 2004 JavaOneSM Conference | Session TS-1952


Serviceability Agent Connectors

• Java Debug Interface SA Connectors


─ Attach connector to core file
─ Attach connector to hung JVM machine
─ Used by JDI aware debug tools
jdb -connect
sun.jvm.hotspot.jdi.SACoreAttachingConnector:
javaExecutable=/usr/java/jdk1.5.0/bin/java

49 | 2004 JavaOneSM Conference | Session TS-1952


jstack Example

• pstack Java extensions (jstack)


─ Understands Java programming language
methods, also jmap, jinfo
$ jstack -m 3248
...
----------------- t@8 -----------------
0xff29f16c ___lwp_cond_wait + 0x4
0xfe0cac90 int Monitor::wait(int,long) + 0x3ec
0xfe1193cc void
CompileBroker::compiler_thread_loop() + 0x354
0xfe0f8bbc void JavaThread::run() + 0x260
0xfe0cb124 _start + 0x144
0xff36b74 _thread_start + 0x40

50 | 2004 JavaOneSM Conference | Session TS-1952


“Tiger” Themes

• Quality
─ Compatibility, compatibility, compatibility!
• Performance and scalability
• Ease of development
• Monitoring and manageability
• Desktop Client

51 | 2004 JavaOneSM Conference | Session TS-1952


Desktop Client

• Continued deployment improvements


─ Unified Java Plug-in and Java™ Web Start console

• Continued quality and performance work


─ Startup and footprint work
─ Direct support for OpenGL®
─ XAWT

52 | 2004 JavaOneSM Conference | Session TS-1952


OpenGL and X11 Toolkit

• OpenGL acceleration for JFC/Swing


applications enabled using:
• java -Dsun.java2d.opengl=true -jar
Java2D.jar

• Optional fast X11 toolkit called XAWT, enabled


on Linux, option on SolarisTM Operating
System
• java -
Dawt.toolkit=sun.awt.X11.XToolkit -
jar Notepad.jar

53 | 2004 JavaOneSM Conference | Session TS-1952


Desktop Client 3

• Improved GUI look-and-feel support


─ Improved Gnome skins support
─ Synth skinnable API
─ Improved Windows XP look and feel
─ Fresh new cross-platform Java technology
look and feel—“Ocean”

54 | 2004 JavaOneSM Conference | Session TS-1952


Screenshot of Ocean

55 | 2004 JavaOneSM Conference | Session TS-1952


Features Outside the Themes

• XML, Web Services


• Unicode
• JDBC API Disconnected Rowsets
• Pack200

56 | 2004 JavaOneSM Conference | Session TS-1952


Core XML Support

• JSR 206 JAXP 1.3


─ XML 1.1 and Namespace
─ XML Schema
─ XSLT/XSLTC
─ SAX 2.0.2
─ DOM level 3

57 | 2004 JavaOneSM Conference | Session TS-1952


Java Web Services Developer Pack

• Commitment to latest Web Services standards


• JAX-RPC and SAAJ (WSDL/SOAP)
• JAXB
• XML Security
─ Encryption and Digital Signature
• JAXR

58 | 2004 JavaOneSM Conference | Session TS-1952


Unicode Supplementary Characters

• Unicode 3.1 adds extra characters


─ Some characters don’t fit in Java “char” 16 bits

• Extended characters are pairs of values


─ Encoded in a string
• Character still uses 16 bits, includes
supplementary int methods
• J2SE 5.0 technology supports Unicode 4.2

59 | 2004 JavaOneSM Conference | Session TS-1952


JDBC Disconnected RowSets

• CachedRowSet
─ Contains in-memory collection of rows
─ Allows disconnected use of data
─ Updates can be resynchronized into database

• WebRowSet
─ Can also import and export XML representation

60 | 2004 JavaOneSM Conference | Session TS-1952


JDBC Rowset Example

CachedRowSetImpl rowSet = new CachedRowSetImpl();


rowSet.setUsername("postgres");
rowSet.setPassword("duke");
rowSet.setUrl("jdbc:postgresql:test");
rowSet.setCommand("SELECT * FROM users");
rowSet.execute();
...
rowSet.first(); //Select the first row
rowSet.updateInt(2,id+1); //Set the field
rowSet.updateRow(); //mark row as done
rowSet.acceptChanges(); //batch up to database

61 | 2004 JavaOneSM Conference | Session TS-1952


Pack200

• JAR File Compression API and Tool


• javax.pack.Pack200 classpack200,
unpack200 command line tools

• pack200 -v charsets.pack.gz
charsets.jar

• unpack200 -v charsets.pack.gz
charsets.jar

62 | 2004 JavaOneSM Conference | Session TS-1952


Summary

• This was a fast-track overview


─ There are over 100 features

• Watch out for “Tiger”!


─ Performance, ease of development,
RAS and Desktop Client

63 | 2004 JavaOneSM Conference | Session TS-1952


Q&A

Calvin Austin

64 | 2004 JavaOneSM Conference | Session TS-1952


Further Information

• http://java.sun.com/j2se/1.5.0

• http://jcp.org/en/jsr/detail?id=176

65 | 2004 JavaOneSM Conference | Session TS-1952


Fast Track to Java™
2 Platform, Standard
Edition (J2SE™) java.sun.com/javaone/sf

5.0 Technology

Calvin Austin
J2SE 5.0 Specification Lead
Sun Microsystems, Inc.
66 | 2004 JavaOneSM Conference | Session TS-1952