j2se5.0

66
java.sun.com/javaone/sf | 2004 JavaOne SM Conference | Session TS-1952 1 Fast Track to Java™ 2 Platform, Standard Edition (J2SE™) 5.0 Technology Calvin Austin J2SE 5.0 Specification Lead Sun Microsystems, Inc.

Upload: api-3772730

Post on 07-Jun-2015

354 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: j2se5.0

java.sun.com/javaone/sf

| 2004 JavaOneSM Conference | Session TS-1952 1

Fast Track to Java™ 2 Platform, Standard Edition (J2SE™) 5.0 Technology

Calvin Austin J2SE 5.0 Specification LeadSun Microsystems, Inc.

Page 2: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 2

Learn the key features of J2SE™ 5.0 technology in one hour!

Goal of This Presentation

Page 3: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 3

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

Page 4: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 4

Agenda

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

Page 5: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 5

“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

Page 6: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 6

J2SE 5.0 Technology Timeline

FCSQ3 04

Beta12/4

Beta25/27

RC

2004 2005

Page 7: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 7

“Tiger” Component JSRs

� 003 JMX™ API� 013 Decimal Arithmetic� 014 Generic Types� 028 SASL� 114 JDBC™ Rowsets� 133 New Memory Model� 160 JMX Remote API� 163 Profiling API

� 166 Concurrency Utilities� 174 Monitoring and

Management� 175 Metadata� 200 Pack transfer format� 201 Language updates� 204 Unicode surrogates� 206 JAXP 1.3

Page 8: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 8

“Tiger” Themes

� Quality─ Compatibility, compatibility, compatibility!

� Performance and scalability� Ease of development� Monitoring and manageability� Desktop client

Page 9: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 9

“Tiger” Themes

� Quality─ Compatibility, compatibility, compatibility!

� Performance and scalability� Ease of development� Monitoring and manageability� Desktop client

Page 10: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 10

� Far tighter controls on quality� Lots and lots of bug fixing� Compatibility is key to successful migration� Reliability and stability are critical

Quality, Stability, Compatibility

Page 11: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 11

“Tiger” Themes

� Quality─ Compatibility, compatibility, compatibility!

� Performance and scalability� Ease of development� Monitoring and manageability� Desktop Client

Page 12: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 12

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

Page 13: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 13

More Performance!

� Faster startup time─ Especially important for desktop applications

� Class Data Sharing, generated at install time� java -Xshare:off to disablejava -Xshare:dump to re-generate

Page 14: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 14

Even More Performance!

� Performance Ergonomics─ Less reliance on command line flags─ More support for adaptive self-tuning

� Performance tools─ Jstat and jvmstat

jstat -class 6047Loaded Bytes Unloaded Bytes Time173 135.6 0 0.0 0.81l

Page 15: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 15

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

Page 16: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 16

Internal Tests

Startup minimal awt applet minimal Swing app0

10

20

30

40

50

60

70

80

90

100

Reduction in Windows Startup Time

1.4.25.0build47

Page 17: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 17

“Tiger” Themes

� Quality─ Compatibility, compatibility, compatibility!

� Performance and scalability� Ease of development� Monitoring and manageability� Desktop Client

Page 18: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 18

Ease of Development Changes

� MetaData� Generics� Autoboxing� Enhanced for loops� Enumerated Types, Static Import� Covariant Return Types� Formatted Output/Input� Varargs� Concurrency Utilities

Page 19: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 19

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

Page 20: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 20

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

Page 21: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 21

Metadata

� Declared using @interface

� @Retention(RUNTIME) @interface debug {

boolean devbuild() default false;

int counter();}

� Developer specifies metadata using @tag

� public @debug mymethod;

Page 22: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 22

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 Filerclass to create source, class or text files

� PrintWriter pw = env.getFiler().createSourceFile("Ex1");

Page 23: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 23

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;}

}

Afterpublic class HelloImpl {

public @remote String HelloImpl(String s) {return "Hello “+s;

}}

Page 24: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 24

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

Page 25: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 25

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

Page 26: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 26

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();

Page 27: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 27

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

Page 28: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 28

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()).intValue()); }

}

Page 29: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 29

Pre-Generic Result

$ javac MoneyBox.java

$ java MoneyBox

Exception in thread "main" java.lang.ClassCastException: java.lang.String

at MoneyBox.main(MoneyBox.java:18)

Page 30: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 30

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());

}}

Page 31: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 31

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

Page 32: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 32

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);

Page 33: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 33

Enhanced for Loop

� Simplified iterating over collections

BeforeArrayList<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) { ... }

Page 34: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 34

Enumerated Types

� Safer than emulated enumerations with constants

public enum StopLight { red, amber, green }

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

Page 35: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 35

Co-variant Return Types

� 1.4 Versionpublic class MyDialog {

MyDialog getDialog() {return this;

}}

