background preloader

Java

Facebook Twitter

My Technical Notes: JAXB Annotation - Can't Handle Interfaces. JAXB Can't Handle Interfaces in Generics If you have a class with a List<Vehicle> and Vehicle is an interface, JAXB will throw an Exception at runtime: The solution is to maket the interface an abstract class. For example: Assuming the following JAXB context: The generated XML and verification is: Notice that JAXB is capable of recreating both Bus and Car instances. Next JAXB to XML • JAXB to JSON • JAXB Annotations Tutorial Table Of Content. Java theory and practice: Managing volatility. Volatile variables in the Java language can be thought of as "synchronized lite"; they require less coding to use than synchronized blocks and often have less runtime overhead, but they can only be used to do a subset of the things that synchronized can. This article presents some patterns for using volatile variables effectively -- and some warnings about when not to use them. Locks offer two primary features: mutual exclusion and visibility.

Mutual exclusion means that only one thread at a time may hold a given lock, and this property can be used to implement protocols for coordinating access to shared data such that only one thread at a time will be using the shared data. Volatile variables Volatile variables share the visibility features of synchronized, but none of the atomicity features. This means that threads will automatically see the most up-to-date value for volatile variables. Conditions for correct use of volatile Listing 1. Performance considerations Pattern #1: status flags.

Efficiency Matters - Jenkov.com. Thread-Safe. Thread-safety or thread-safe code in Java refers to code which can safely be used or shared in concurrent or multi-threading environment and they will behave as expected. any code, class or object which can behave differently from its contract on concurrent environment is not thread-safe. thread-safety is one of the risk introduced by using threads in Java and I have seen java programmers and developers struggling to write thread-safe code or just understanding what is thread-safe code and what is not?

This will not be very detailed article on thread-safety or low level details of synchronization in Java instead we will keep it simple and focus on one example of non thread-safe code and try to understand what is thread-safety and how to make an code thread-safe. Example of Non Thread Safe Code in Java Here is an example of non thread-safe code, look at the code and find out why this code is not thread safe ? * Non Thread-Safe Class in Java public class Counter { private int count; Thanks. NoClassDefFoundError. I know how frustrating is to see Exception in thread "main" java.lang.NoClassDefFoundError, which is a manifestation of NoClassDefFoundError in Java. I have seen it couple of times and spent quite a lot time initially to figure out what is wrong , which class is missing etc.

First mistake I did was mingling java.lang.ClassNotfoundException and NoClassDefFoundError, in reality they are totally different, and my second mistake was using trial and error method to solve this java.lang.NoClassDefFoundError instead of understanding why NoClassDefFoundError is coming, what is real reason behind NoClassDefFoundError and how to resolve this. In this Java tutorial I have tried to rectify that mistakes and uncover some secrets of NoClassDefFoundError in Java and will share my experience around it. NoClassDefFoundError is not something which cannot be resolved or hard to resolve it’s just its manifestation which puzzles most of Java developer. What is reason of NoClassDefFoundError in Java? 1. 2. Tutorials.jenkov.com.

Java GUI

Coreservlets.com: Java, JSF 2.0, Ajax, jQuery, Spring, Hibernate, REST, Hadoop, and GWT Training, Tutorials, Consulting, Books, & Resources.