Introduction to Real-Time Java


I wish I have more time to spend on this. But I am exhausted between my work,  my 3 months old child and this free time project.

I am not an expert on real-time Java. If you send your corrections to denizoguz@denizoguz.com, I will apply them to this presentation and upload it again.

Thanks.

You can download presentation in following formats.

Introduction to Real Time Java — Presentation Transcript

  1. Real Time Java Deniz Oğuz Initial:22.06.2008 Update:05.12.2009
    • Downside of Using C and Java Together
    • Aim of This Course
  2. Aim of This Course Gaining introductory level knowledge about Real-Time Java and evaluate its suitability to your next project.
  3. Outline
    • What is Real-time?
    • What are the problems of Java SE?
    • Real-Time Linux
    • Features of RTSJ (Real Time Specification for Java), JSR 1, JSR 282
    • IBM Websphere Real-Time
    • SUN RTS
  4. What is Real-time?
    • Real-time does not mean fast
      • Throughput and latency are important but does not enough
    • Real-time is about determinism
      • Deadlines must be met
    • Real-time is not just for embedded systems
      • Financial applications
      • Telecommunication applications
      • Network centric systems etc…
  5. Categories of Application Predictability Hard Real-Time None Real-Time Soft Real-Time No time-based deadlines Ex:Batch processing, web services Deadlines may be missed occasionally Ex:Router, automated trading systems Deadlines can not be missed Ex:fly-by-wire system, anti-lock brake system, motion control
  6. Hard/Soft real-time
    • Hard real-time
      • A late answer has no value
    • Soft real-time
      • late answer has still a value but value of answer rapidly degrees
  7. Software Complexity vs. Time Taken From IBM WebSphere Real Time: Providing predictable performance by Michael S. Fulton, Java chief architect;Darren V. Hart, Real Time team lead; andGregory A. Porpora, IBM Software Group. December 2006
  8. Latency and Jitter
    • Latency is the time between external event and system’s response to that event
    • Jitter is the variation in latency
  9. Hardware Architecture & OS
    • Modern processors and operating systems are optimized for throughput
      • It makes excellent sense for most systems to trade a rare factor of 100 times slowdown for a performance doubling everywhere else
  10. Hardware & OS Cont.
    • Worst-case scenario for an instruction
      • Instruction is not in the cache, processor must read it from memory
      • An address translation cache miss requires more memory access
      • Instruction might be in demand paged memory
      • Data may not be in cache
      • A large DMA may delay operations
      • SMI on X86 platforms
      • …….
  11. Worst Case Execution Cont. This example is taken from Real-Time Java Platform Programming by Peter C. Dibble 100101660 Total 10000000 One Big DMA 20000000 Demand paging for instruction read (write dirty page) 500 Dirty data cache write ATC miss 500 Instruction ATC miss 10 Execute Instruction 100000 Interrupts 10000000 Demand paging for data cache read (read page) 10000000 Demand paing for data cache write (read page) 20000000 Demand paging for data cache write (write dirty page) 500 Data cache read ATC miss 50 Instruction cache miss 20000000 Demand paging for data cache read (write page) 10000000 Demand paging for dirty data cache write (read page) 100 Data cache miss Estimate Time (ns) Event
  12. How to prevent worst case
    • Disable paging for time critical code
    • Use processor affinity and pin RT Threads to cpus
    • Use tunable DMA
    • Use large pages to reduce load on TLB
    • Disable interrupts or give RT Threads higher priority than some interrupts
    • On x86 architecture pay attention to SMI (System Management Interrupts)
      • Do not disable it completely , you may burn down your processor.
  13. Processor Pinning
    • Less context switching jitter
    • Decreased cache miss
    • Example (Linux and Sun RTS only):
      • -XX:RTSJBindRTTToProcessors=0,1
      • -XX:RTSJBindNHRTToProcessors=0,1
      • Alternatively you can create a cpu set named xx and use /dev/cpuset/xx
  14. Large Memory Pages
    • Garbage collectors performs very bad if memory is swaped to disk
    • For increased determinism use large memory pages and pin these pages to memory
    • Example Linux and Sun’s Java SE only:
      • echo shared_memory_in_bytes > /proc/sys/kernel/shmmax
      • echo number_of_large_pages > /proc/sys/vm/nr_hugepages
      • Start JVM using XX:+UseLargePages argument
      • Verify using cat /proc/meminfo | grep Huge

    Refer following Sun article for large memory pages: Java Support for Large Memory Pages

  15. RT-POSIX
    • An extension to POSIX standard to address hard and soft real-time systems (POSIX 1003.1b)
      • Priority Inversion Control and Priority Inheritance
      • New schedulers
      • Asynchronous IO
      • Periodic, Aperiodic and Sporadic Threads
      • High Resolution Timers
      • RT File System
      • Some others ….
    • For further information refer to RT Posix standard
  16. RT Linux
    • Fully preemptible kernel
    • Threaded interrupt handlers for reduced latency
    • High-resolution timers
    • Priority inheritance
    • Robust mutexes and rt-mutexes
    • To install use
    • sudo apt-get install linux-rt
    • in ubuntu. Select rt kernel at next system start-up
  17. Why Java?
    • Software (including embedded software) becomes more complex and gets unmanageable with old practices and tools
      • Ex:Financial systems, Network Centric systems
    • Single language, tools for real-time and non-real time parts
    • Java Platform provides a more productive environment
    • A large set of 3 rd party libraries are available
    • Has a very big community
      • Large number of developers
      • Support from big companies like IBM, SUN, Oracle
    • A lot safer when compared to low level languages
  18. Downside of Using C and Java Together
    • Same functionality is codded twice
      • for example coordinate conversion, distance calculations, R-tree etc. are implemented in C++ because they can not reuse PiriMap
    • High amount of integration problems
      • For example work package is divided between two people
    • Interfaces between C and Java is ugly and introduce overhead
      • For example DDS transfer Struct but large amount of code is written to convert these structs to Java classes and interfaces accustomed by java developers
    • Communication via JNI can violate safe features of Java
    • The JNI interface is inefficient
    • Increased maintenance cost in the feature due to above problems
  19. What are the problems of Java SE?
    • Dynamic Class Loading and Linking
    • JIT (Just in Time Compiler)
    • Thread Handling
    • Garbage Collector
    • No Raw Memory Access
    • No support for real-time operations, like deadline miss handling, periodic scheduling, processor pinning etc.
  20. Dynamic Class Loading
    • A Java-conformant JVM must delay loading a class until it’s first referenced by a program
      • Early loading is not allowed so JVM can not do this for you at application startup
    • This introduce unpredictable latency from a few microseconds to milliseconds.
      • Depends on from where classes are loaded
      • Static initialization
      • Number of classes loaded
  21. JIT (Just In Time Compiler)
    • Most modern JVMs initially interpret Java methods and, and later compile to native code.
    • For a hard RT application, the inability to predict when the compilation will occur introduces too much nondeterminism to make it possible to plan the application’s activities effectively.
  22. Garbage Collection
    • Pros
      • Pointer safety,
      • leak avoidance,
      • Fast memory allocation:faster than malloc and comparable to alloca
      • Possible de-fragmentation
    • Cons
      • Unpredictable pauses:Depends on size of the heap, number of live objects on the heap and garbage collection algorithm , number of cpus and their speed
  23. Main Garbage Collection Features
    • Stop-the-world or Concurrent
    • Moving objects
    • Generational
    • Parallel
  24. Garbage Collection in HotSpot
    • Serial Collector
      • -XX:+UseSerialGC
    • Parallel-scavenging
      • -XX:+UseParallelGC
    • Parallel compacting
      • -XX:+UseParallelOldGC
    • Concurrent Mark and Sweep (CMS)
      • -XX:+UseConcMarkSweepGC
  25. Garbage First Collector (G1)
    • Planned for JDK 7
    • Low pause and high throughput soft real-time collector
    • It is parallel and concurrent
    • Performs compaction
    • Devides heap to regions and further devides them to 512 bytes cards
    • It is not a hard real-time collector
  26. Thread Management
    • Although standard Java allows priority assignment to threads, does not require low priority threads to be scheduled before high priority ones
    • Asynchronously interrupted exceptions
      • Similar to Thread.stop but this version is safe
    • Priority Inversion may occur in standard Java/Operating systems
      • RTSJ requires Priority Inheritance
  27. What is RTSJ?
    • Designed to support both hard real-time and soft real-time applications
    • Spec is submitted jointly by IBM and Sun
    • Spec is first approved in 2002 (JSR 1)
    • Minor updates started on 2005 (JSR 282)
    • First requirements are developed by The National Institute of Standards and Technology (NIST) real-time Java requirements group
  28. Expert Group of RTSJ
  29. RTSJ’s Problem Domain
    • If price of processor or memory is a small part of whole system RTSJ is a good choice
      • If processor and memory price is very significant when compared with the rest of the system RTSJ may not be a good choice because you will need slightly more powerful processor and larger memory
      • For very low footprint applications with very limited resources you may consider a non RTSJ compliant virtual machine like Aonix Perc Pico or you may even consider a hardware JVM.
  30. Implementations
    • IBM Webshere Real Time (RTSJ Compliant)
      • Works on real-time linux kernel
      • There is a soft real-time version
    • SUN RTS (RTSJ Compliant)
      • Works on Solaris x86/Sparc or real-time linux kernel
    • Aonix Perc (Not RTSJ Compliant but close)
      • Perc-Ultra works on nearly all platforms
      • They have a safety critical JVM (Perc-Raven)
    • Apogee
      • Woks on nearly all platforms
  31. Main Features of RT Java Implementations
    • Full Java SE compatibility and Java syntax
    • A way to write programs that do not need garbage collection (New API)
    • High resolution timer (New API)
    • Thread priorities and locking (New API)
    • Asynchronous Event Handers (New API)
    • Direct memory access (New API)
    • Asynchronous Transfer of Control (New API)
    • AOT (A Head of Time) compilation (Not in RTSJ specification)
    • Garbage collection (Not in RTSJ specification)
  32. Real-Time Garbage Collectors
    • Time based (Metronome of Websphere RT)
    • Work based
    • Henriksson’s GC (Garbage Collector of Sun’s Java RTS is based on this)
  33. Sun’s RT Garbage Collector
    • 3 modes of execution
    • Non generational, concurrent and parallel collector
    • In normal mode works with priority higher than non-rt threads but lower than any rt thread.
    • When free memory goes below a certain threshold priority is increased to boosted priority but RT Threads (including the one with priority lower than GC) still continues to work concurently
    • When free memory goes below a critical threshold.All threads whose priority is lower than GC boosted priority are suspended
    • Most of its working can be tuned using command line switches like: NormalMinFreeBytes, RTGCNormalWorkers, RTGCBoostedPriority, BoostedMinFreeBytes etc.
  34. AOT, ITC and JIT
    • Nearly all Java SE vendors use JIT (Just in time compiler) due to performance reasons
    • Websphere RT uses AOT (Ahead of Time Compilation)
    • SUN’s Java RTS uses ITC (Initialization Time Compilation)

    Refer following IBM Article for further information: Real-time Java, Part 2: Comparing compilation techniques

  35. High-Resolution Timer
    • A more powerful notion of time
      • AbsoluteTime
      • RelativeTime
    • 84 bits value
      • 64 bits milliseconds and 32 bits nanoseconds
      • Range is 292 million years
    • Every RTSJ implementation should have provide at least one high resolution clock accessible with Clock.getRealTimeClock()
    • Resolution is system dependent
      • For example it is 200ns for SunFire v240 (Ref:Real-Time Java Programming by Greg Bollella)
      • Do not expect too much from a laptop
  36. HighResolutionTime Base Class
    • Base class for other 3 time classes.
    • Define the interface and provides implementation of some methods
    • This class and its subclass do not provide any synchronization
  37. AbsoluteTime and RelativeTime
    • AbsoluteTime represents a specific point in time
      • The string 03.12.2008 11:00:00.000 AM represents an absolute time
      • Relative to 00:00:00.000 01.01.1970 GMT
    • RelativeTime represents a time interval
      • Usually used to specify a period for a schedulable object
      • Can be positive, negative or zero
  38. Priority Scheduler
    • Normal scheduling algorithms try to be fair for task scheduling.
      • Even lower priority tasks are scheduled some times
    • There are different real-time and non real-time schedulers
      • Earliest-deadline first, least slack time, latest release time etc.
    • Fixed priority preemptive scheduler is most used real-time scheduler
  39. Thread Scheduling in RTSJ
    • Does not specify a scheduling implementation
    • Allow different schedulers to be plugged
    • Required base scheduler should be a priority scheduler with at least 28 unique priorities
    • Most implementations provide more than 28 priorities
    • More on this with Scheduling Parameters Slide
  40. What is Priority Inversion ?
    • Priority Inversion is the situation where a higher priority task waits for a low priority task.
    • There are 2 generally accepted solution
      • Priority Inheritance (required by RTSJ)
      • Priority Ceiling Emulation (optional for RTSJ)
  41. What is Priority Inversion? cont. Time Legend Lock request  Waiting Task T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 Med priority executing task High priority executing task Shared Resource         Low priority executing task
  42. Schedulable Interface
  43. Scheduling Parameters
  44. Different Task Types
    • Periodic
    • Aperiodic Tasks
    • Sporadic
  45. Release Parameters
  46. Sample Periodic Task public class HelloWorld extends RealtimeThread {        public static long[] times = (long[]) ImmortalMemory.instance().newArray(long.class, 100);           public HelloWorld(PeriodicParameters pp) {            super(null, pp);        }           public void run() {            for (int i = 0; i < 100; i++) {                times[i] = System.currentTimeMillis();                waitForNextPeriod(); //wait for next period            }        }           public static void main(String[] argv) {             //schedule real time thread at every 100 milisecond            PeriodicParameters pp = new PeriodicParameters(new RelativeTime(100, 0));            HelloWorld rtt = new HelloWorld(pp);            rtt.start();               //wait real time thread to terminate            try {                rtt.join();            } catch (InterruptedException ex) {                ex.printStackTrace();            }               //print results            for (int i = 0; i < 100; i++) {                System.out.println(times[i]);            }        }   }
  47. Periodic Execution vs Thread.sleep Real-time systems do not use a loop with a sleep in it to drive periodic executions While (true) { performPeriodicTask() Thread.sleep(period) } WRONG
  48. Deadline Miss!
    • For some applications a deadline should not be missed for any reason
    • For most applications dealine miss is bad, but it is not a total failure and a recovery is possible
    • In RTSJ there are 2 ways to detect a deadline miss
      • waitForNextPeriod() returns false immediately
      • Use a deadline miss handler which is an instance of AEH
  49. NoHeapRealTime Thread
    • Can not access heap
      • Can preempt GC immediately
      • If this rule is violated, MemoryAccessError is thrown
    • Can use ScopedMemory or ImmortalMemory
    • Should be created and started in a scoped or immortal memory
      • They can not be created from normal Threads
  50. WaitFreeWriteQueue
    • Intendent for exchanging data between real-time and non real-time part
    • Real-time producer does not block when queueing data
    • Multiple non-real time consumers may dequeue data
  51. WaitFreeReadQueue
    • Intendent for exchanging data between real-time and non real-time part
    • Real-time consumer does not block when read ing data
    • Multiple non-real time producers may queue data
  52. Memory Regions
    • Heap Memory
      • Same as in Java SE
      • Can be accessed with javax.realtime.HeapMemory
    • Scoped Memory
      • Created and sized at development time
      • Can be accessed with javax.realtime.ScopedMemory
      • Can not be garbage collected; reference count to ScopedMemory object is used. Finalize method of objects are called
      • Can be stacked
    • Immortal Memory
      • Can be accessed with javax.realtime.ImmortalMemory
      • Only one instance exist and size is determined at development time.
      • All static data and allocations performed from static initializers are allocated in Immortal memory. Interned Strings are also allocated in immortal memory
    • Physical Memory
      • There are LTPhysicalMemory, VTPhysicalMemory, and ImmortalPhysicalMemory
  53. Memory Regions
  54. Raw Memory Access
    • Models a range of physical memory as a fixed sequence of bytes
    • Allows device drivers to be writen in java
    • All raw memory access is treated as volatile, and serialized
  55. Raw Memory Access Cont private final long DEVICE_BASE_ADDRESS = xxxxxx ; private final long CTRLREG = 0; private final long STAT REG = 4 ; ……… p ublic void init() { RawMemoryAccess device = new RawMemoryAccess(type, DEVICE_BASE_ADDRESS); device.setInt(CTRL_REG, MY_COMMAND); //send command to device while (device.getInt(STATREG) != 0); //wait for device to response }
  56. Async Events
    • Many of the processing in RT systems are triggered by internal or external events
    • You don’t need to manage threads
    • Hundreds of AEHs may share a pool of threads
    • BoundAsyncEventHandler has always bounded to a dedicated thread
  57. Handling Posix Events
    • Use POSIXSignalHandler
      • Ex :

    ….. class SigintHander extends AsynchEventHandler { public SigintHandler() { //set it to highest priority setSchedulingParameters(new PriorityParameters(RTSJ_MAX_PRI); } public void handleAsynchEvent() { //handle user specified signal } } …… //add handler to posix predefined posix signal POSIXSignalHandler.addHandler(PosixSignalHandler.SIGUSR1, sigintHandler) Use kill -s SIGUSR1 PID to test

  58. Time Triggered Events
    • OneShotTimer : execute handleAsyncEvent method once at the specified time
    • PeriodicTimer : execute handleAsyncEvent method repeatedly at specified interval. A periodicTimer and a AEH combination is roughly equivalent to Periodic Threads.
    • Enable/Disable Timer : A disabled timer is still kicking. But it does not generate events. When enabled again, it continues like never disabled.
  59. Javolution Library (http://javolution.org)
    • High performance and time deterministic (util/lang/text/io/xml)
    • Struct and Union base classes for direct interfacing with native applications
    • NHRT Safe
    • Pure Java and less than 300Kbytes
    • BSD License
  60. IBM WebSphere Real Time
    • Runs on linux with real time patches applied to linux kernel on x86 platforms
    • Has AOT and JIT Compilers
    • Shared classes support
    • Contains Metronome: a real-time garbage collector
    • Full RTSJ 1.0.2 and Java SE 6.0 support
    • A well defined list of NHRT safe classes
  61. Metronome Garbage Collector
    • Uses a time based method for scheduling
    • Applications threads are given a minimum percentage of time (utilization)
      • User supplied at startup
    • Uses a new two-level object model for arrays called arraylets
  62. Metronome Garbage Collector Sample Execution
    • GC Pause Time Histogram
    • Utilization Graph
  63. Websphere AOT
  64. SUN RTS
    • Achieves maximum latencies of 15 microseconds, with around 5 microseconds of jitter.
    • Runs on real-time linux and Solaris 10
    • Has a real-time garbage collector
  65. IBM WebSphere Real Time
    • Runs on linux with real time patches applied to linux kernel
    • Has AOT and JIT Compilers
    • Contains Metronome real-time garbage collector
    • RTSJ and Java SE 5.0 Compliant
  66. Real World Projects:J-UCAS X-45C
  67. Real World Projects:FELIN
  68. Real World Projects:DDG-1000
  69. Real World Projects:ScanEagle
    • This milestone marked the first flight using the RTSJ on an UAV and received the Java 2005 Duke’s Choice Award for innovation in Java technology.
  70. Is RTSJ Required for You?
    • If your application can tolerate some degree of indeterminism use standard JVM and tune it to milliseconds level
    • Only if you fail the first approach use RTSJ.
      • Try to meet your timing constrains with real-time garbage collector (if available) without using advance/complex features like scoped memory
      • If first approach fails make use of NonHeapRealTimeThread, ImmortalMemory, ScopedMemory
  71. A comparison of the features of RTSJ with the increased predictability From IBM WebSphere Real Time Manual
  72. Safety Critical Java (JSR 302)
    • A subset of RTSJ that can be certified DO-178B and ED-128B.
    • Most probably garbage collector will not be available (not needed)
    • Will be targeted to Java ME platform, because Java SE and Java EE are too complex to be certified.
  73. Expert Group of JSR 302
    • Specification Lead: C. Douglass Locke (POSIX 1003.4 Real‑Time Extensions Working Group)
    • Expert Group
  74. Resources
    • Real-Time Java Platform Programming by Peter C. Dibble
    • Real-Time Java Programming with Java RTS by Greg Bollea
    • RTSJ Main Site ( www.rtsj.org )
    • IBM’s Developerworks Articles ( http:// www.ibm.com/developerworks/ )
    • SUN RTS ( http:// java.sun.com/javase/technologies/realtime/reference.jsp )
    • Deniz Oğuz’s blog ( www.denizoguz.com )
  75. Resources Cont. (JavaOne Presentations in 2008)
    • TS-4797 Fully Time-Deterministic Java Technology
      • Explains Javalution Library
    • TS-5767 Real-Time Specification for Java (JSR 1)
      • Explains status of RTSJ. Join presentation from SUN, IBM, Locke Consulting (JSR 302 spec lead)
    • TS-5925 A City-Driving Robotic Car Named Tommy Jr.
      • An autonomous ground vehicle fully powered by Java.
    • TS-5609 Real Time: Understanding the Trade-offs Between Determinism and Throughput
New Features Of JDK 7 — Presentation Transcript
1. Java Reloaded JDK 7 by Deniz Oğuz
2. Objective Learn what is included in JDK 7 and remember to use Google when required
3. JDK Release History Version Release Date JDK 1.0 1996-01-23 JDK 1.1 1997-02-18 JDK 1.1.4 1997-09-12 JDK 1.1.5 1997-12-03 JDK 1.1.6 1998-04-24 JDK 1.1.7 1998-09-28 • Normal release cycle was 2 years, J2SE is 3 JDK 1.1.8 1999-04-08 J2SE 1.2 1998-12-04 years late. It should have bean released at the J2SE 1.2.1 1999-03-30 end of 2008. J2SE 1.2.2 1999-07-08 J2SE 1.3 2000-05-08 • Some of the most wanted features are J2SE 1.3.1 2001-05-17 postponed to J2SE 8. J2SE 1.4.0 2002-02-13 J2SE 1.4.1 2002-09-16 J2SE 1.4.2 2003-06-26 J2SE 5.0 2004-09-29 J2SE 6 2006-12-11 Future Releases J2SE 7 2011-07-28 J2SE 8 Expected in late 2012
4. How to Try JDK 7 Features?1. Download JDK 7 build from jdk7.java.net  (download zipped version and extract to a folder)2. Download Netbeans 7  (download SE version, 80 MB)3. In the IDE, choose Tools > Java Platforms from the main menu4. Click Add Platform and specify the directory that contains the JDK5. Ensure JDK 1.7 is chosen in the Platforms list and click Close6. On Project Properties Libraries section Select JDK 1.7 as Java     Platform7. On Project Properties Sources section Select JDK 7 as Source/Binary Format
5. New Javadoc Format
6. Project Coin String in switch statement Binary literals Underscore in literals Diamond operator Improved exception handling Try with resource Simplified vararg methods invocation
7. String in Switch Statement Before JDK 7 only byte, short, char, int were allowed in switch statements JDK 7 allows Strings to be used in switch statements Why isn’t long supported in switch statements? Console console = System.console(); String day= console.readLine(); switch (day) { case “monday” : console.writer().write(“1”);break; case “tuesday” : console.writer().write(“2”);break; case “wednesday” : console.writer().write(“3”);break; case “thursday” : console.writer().write(“4”);break; case “friday” : console.writer().write(“5”);break; case “saturday” : console.writer().write(“6”);break; case “sunday” : console.writer().write(“7”);break; default:console.writer().write(“?”); } console.flush();
8. Binary Literals It is now easier to specify numbers in binary form  int mask = 0b00000000000000000000000011111111;  int mask = Integer.parseInt(“00000000000000000000000011111111”, 2);  int mask = 255;
9. Underscores in Numbers int money = 100_000_000; long creditCNumber = 3434_3423_4343_4232L; int mask = 0b0000_0000_0000_0000_0000_0000_1111_1111;
10. Diamond Operator Before JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<Integer, Track>(); With JDK 7: Map<Integer, Track> trackStore = new ConcurrentHashMap<>();
11. Multi Catch and Final Rethrow Problem : You want to handle multiple exceptions with the same code block.try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException ex) {     log.warn(“Can not access file”, settingFile); } catch (FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (IOException | FileNotFoundException ex) { log.warn(“Can not access file”, settingFile); } catch (ParseException ex) { log.warn(“{} has incorrect format:{}”, settingFile, ex.getMessage()); }
12. Multi Catch and Final Re-throw Cont. Problem : You want to handle multiple exceptions with the same code block.public Setting readSettings(Strin settingFile) throws ParseException, IOException,FileNotFoundException { try { InputStream inStream = readStream(settingFile); Setting setting = parseFile(inStream); } catch (Throwable ex) { log.warn(“Can not read settings”, settingFile); throw ex; } ……………..}
13. Try With Resourceprivate static String readConfiguration(String file) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } finally { if (reader != null) { try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } }
14. Try With Resource Cont’private static String readConfigurationNew(String file) { try (BufferedReader reader = new BufferedReader(new FileReader(file));) { String line = null; StringBuilder content = new StringBuilder(1000); while ((line = reader.readLine()) != null) { content.append(line); } return content.toString(); } catch (IOException ex){ throw new ConfigurationException(“Can not read configuration file:{}”, file); } }
15. Try With Resource and Autocloseable A new interface AutoCloseable is introduced Existing Closeable interface is changed to extend AutoCloseable interface A new method addSuppressed(Exception) is added to Throwable Exceptions throwed from close method of AutoCloseable are suppressed in favor of exceptions throwed from try-catch block See JavaDoc of Autocloseable for more detail
16. Autocloseable and Suppressed ExceptionExceptions from try-catch body suppresses exceptions thrown from AutoCloseable.close public ABCResource implements AutoCloseable { @Override public void close() throws ABCException { dosomething(); if (errorCondition) { throw new ABCException(“Not supported yet.”); } } } public ClientClass { public void useABCResource() { try (ABCResource resource = new ABCResource();) { dosomething(); if (errorCondition) { throw new ClientException(“Not supported yet.”); } } } }
17. Better Support for Other Languages in JVM  Statically Typed vs Dynamically Typed  Compile time type checking vs runtime type checking  Java, C, Scala are examples of statically typed languages  Java Script, Ruby are examples of dynamically typed languages  Strongly Typed vs Weakly Typed  Automatic type conversion as necessary vs fixed type  Java Script is weakly typed language  Java is a strongly typed languagepublic void printTotal(a, b) { print a + b;}public void printTotal(int a, int b) { print a + b;}
18. Dynamic Typing was Difficult to Implement on JVM• New bytecode is introduced • invokeinterface • invokestatic • invokevirtual • invokespecial • invokedynamic (Only bytecode in JVM that is not used by Java PL)• Execution environment of the programming language provides a bootstrapmethod for resolving method invocations
19. Fork/Join Framework Uses work-stealing algorithm Task is broken into smaller parts recursively A new ExecutorService implementation ForkJoinPool is added ForkJoinTask and its subclasses RecursiveAction and RecursiveTask are added
20. General Usage Pattern of Fork/JoinResult compute(Problem problem) { if (problem is small) directly solve problem else { split problem into independent parts fork new subtasks to solve each part join all subtasks compose result from subresults }else { RecursiveTask left = new ComputationTask(array, low, mid); RecursiveTask right = new ComputationTask(array, mid, high); right.fork(); return left.compute() + right.join();}main { RecursiveTask computationTask = new ComputationTask(array, 0, array.length); ForkJoinPool mainPool = new ForkJoinPool(); Long result = mainPool.invoke(computationTask);}
21. Work Stealing Less thread contention Improved data locality Execute large tasks early
22. TransferQueue Allows producers to wait until message is processed by a consumer even if the queue is not    full. transfer(E e)  Transfers the element to a consumer, waiting if necessary to do so. tryTransfer(E e)  Transfers the element to a waiting consumer immediately, if possible. tryTransfer(E e, long timeout, TimeUnit unit)  Transfers the element to a consumer if it is possible to do so before the timeout elapses. getWaitingConsumerCount()  Returns an estimate of the number of consumers waiting to receive elements via BlockingQueue.take() or timed poll hasWaitingConsumer()  Returns true if there is at least one consumer waiting to receive an element via BlockingQueue.take () or timed poll.
23. ThreadLocalRandom A random number generator isolated to current thread Aim is to reduce contention in multi threaded environments Usage:  ThreadLocalRandom.current().nextInt(min, max);  ThreadLocalRandom.current().nextLong(min, max);  ThreadLocalRandom.current().nextDouble(min, max);
24. ConcurrentLinkedDeque Unbound concurrent deque based on linked nodes Concurrent insertion, removal, and access operations execute safely across multiple threads Iterators are weakly consistent and do not throw ConcurrentModificationException size() method is NOT constant in time Bulk operations are not guaranteed to perform atomically
25. Phaser An reusable synchronization barrier Similar to CyclicBarrier or CountDownLatch but with more advance features :  Allows number of registered parties to change after Phaser creation  Each generation increment phase number of phaser  There are blocking and non blocking versions of operations Extremely flexible and complex, use Javadoc when you need to use this class
26. SCTP Support Feature SCTP TCP UDPConnection-oriented  Full duplex   Reliable data transfer  Partial-reliable data transfer optionalOrdered data delivery  Unordered data delivery  Flow control  Congestion control  ECN capable  Selective ACKs  optionalPreservation of message boundaries  Path MTU discovery  Application PDU fragmentation  Application PDU bundling  Multistreaming Multihoming Protection against SYN flooding attacks  n/aAllows half-closed connections  n/aReachability check  Psuedo-header for checksum (uses vtags)  Time wait state for vtags for 4-tuple n/a • Not all operating systems support SCTP
27. The Need for Java NIO.2 Methods works inconsistently  Delete method sometimes can not delete  Rename method sometimes can not rename No Exception is thrown from failed method Accessing metadata of files is limited Does not scale well  Listing a directory may take a long time (especially over network directories) A change notification facility is not provided Developers wanted to create their own file system implementations  For example an in-memory file system
28. Pluggable FileSystems java.nio.file.FileSystems is factory for file systems. Implement java.nio.file.spi.FileSystemProvider to provide your own file systems  A filesystem provider for Zip and Jar files are included in JDK 7  You can implement a filesystem to open a ISO image as a file system, or implement a RAM disk etc…… java.nio.file.FileSystems.getDefault() is used most of the time Multiple/Alternate views of same underlying files  Hides some files for security, read-only view, etc.
29. java.nio.file.Path and java.nio.file.Files Use java.nio.file.Path and java.nio.file.Files instead of java.io.File  Use java.io.File.toPath and java.nio.file.Path.toFile methods to integrate with legacy code java.nio.file.Paths contains factory methods for java.nio.file.Path  static Path getPath(String first, String… more)  static Path getPath(URI uri) Once you obtained Path object use java.nio.file.Files static methods to process  copy, createLink, createTempFile, delete, exist, getPosixFilePermissions, getAttribute, isHidden, isExecutable, isSymbolicLink, newByteChannel ……
30. Asynchronous I/O Allows application to continue on something while waiting for I/O Asynchronous I/O operations will usually take one of two forms:  Future<V> operation(…)  void operation(… A attachment, CompletionHandler<V,? super A> handler) See classes AsynchronousXXXXChannel that extends AsynchronousChannel Path path = Paths.get(“/home/deniz/dropbox/Getting Started.pdf”); AsynchronousFileChannel ch = AsynchronousFileChannel.open(path); ByteBuffer buf = ByteBuffer.allocate(1024); Future<Integer> result = ch.read(buf, 0); //read does not block while (!result.isDone()) { System.out.println(“lets do something else while waiting”); } System.out.println(“Bytes read = ” + result.get()); //Future.get will block ch.close();
31. Watch Service WatchService allows you to monitor a watchable object for changes and events. It implements Reactor pattern. Just like Selector does for Channels. Use Watchable.register to register with java.nio.file.WatchService to listen changes WatchKey register(WatchService watcher, WatchEvent.Kind<?>… events)  WatchEvent.Kind is an interface, actual applicable alternatives depends on Watchable Example Watchable objects  Path, FileSystem Path path = Paths.get(“C:/Users/Deniz/Dropbox/”); WatchService watcher = path.getFileSystem().newWatchService(); while (true) { path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE); WatchKey key = watcher.take(); // block for event for (WatchEvent event : key.pollEvents()) { Path pathOfNewFile = (Path) event.context(); // path of new file System.out.println(“File is created:” + pathOfNewFile.toString()); } key.cancel(); }
32. FileVisitorPath start = …Files.walkFileTree(start, new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE;   } public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException { if (e == null) { Files.delete(dir); return FileVisitResult.CONTINUE; } else { // directory iteration failed throw e; } }});
33. FileTypeDetector Files.walkFileTree(Paths.get(“C:/Users/Deniz/Downloads”), new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { String fileType = Files.probeContentType(file); System.out.println(file.toString() + “:” + fileType); return FileVisitResult.CONTINUE; } });Output:C:UsersDenizDownloadsWirofon-Client-0.2.10-rc01.exe:application/x-msdownloadC:UsersDenizDownloadsPlan B-She Said.mp3:audio/mpegC:UsersDenizDownloadsROM or Kernel-Update.pdf:application/pdf
34. Xrender Pipeline Will replace OpenGL pipeline on Unix, Linux platforms  Due to poor OpenGL drivers OpenGL pipeline has several problems  A lot of drivers has optimized Xrender implementations  Xrender is more suited 2D applications than OpenGL  Xrender applications are native X11 applications  Other GUI libraries are also using Xrender for 2D effects  QT4, GTK+, KDE4 Use -Dsun.java2d.xrender=true to enable it
35. Translucent Windows Java SE 6u10 introduced com.sun.awt.AWTUtilities to support translucent and shaped windows  AWTUtilities is removed in JDK7  Simple translucencysetUndecorated(true);setOpacity (opacityValue); //opacity value is between 0.0 – 1.0 Per-Pixel translucencyColor colorA = new Color (255, 0, 0, 0); // Transparent redColor colorB = new Color (255, 0, 0, 255); // Solid redsetBackground(new Color (0, 0, 0, 0)); // activate per-pixel  Transparent and shaped windows // translucency.protected void paintComponent (Graphics g) { Graphics2D g2d = (Graphics2D) g; GradientPaint gp = new GradientPaint ( 0.0f, 0.0f, colorA, 0.0f, getHeight (), colorB, true); g2d.setPaint (gp); g2d.fillRect (0, 0, getWidth (), getHeight ());}
36. Shaped Windows  Shaped windows  Only the parts that belong to the given Shape remain visible and clickable  A shaped window can also be translucent if you wantsetUndecorated(true);setShape(new Ellipse2D.Float (0, 0, getWidth (), getHeight ()));For code samples see: Exploring JDK 7, Part 2: Translucent and Shaped Windows
37. JLayer and LayerUI Similar to GlassPane but it is a layer around any JComponent not only JRootPane You can mask mouse events in installUI methodLayerUI<JFormattedTextField> layerUI = new ValidationLayerUI();……JFormattedTextField dateField = new JFormattedTextField(dateFormat);JFormattedTextField numberField = new JFormattedTextField(numberFormat);……JPanel datePanel = new JPanel();……datePanel.add(new JLayer<JFormattedTextField>(dateField, layerUI));datePanel.add(new JLayer<JFormattedTextField>(numberField , layerUI));……class ValidationLayerUI extends LayerUI<JFormattedTextField> { public void paint (Graphics g, JComponent c) { super.paint (g, c); JLayer jlayer = (JLayer) c; JFormattedTextField ftf = (JFormattedTextField)jlayer.getView(); if (!ftf.isEditValid()) { //paint an error indicator using Graphics } }}//See http://download.oracle.com/javase/tutorial/uiswing/misc/jlayer.html for details
38. java.util.Objects Static utility methods for simplifying common operations on objects requireNonNull(T obj, String message)  Checks that the specified object reference is not null and throws a customized NullPointerException if it is int compare(T a, T b, Comparator<? super T> c)  Perform null and equals checks before invoking c.compare(a,b) boolean deepEquals(Object a, Object b) boolean equals(Object a, Object b)  Perform null checks and Arrays.deepEquals if necessary String toString(Object o, String nullDefault)  Perform null check and return nullDefault if o is null There are other similar methods, check javadoc
39. ThreadLocal  These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variableimport java.util.concurrent.atomic.AtomicInteger;public class UniqueThreadIdGenerator { private static final AtomicInteger uniqueId = new AtomicInteger(0); private static final ThreadLocal<Integer> uniqueNum = new ThreadLocal<Integer> () { @Override protected Integer initialValue() { return uniqueId.getAndIncrement(); } }; public static int getCurrentThreadId() { return uniqueId.get(); }} // UniqueThreadIdGenerator
40. Locale Category Locale.setDefault(Category, Locale) Locale Locale.getDefault(Category) Category.DISPLAY  Category used to represent the default locale for displaying user interfaces Category.FORMAT  Category used to represent the default locale for formatting dates, numbers, and/or currencies What about default locale Locale.getDefault() ?  It stays as a different locale (not display or format)     But Locale.setDefault(Locale) sets 3 different locales now:display, format and default locales  Default locale depends on OS environment variables or value of –D flags. Set it to English for your health and change newly provided locales (display/format)
41. References Exploring JDK 7, Part 2: Translucent and Shaped Windows What’s New in NIO.2 (pdf) JDK 7 Features The Java NIO.2 File System in JDK 7 Xrender Proposal Java 2D Enhancements in Java SE 7 How to Decorate Components with the JLayer Class StackOverflow

Leave a Reply

Your email address will not be published. Required fields are marked *