public class MyFileDialog extends MyDialog{public boolean isDirectory() {}

MyDialog getDialog() {return this;

}}

Page 36: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 36

Co-variant Return Types (2)

5.0 Versionclass 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.getDialog().isDirectory());

Page 37: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 37

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

Page 38: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 38

Formatted Input

� Scanner API provides simplified Input functionality

� C versionscanf(“%s”, name);

scanf(“%d”, age);

� Java VersionScanner s=new Scanner(System.in);

String name= s.next();int age=s.nextInt();s.close();

Page 39: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 39

Varargs

� Allows multiple arguments to be passed into methods

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

}

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

Page 40: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 40

Concurrency Utilities

� Concurrency Utility Library (JSR-166)─ Being led by Doug Lea─ Locking APIs─ Powerful high-level thread constructs

� Semaphoresfinal private Semaphore s= new Semaphore(1, true)s.acquireUninterruptibly(); //for non-blocking

//use s.acquire()balance=balance+10; //protected values.release(); //return semaphore token

Page 41: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 41

Concurrency Utilities (2)

� Atomic VariablesAtomicInteger balance=new AtomicInteger(10);System.out.println(balance.getAndAdd(10));

� Thread Executorsclass 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());

}}

Page 42: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 42

“Tiger” Themes

� Quality─ Compatibility, compatibility, compatibility!

� Performance and scalability� Ease of development� Monitoring and manageability� Desktop Client

Page 43: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 43

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

Page 44: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 44

SNMP Monitoring

� SNMP Out-of-the-Boxjava -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-MIBjvmLowMemoryPoolUsageNotifjvmLowMemoryPoolCollectNotif

� Easy to configure─ Set oid jvmMemPoolThreshold

Page 45: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 45

JMX API Monitoring and Management

� JMX Out-of-the-Boxjava -Dcom.sun.management.jmxremote.port=5001-Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -jar Java2Demo.jar

� Low Memory Notificationsjava.management.memory.threshold.exceeded

� Easy to Configurejava.lang.MemoryPool.TenuredGen.UsageThreshold=16000

Page 46: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 46

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

Page 47: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 47

StackTrace API

� Generating stack traces has been awkward if no console availableNew 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]);

}

Page 48: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 48

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 [email protected]"

Page 49: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 49

Serviceability Agent Connectors

� Java Debug Interface SA Connectors ─ Attach connector to core file ─ Attach connector to hung JVM machine─ Used by JDI aware debug toolsjdb -connect

sun.jvm.hotspot.jdi.SACoreAttachingConnector:javaExecutable=/usr/java/jdk1.5.0/bin/java

Page 50: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 50

jstack Example

� pstack Java extensions (jstack)─ Understands Java programming language

methods, also jmap, jinfo$ jstack -m 3248

...

----------------- t@8 -----------------

0xff29f16c ___lwp_cond_wait + 0x40xfe0cac90 int Monitor::wait(int,long) + 0x3ec0xfe1193cc void CompileBroker::compiler_thread_loop() + 0x3540xfe0f8bbc void JavaThread::run() + 0x2600xfe0cb124 _start + 0x1440xff36b74 _thread_start + 0x40

Page 51: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 51

“Tiger” Themes

� Quality─ Compatibility, compatibility, compatibility!

� Performance and scalability� Ease of development� Monitoring and manageability� Desktop Client

Page 52: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 52

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

Page 53: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 53

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

Page 54: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 54

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”

Page 55: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 55

Screenshot of Ocean

Page 56: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 56

Features Outside the Themes

� XML, Web Services� Unicode� JDBC API Disconnected Rowsets� Pack200

Page 57: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 57

Core XML Support

� JSR 206 JAXP 1.3─ XML 1.1 and Namespace─ XML Schema─ XSLT/XSLTC─ SAX 2.0.2─ DOM level 3

Page 58: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 58

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

Page 59: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 59

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

Page 60: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 60

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

Page 61: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 61

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 rowrowSet.updateInt(2,id+1); //Set the fieldrowSet.updateRow(); //mark row as donerowSet.acceptChanges(); //batch up to database

Page 62: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 62

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

Page 63: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 63

Summary

� This was a fast-track overview─There are over 100 features

� Watch out for “Tiger”!─ Performance, ease of development,

RAS and Desktop Client

Page 64: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 64

Q&A

Calvin Austin

Page 65: j2se5.0

| 2004 JavaOneSM Conference | Session TS-1952 65

Further Information

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

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

Page 66: j2se5.0

java.sun.com/javaone/sf

| 2004 JavaOneSM Conference | Session TS-1952 66

Fast Track to Java™ 2 Platform, Standard Edition (J2SE™) 5.0 Technology

Calvin Austin J2SE 5.0 Specification LeadSun Microsystems, Inc.