Featured FREE Whitepapers

What's New Here?

java-logo

Java Reflection Tutorial – The ULTIMATE Guide

This tutorial is about reflection, the ability of a computer program to examine and modify the structure and behavior (specifically the values, meta-data, properties and functions) of the program at runtime. We are going to explain what reflection is in general and how can be used in Java. Real uses cases about different reflection uses are listed in the next chapters. Several code snippets will be shown; at the end of this article you can find a compressed file that contains all these examples (and some more). All code has been written using Eclipse Luna 4.4 and Java update 8.25, no third party libraries are needed.Want to master Java Reflection ?Subscribe to our newsletter and download the Java Reflection Ultimate Guide right now! In order to help you master the topic of Reflection, we have compiled a kick-ass guide with all the major features and use cases! Besides studying them online you may download the eBook in PDF format!Email address:Given email address is already subscribed, thank you!Oops. Something went wrong. Please try again later.Please provide a valid email address.Thank you, your sign-up request was successful! Please check your e-mail inbox.Please complete the CAPTCHA.Please fill in the required fields.Table Of Contents1. Reflection 2. Introduction to reflection in Java 3. Use cases 4. Reflection components and mechanisms 5. Classes 6. Interfaces 7. Enums 8. Primitive types 9. Fields 10. Methods 11. Constructors 12. Getters and Setters 13. Static elements 14. Arrays 15. Collections 16. Annotations 17. Generics 18. Class Loaders 19. Dynamic Proxies 20. Java 8 Reflection features 21. Summary 22. Download 23. Resources  1. Reflection The concept of reflection in software means the ability to inspect, analyze and modify other code at runtime. For example imagine an application that takes as input some files containing source code (we do not care about what source code yet). The goal of this application is to count the number of methods that are contained in each passed class. This can be solved using reflection by analyzing the code and counting the elements which are actually methods, ignoring other kind of elements like attributes, interfaces, etc, and grouping them by classes. Purely speaking, this example is not really reflection, because the code does not have to be analyzed at runtime and the task can be done in any other stage, but it can be also done at runtime and then we would be actually talking about reflection. Another example would be an application that analyzes the content of given classes and executes the methods that contain a specific annotation with arguments provided in runtime: In the Java Junit framework we have for example the annotation @Test. This is actually what Junit does; and does it using reflection. 2. Introduction to reflection in Java In Java, it is possible to inspect fields, classes, methods, annotations, interfaces, etc. at runtime. You do not need to know how classes or methods are called, neither the parameters that are needed, all of that can be retrieved at runtime using reflection. It is also possible to instantiate new classes, to create new instances and to execute their methods, all of it using reflection. Reflection is present in Java since the beginning of the times via its reflection API. The class Class contains all the reflection related methods that can be applied to classes and objects like the ones that allow a programmer to retrieve the class name, to retrieve the public methods of a class, etc. Other important classes are Method, Field and Type containing specific reflection methods that we are going to see in this tutorial. Although reflection is very useful in many scenarios, it should not be used for everything. If some operation can be executed without using reflection, then we should not use it. Here are some reasons:The performance is affected by the use of reflection since all compilation optimizations cannot be applied: reflection is resolved at runtime and not at compile stages. Security vulnerabilities have to be taken into consideration since the use of reflection may not be possible when running in secure contexts like Applets. Another important disadvantage that is good to mention here is the maintenance of the code. If your code uses reflection heavily it is going to be more difficult to maintain. The classes and methods are not directly exposed in the code and may vary dynamically so it can get difficult to change the number of parameters that a method expects if the code that calls this method is invoked via reflection. Tools that automatically refactor or analyze the code may have trouble when a lot of reflection is present.3. Use cases Despite all the limitations, reflection is a very powerful tool in Java that can be taken into consideration in several scenarios. In general, reflection can be used to observe and modify the behavior of a program at runtime. Here is a list with the most common use cases:IDEs can heavily make use of reflection in order to provide solutions for auto completion features, dynamic typing, hierarchy structures, etc. For example, IDEs like Eclipse or PHP Storm provide a mechanism to retrieve dynamically the arguments expected for a given method or a list of public methods starting by “get” for a given instance. All these are done using reflection. Debuggers use reflection to inspect dynamically the code that is being executed. Test tools like Junit or Mockito use reflection in order to invoke desired methods containing specific syntax or to mock specific classes, interfaces and methods. Dependency injection frameworks use reflection to inject beans and properties at runtime and initialize all the context of an application. Code analysis tools like PMD or Findbugs use reflection in order to analyze the code against the list of code violations that are currently configured. External tools that make use of the code dynamically may use reflection as well.In this tutorial we are going to see several examples of use of reflection in Java. We will see how to get all methods for a given instance, without knowing what kind of class this instance is and we are going to invoke different methods depending on their syntax. We are not just going to show what other tutorials do, but we will go one step forward by indicating how to proceed when using reflection with generics, annotations, arrays, collections and other kind of objects. Finally we will explain the main new features coming out with Java 8 related to this topic. 4. Reflection components and mechanisms In order to start coding and using reflection in Java we first have to explain a couple of concepts that may be relevant.Interface in Java is a contract with the applications that may use them. Interfaces contain a list of methods that are exposed and that have to be implemented by the subclasses implementing these interfaces. Interfaces cannot be instantiated. Since Java 8 they can contain default method implementations although this is not the common use. Class is the implementation of a series of methods and the container of a series of properties. It can be instantiated. Object is an instance of a given class. Method is some code performing some actions. They have return types as outputs and input parameters. Field is a property of a class. Enums are elements containing a set of predefined constants. Private element is an element that is only visible inside a class and cannot be accessed from outside. It can be a method, a field… Static elements are elements that belong to the class and not to a specific instance. Static elements can be fields used across all instances of a given class, methods that can be invoked without need to instantiate the class, etc. This is very interesting while using reflection since it is different to invoke a static method than a non static one where you need an instance of a class to execute it. Annotation is code Meta data informing about the code itself. Collection is a group of elements, can be a List, a Map, a Queue, etc. Array is an object containing a fixed number of values. Its length is fixed and is specified on creation. Dynamic proxy is a class implementing a list of interfaces specified at runtime. They use the class java.lang.reflect.Proxy. We will see this more in detail in the next chapters. Class loader is an object in charge of loading classes given the name of a class. In Java, every class provide methods to retrieve the class loader: Class.getClassLoader(). Generics were introduced in java update 5. They offer compile time safety by indicating what type or sub types a collection is going to use. For example using generics you can prevent that an application using a list containing strings would try to add a Double to the list in compile time.The different nature of these components is important in order to use reflection within them. Is not the same to try to invoke a private method than a public one; it is different to get an annotation name or an interface one, etc. We will see examples for all of these in the next chapters. 5. Classes Everything in Java is about classes, reflection as well. Classes are the starting point when we talk about reflection. The class java.lang.Class contains several methods that allow programmers to retrieve information about classes and objects (and other elements) at runtime. In order to retrieve the class information from a single instance we can write (in this case, for the String class): Class<? extends String> stringGetClass = stringer.getClass();Or directly from the class name without instantiation: Class<String> stringclass = String.class;or using the java.lang.Class.forName(String) method: Class.forName( "java.lang.String" )From a class object we can retrieve all kind of information like declared methods, constructors, visible fields, annotations, types…In this tutorial all these is explained in the following chapters. It is also possible to check properties for a given class like for example if a class is a primitive, or an instance: stringGetClass.isInstance( "dani" ); stringGetClass.isPrimitive();It is also possible to create new instances of a given class using the method java.lang.Class.newInstance() passing the right arguments: String newInstanceStringClass = stringclass.newInstance();String otherInstance = (String)Class.forName( "java.lang.String" ).newInstance();The java.lang.Class.newInstance() method can be used only when the class contains a public default constructor or a constructor without arguments, if this is not the case, this method cannot be used. In these cases where the java.lang.Class.newInstance() method cannot be used the solution is to retrieve a proper constructor at runtime and create an instance using this constructor with the arguments that it is expecting. We will see in the chapter related to constructors. 6. Interfaces Interfaces are elements that cannot be instantiated and that contain the exposed methods that should be implemented by their subclasses. Related to reflection there is nothing special regarding interfaces. Interfaces can be accessed like a class using their qualified name. All methods available for classes are available for interfaces as well. Here is an example of how to access interface class information at runtime:// can be accessed like a class System.out.println( "interface name: " + InterfaceExample.class.getName() );Assuming that the InterfaceExample element is an interface. One obvious difference between classes and interfaces is that interfaces cannot be instantiated using reflection via the newInstance() method:// cannot be instantiated: java.lang.InstantiationException InterfaceExample.class.newInstance();The snippet above will throw an InstantiationException at runtime. At compile time no error appears. 7. Enums Enums are special java types that allow variables to be a set of constants. These constants are predefined in the enum declaration:enum ExampleEnum { ONE, TWO, THREE, FOUR };Java contains several enums specific methods:java.lang.Class.isEnum(): Returns true if the element is of the type enum. False otherwise java.lang.Class.getEnumConstants(): Gets all constants for the given element (which is an enum). In case the element is not an enum an exception is thrown. java.lang.reflect.Field.isEnumConstant(): Returns true in case the field used is an enum constant. False otherwise. Only applicable to fields.We are going to see an example of how to use the main enum methods related to reflection. First of all we create an instance of the enum:ExampleEnum value = ExampleEnum.FOUR;We can check if the element is an enum using the method isEnum(): System.out.println( "isEnum " + value.getClass().isEnum() );In order to retrieve all the enum constants we can do something like the following using the method getEnumConstants():ExampleEnum[] enumConstants = value.getClass().getEnumConstants(); for( ExampleEnum exampleEnum : enumConstants ) { System.out.println( "enum constant " + exampleEnum ); }Finally we can check how to use the field related method isEnumConstants(). First we retrieve all declared fields for the given class (we will see more in detail in the next chapters all methods related reflection utilities) and after that we can check if the field is an enum constant or not:Field[] flds = value.getClass().getDeclaredFields(); for( Field f : flds ) { // check for each field if it is an enum constant or not System.out.println( f.getName() + " " + f.isEnumConstant() ); }The output of all these pieces of code will be something like the following:isEnum true enum constant ONE enum constant TWO enum constant THREE enum constant FOUR ONE true TWO true THREE true FOUR true ENUM$VALUES falseThe string ENUM$VALUES false refers to the internal enum values field. For more information about enums and how to handle them, please visit http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html. 8. Primitive types In Java, there are a couple of types that are handled differently because of its nature and behavior: when we are talking about reflection, primitive types like int, float, double, etc. can be accessed and used almost like any other classes. Here are a couple of examples of how to use reflection when we are working with primitive types: • It is possible to retrieve a class object from a primitive type as for any other non primitive type:Class<Integer> intClass = int.class; • But It is not possible to create new instances for primitive types using reflection:Integer intInstance = intClass.newInstance(); • It is possible to check if a given class belongs to a primitive type or not using the method java.lang.Class.isPrimitive():System.out.println( "is primitive: " + intClass.isPrimitive() ); In this case an exception of the type java.lang.InstantiationException is going to be thrown. 9. Fields Class fields can be handled in runtime using reflection. Classes offer several methods to access their fields at runtime. The most important ones are: • java.lang.Class.getDeclaredFields(): It returns an array with all declared fields for the class. It returns all private fields as well. • java.lang.Class.getFields(): It returns an array with all accessible fields for the class. • java.lang.Class.getField(String): It returns a field with the name passed as parameter. It throws an exception if the field does not exist or is not accessible. • java.lang.Class.getDeclaredFields(): It returns a field with the given name, if the field does not exist it throws an exception. These methods return an array of elements (or a single one) of the type java.lang.reflect.Field. This class contains several interesting methods that can be used at runtime that allow a programmer to read the properties and the values of the specific field. Here is a class that uses this functionality:String stringer = "this is a String called stringer";Class<? extends String> stringGetClass = stringer.getClass();Class<String> stringclass = String.class;Field[] fields = stringclass.getDeclaredFields();for( Field field : fields ) { System.out.println( "*************************" ); System.out.println( "Name: " + field.getName() ); System.out.println( "Type: " + field.getType() );// values if( field.isAccessible() ) { System.out.println( "Get: " + field.get( stringer ) ); // depending on the type we can access the fields using these methods // System.out.println( "Get boolean: " + field.getBoolean( stringer ) ); // System.out.println( "Get short: " + field.getShort( stringer ) ); // ... } System.out.println( "Modifiers:" + field.getModifiers() ); System.out.println( "isAccesible: " + field.isAccessible() );}// stringclass.getField( "hashCode" );//exceptionField fieldHashCode = stringclass.getDeclaredField( "hash" );// all fields can be // accessed this way// fieldHashCode.get( stringer ); // this produces an java.lang.IllegalAccessException// we change the visibility fieldHashCode.setAccessible( true );// and we can access it Object value = fieldHashCode.get( stringer ); int valueInt = fieldHashCode.getInt( stringer ); System.out.println( value );System.out.println( valueInt );In the snippet shown above you can see that the Field contains several methods to get the values of a given field like get() or type specific ones like getInt(). We also can see in the pasted code how we can change the way the visibility of a given field by using the method setAccessible(). This is not always possible and under specific conditions and environments may be prevented. However this allows us to make a private field accessible and access its value and properties via reflection. This is very useful in testing frameworks like Mockito or PowerMock. The output or the program would be:************************* Name: value Type: class [C Modifiers:18 isAccesible: false ************************* Name: hash Type: int Modifiers:2 isAccesible: false ************************* Name: serialVersionUID Type: long Modifiers:26 isAccesible: false ************************* Name: serialPersistentFields Type: class [Ljava.io.ObjectStreamField; Modifiers:26 isAccesible: false ************************* Name: CASE_INSENSITIVE_ORDER Type: interface java.util.Comparator Modifiers:25 isAccesible: false 0 010. Methods In order to retrieve all visible methods for a given class we can do the following:Class<String> stringclass = String.class; Method[] methods = stringclass.getMethods();Using the method java.lang.Class.getMethods() all visible or accessible methods for a given class are retrieved. We can also retrieve an specific method using its name and the type of the arguments he is expecting to receive, as an example:Method methodIndexOf = stringclass.getMethod( "indexOf", String.class ); For a given method (an instance of the type java.lang.reflect.Method), we can access all its properties. The following snippet shows a couple of them like name, default values, return type, modifiers, parameters, parameter types or the exceptions thrown, we can also check if a method is accessible or not:// All methods for the String class for( Method method : methods ) { System.out.println( "****************************************************" ); System.out.println( "name: " + method.getName() ); System.out.println( "defaultValue: " + method.getDefaultValue() );System.out.println( "generic return type: " + method.getGenericReturnType() ); System.out.println( "return type: " + method.getReturnType() );System.out.println( "modifiers: " + method.getModifiers() );// Parameters Parameter[] parameters = method.getParameters(); System.out.println( parameters.length + " parameters:" ); // also method.getParameterCount() is possible for( Parameter parameter : parameters ) { System.out.println( "parameter name: " + parameter.getName() ); System.out.println( "parameter type: " + parameter.getType() ); } Class<?>[] parameterTypes = method.getParameterTypes(); System.out.println( parameterTypes.length + " parameters:" ); for( Class<?> parameterType : parameterTypes ) { System.out.println( "parameter type name: " + parameterType.getName() ); }// Exceptions Class<?>[] exceptionTypes = method.getExceptionTypes(); System.out.println( exceptionTypes.length + " exception types: " ); for( Class<?> exceptionType : exceptionTypes ) { System.out.println( "exception name " + exceptionType.getName() ); }System.out.println( "is accesible: " + method.isAccessible() ); System.out.println( "is varArgs: " + method.isVarArgs() );}It is also possible to instantiate given methods for specific objects passing the arguments that we want, we should assure that the amount and type of the arguments is correct:Object indexOf = methodIndexOf.invoke( stringer, "called" ); This last feature is very interesting when we want to execute specific methods at runtime under special circumstances. Also in the creation of Invocation handlers for dynamic proxies is very useful, we will see this point at the end of the tutorial. 11. Constructors Constructors can be used via reflection as well. Like other class methods they can be retrieved in runtime and several properties can be analyzed and checked like the accessibility, the number of parameters, their types, etc. In order to retrieve all visible constructors from a class, we can do something like:// get all visible constructors Constructor<?>[] constructors = stringGetClass.getConstructors();In the snippet above we are retrieving all visible constructors. If we want to get all the constructors, including the private ones, we can do something like://all constructors Constructor<?>[] declaredConstructors = stringclass.getDeclaredConstructors();General information about constructors such as parameters, types, names, visibility, annotations associated, etc. can be retrieved in the following way:for( Constructor<?> constructor : constructors ) { int numberParams = constructor.getParameterCount() ; System.out.println( "constructor " + constructor.getName() ); System.out.println( "number of arguments " + numberParams); // public, private, etc. int modifiersConstructor = constructor.getModifiers(); System.out.println( "modifiers " + modifiersConstructor ); // array of parameters, more info in the methods section Parameter[] parameters = constructor.getParameters(); // annotations array, more info in the annotations section Annotation[] annotations = constructor.getAnnotations();}Constructors can also be used to create new instances. This may be very useful in order to access private or not visible constructors. This should be done only under very special circumstances and depending on the system where the application is running may not work because of security reasons as explained at the beginning of this tutorial. In order to create a new instance of a class using a specific constructor we can do something like the following:// can be used to create new instances (no params in this case) String danibuizaString = (String)constructor.newInstance( );In has to be taken into consideration that the amount of parameters and their type should match the constructor instance ones. Also the accessibility of the constructor has to be set to true in order to invoke it (if it was not accesible). This can be done in the same way as we did for class methods and fields. 12. Getters and Setters Getters and setters are not different to any other class method inside a class. The main difference is that they are a standard way to access private fields. Here is a description of both: • Getters are used to retrieve the value of a private field inside a class. Its name starts with “get” and ends with the name of the property in camel case. They do not receive any parameter and their return type is the same than the property that they are returning. They are public. • Setters are used to modify the value of a private field inside a class. Its name starts with “set” and ends with the name of the property in camel case. They receive one parameters of the same type than the property that they are modifying and they do not return any value (void). They are public. For example, for the private property private int count; we can have the getter and setter methods:public int getCount(){ return this.count; }public void setCount(int count){ this.count = count; }Following these standards we can use reflection to access (read and modify) at runtime all the private properties of a class exposed via getters and setters. This mechanism is used by several known libraries like Spring Framework or Hibernate, where they expect classes to expose their properties using these kinds of methods. Here is an example of how to use getters and setters using reflection:Car car = new Car( "vw touran", "2010", "12000" );Method[] methods = car.getClass().getDeclaredMethods();// all getters, original values for( Method method : methods ) { if( method.getName().startsWith( "get" ) ) { System.out.println( method.invoke( car ) ); } }// setting values for( Method method : methods ) {if( method.getName().startsWith( "set" ) ) { method.invoke( car, "destroyed" ); } }// get new values for( Method method : methods ) { if( method.getName().startsWith( "get" ) ) { System.out.println( method.invoke( car ) ); } }Where the class Car. looks like the following:public class Car {private String name; private Object price; private Object year;public Car( String name, String year, String price ) { this.name = name; this.price = price; this.year = year; }public String getName() { return name; }public void setName( String name ) { this.name = name; }public Object getPrice() { return price; }public void setPrice( Object price ) { this.price = price; }public Object getYear() { return year; }public void setYear( Object year ) { this.year = year; }}The output will be something like:vw touran 2010 12000 destroyed destroyed destroyed13. Static elements Static classes, methods and fields behave completely different than instance ones. The main reason is that they do not need to be instantiated or created before they are invoked. They can be used without previous instantiation. This fact changes everything: static methods can be invoked without instantiating their container classes, static class fields are stateless (so thread safe), static elements are very useful in order to create singletons and factories… Summarizing, static elements are a very important mechanism in Java. In this chapter we are going to show the main differences between static and instance elements in relation to reflection: How to create static elements at runtime and how to invoke them. For example, for the next static inline class:public class StaticReflection {static class StaticExample { int counter; } ... In order to retrieve static inline classes we have the following options:// 1 access static class System.out.println( "directly " + StaticExample.class.getName() );//2 using for name directly throws an exception Class<?> forname = Class.forName("com.danibuiza.javacodegeeks.reflection.StaticReflection.StaticExample" );//3 using $ would work but is not that nice Class<?> forname = Class.forName("com.danibuiza.javacodegeeks.reflection.StaticReflection$StaticExample" );// 4 another way iterating through all classes declared inside this class Class<?>[] classes = StaticReflection.class.getDeclaredClasses(); for( Class<?> class1 : classes ) { System.out.println( "iterating through declared classes " + class1.getName() ); }The main difference is that the class is contained inside another class; this has nothing to do with reflection but with the nature of inline classes. In order to get static methods from a class, there are no differences with the access to instance ones (this applies to fields as well):// access static methods in the same way as instance ones Method mathMethod = Math.class.getDeclaredMethod( "round", double.class );In order to invoke static methods or fields we do not need to create or specify an instance of the class, since the method (or the field) belongs to the class itself, not to a single instance:// methods: object instance passed can be null since method is static Object result = mathMethod.invoke( null, new Double( 12.4 ) );// static field access, instance can be null Field counterField = Counter.class.getDeclaredField( "counter" ); System.out.println( counterField.get( null ) );14. Arrays The class java.lang.reflect.Array offers several functionalities for handling arrays; it includes various static reflective methods: • java.lang.reflect.Array.newInstance(Class, int): Creates a new instance of an array of the type passed as parameter with the length given in the second argument. Is similar to the method with the same name in the java.lang.Class class but this one contains parameters that allows the programmer to set the type of the array and its length. • java.lang.reflect.Array.set(Object, int, Object): Sets an element (passed index) of the given array with the object passed as argument. • java.lang.reflect.Array.getLength(Object): Returns the length of the array as int. • java.lang.reflect.Array.get(Object, int): Retrieves the element of the array positioned in the pased index. Returns an object. • java.lang.reflect.Array.getInt(Object, int): Similar method for the primitive type int. Returns an int. There are methods available for all primitive types. Here is an example of how we can use all these methods:// using the Array class it is possible to create new arrays passing the type and the length via reflection String[] strArrayOne = (String[])Array.newInstance( String.class, 10 );// it contains utility methods for setting values Array.set( strArrayOne, 0, "member0" ); Array.set( strArrayOne, 1, "member1" ); Array.set( strArrayOne, 9, "member9" );// and for getting values as well System.out.println( "strArrayOne[0] : " + Array.get( strArrayOne, 0 ) ); System.out.println( "strArrayOne[1] : " + Array.get( strArrayOne, 1 ) ); System.out.println( "strArrayOne[3] (not initialized) : " + Array.get( strArrayOne, 3 ) ); System.out.println( "strArrayOne[9] : " + Array.get( strArrayOne, 9 ) );// also methods to retrieve the lenght of the array System.out.println( "lenght strArrayOne: " + Array.getLength( strArrayOne ) );// primitive types work as well int[] intArrayOne = (int[])Array.newInstance( int.class, 10 );Array.set( intArrayOne, 0, 1 ); Array.set( intArrayOne, 1, 2 ); Array.set( intArrayOne, 9, 10 );// and specific getters and setters for primitive types for( int i = 0; i < Array.getLength( intArrayOne ); ++i ) { System.out.println( "intArrayOne[" + i + "] : " + Array.getInt( intArrayOne, i ) ); }The output of the program above would be: ...
java-logo

Multithreading and Concurrency Interview Questions and Answers – The ULTIMATE List (PDF Download)

EDITORIAL NOTE: Concurrency is always a challenge for developers and writing concurrent programs can be extremely hard. There is a number of things that could potentially blow up and the complexity of systems rises considerably when concurrency is introduced. However, the ability to write robust concurrent programs is a great tool in a developer’s belt and can help build sophisticated, enterprise level applications. In this article we will discuss different types of questions that can be used in a programming interview in order to assess a candidate’s understanding of concurrency and multithreading. The questions are not only Java specific, but revolve around general programming principles. Enjoy!Programming Interview Coming Up?Subscribe to our newsletter and download the Ultimate Multithreading and Concurrency interview questions and answers collection right now! In order to get you prepared for your next Programming Interview, we have compiled a huge list of relevant Questions and their respective Answers. Besides studying them online you may download the eBook in PDF format!Email address:Given email address is already subscribed, thank you!Oops. Something went wrong. Please try again later.Please provide a valid email address.Thank you, your sign-up request was successful! Please check your e-mail inbox.Please complete the CAPTCHA.Please fill in the required fields.Table Of Contents1. What do we understand by the term concurrency? 2. What is the difference between processes and threads? 3. In Java, what is a process and a thread? 4. What is a scheduler? 5. How many threads does a Java program have at least? 6. How can a Java application access the current thread? 7. What properties does each Java thread have? 8. What is the purpose of thread groups? 9. What states can a thread have and what is the meaning of each state? 10. How do we set the priority of a thread? 11. How is a thread created in Java? 12. How do we stop a thread in Java? 13. Why should a thread not be stopped by calling its method stop()? 14. Is it possible to start a thread twice? 15. What is the output of the following code? 16. What is a daemon thread? 17. Is it possible to convert a normal user thread into a daemon thread after it has been started? 18. What do we understand by busy waiting? 19. How can we prevent busy waiting? 20. Can we use Thread.sleep() for real-time processing? 21. How can a thread be woken up that has been put to sleep before using Thread.sleep()? 22. How can a thread query if it has been interrupted? 23. How should an InterruptedException be handled? 24. After having started a child thread, how do we wait in the parent thread for the termination of the child thread? 25. What is the output of the following program? 26. What happens when an uncaught exception leaves the run() method? 27. What is a shutdown hook? 28. For what purposes is the keyword synchronized used? 29. What intrinsic lock does a synchronized method acquire? 30. Can a constructor be synchronized? 31. Can primitive values be used for intrinsic locks? 32. Are intrinsic locks reentrant? 33. What do we understand by an atomic operation? 34. Is the statement c++ atomic? 35. What operations are atomic in Java? 36. Is the following implementation thread-safe? 37. What do we understand by a deadlock? 38. What are the requirements for a deadlock situation? 39. Is it possible to prevent deadlocks at all? 40. Is it possible to implement a deadlock detection? 41. What is a livelock? 42. What do we understand by thread starvation? 43. Can a synchronized block cause thread starvation? 44. What do we understand by the term race condition? 45. What do we understand by fair locks? 46. Which two methods that each object inherits from java.lang.Object can be used to implement a simple producer/consumer scenario? 47. What is the difference between notify() and notifyAll()? 48. How it is determined which thread wakes up by calling notify()? 49. Is the following code that retrieves an integer value from some queue implementation correct? 50. Is it possible to check whether a thread holds a monitor lock on some given object? 51. What does the method Thread.yield() do? 52. What do you have to consider when passing object instances from one thread to another? 53. Which rules do you have to follow in order to implement an immutable class? 54. What is the purpose of the class java.lang.ThreadLocal? 55. What are possible use cases for java.lang.ThreadLocal? 56. Is it possible to improve the performance of an application by the usage of multi-threading? Name some examples. 57. What do we understand by the term scalability? 58. Is it possible to compute the theoretical maximum speed up for an application by using multiple processors? 59. What do we understand by lock contention? 60. Which techniques help to reduce lock contention? 61. Which technique to reduce lock contention can be applied to the following code? 62. Explain by an example the technique lock splitting. 63. What kind of technique for reducing lock contention is used by the SDK class ReadWriteLock? 64. What do we understand by lock striping? 65. What do we understand by a CAS operation? 66. Which Java classes use the CAS operation? 67. Provide an example why performance improvements for single-threaded applications can cause performance degradation for multi-threaded applications. 68. Is object pooling always a performance improvement for multi-threaded applications? 69. What is the relation between the two interfaces Executor and ExecutorService? 70. What happens when you submit() a new task to an ExecutorService instance whose queue is already full? 71. What is a ScheduledExecutorService? 72. Do you know an easy way to construct a thread pool with 5 threads that executes some tasks that return a value? 73. What is the difference between the two interfaces Runnable and Callable? 74. Which are use cases for the class java.util.concurrent.Future? 75. What is the difference between HashMap and Hashtable particularly with regard to thread-safety? 76. Is there a simple way to create a synchronized instance of an arbitrary implementation of Collection, List or Map? 77. What is a semaphore? 78. What is a CountDownLatch? 79. What is the difference between a CountDownLatch and a CyclicBarrier? 80. What kind of tasks can be solved by using the Fork/Join framework? 81. Is it possible to find the smallest number within an array of numbers using the Fork/Join-Framework? 82. What is the difference between the two classes RecursiveTask and RecursiveAction? 83. Is it possible to perform stream operations in Java 8 with a thread pool? 84. How can we access the thread pool that is used by parallel stream operations?  1. What do we understand by the term concurrency? Concurrency is the ability of a program to execute several computations simultaneously. This can be achieved by distributing the computations over the available CPU cores of a machine or even over different machines within the same network. 2. What is the difference between processes and threads? A process is an execution environment provided by the operating system that has its own set of private resources (e.g. memory, open files, etc.). Threads, in contrast to processes, live within a process and share their resources (memory, open files, etc.) with the other threads of the process. The ability to share resources between different threads makes thread more suitable for tasks where performance is a significant requirement. 3. In Java, what is a process and a thread? In Java, processes correspond to a running Java Virtual Machine (JVM) whereas threads live within the JVM and can be created and stopped by the Java application dynamically at runtime. 4. What is a scheduler? A scheduler is the implementation of a scheduling algorithm that manages access of processes and threads to some limited resource like the processor or some I/O channel. The goal of most scheduling algorithms is to provide some kind of load balancing for the available processes/threads that guarantees that each process/thread gets an appropriate time frame to access the requested resource exclusively. 5. How many threads does a Java program have at least? Each Java program is executed within the main thread; hence each Java application has at least one thread. 6. How can a Java application access the current thread? The current thread can be accessed by calling the static method currentThread() of the JDK class java.lang.Thread: public class MainThread {public static void main(String[] args) { long id = Thread.currentThread().getId(); String name = Thread.currentThread().getName(); ... } }7. What properties does each Java thread have? Each Java thread has the following properties:an identifier of type long that is unique within the JVM a name of type String a priority of type int a state of type java.lang.Thread.State a thread group the thread belongs to8. What is the purpose of thread groups? Each thread belongs to a group of threads. The JDK class java.lang.ThreadGroup provides some methods to handle a whole group of Threads. With these methods we can, for example, interrupt all threads of a group or set their maximum priority. 9. What states can a thread have and what is the meaning of each state?NEW: A thread that has not yet started is in this state. RUNNABLE: A thread executing in the Java virtual machine is in this state. BLOCKED: A thread that is blocked waiting for a monitor lock is in this state. WAITING: A thread that is waiting indefinitely for another thread to perform a particular action is in this state. TIMED_WAITING: A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. TERMINATED: A thread that has exited is in this state.10. How do we set the priority of a thread? The priority of a thread is set by using the method setPriority(int). To set the priority to the maximum value, we use the constant Thread.MAX_PRIORITY and to set it to the minimum value we use the constant Thread.MIN_PRIORITY because these values can differ between different JVM implementations. 11. How is a thread created in Java? Basically, there are two ways to create a thread in Java. The first one is to write a class that extends the JDK class java.lang.Thread and call its method start(): public class MyThread extends Thread {public MyThread(String name) { super(name); }@Override public void run() { System.out.println("Executing thread "+Thread.currentThread().getName()); }public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread("myThread"); myThread.start(); } }The second way is to implement the interface java.lang.Runnable and pass this implementation as a parameter to the constructor of java.lang.Thread: public class MyRunnable implements Runnable {public void run() { System.out.println("Executing thread "+Thread.currentThread().getName()); }public static void main(String[] args) throws InterruptedException { Thread myThread = new Thread(new MyRunnable(), "myRunnable"); myThread.start(); } }12. How do we stop a thread in Java? To stop a thread one can use a volatile reference pointing to the current thread that can be set to null by other threads to indicate the current thread should stop its execution: private static class MyStopThread extends Thread { private volatile Thread stopIndicator;public void start() { stopIndicator = new Thread(this); stopIndicator.start(); }public void stopThread() { stopIndicator = null; }@Override public void run() { Thread thisThread = Thread.currentThread(); while(thisThread == stopIndicator) { try { Thread.sleep(1000); } catch (InterruptedException e) { } } } }13. Why should a thread not be stopped by calling its method stop()? A thread should not be stopped by using the deprecated methods stop() of java.lang.Thread, as a call of this method causes the thread to unlock all monitors it has acquired. If any object protected by one of the released locks was in an inconsistent state, this state gets visible to all other threads. This can cause arbitrary behavior when other threads work this this inconsistent object. 14. Is it possible to start a thread twice? No, after having started a thread by invoking its start() method, a second invocation of start() will throw an IllegalThreadStateException. 15. What is the output of the following code? public class MultiThreading {private static class MyThread extends Thread {public MyThread(String name) { super(name); }@Override public void run() { System.out.println(Thread.currentThread().getName()); } }public static void main(String[] args) { MyThread myThread = new MyThread("myThread"); myThread.run(); } }The code above produces the output “main” and not “myThread”. As can be seen in line two of the main() method, we invoke by mistake the method run() instead of start(). Hence, no new thread is started, but the method run() gets executed within the main thread. 16. What is a daemon thread? A daemon thread is a thread whose execution state is not evaluated when the JVM decides if it should stop or not. The JVM stops when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement for example monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped: public class Example {private static class MyDaemonThread extends Thread {public MyDaemonThread() { setDaemon(true); }@Override public void run() { while (true) { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } }public static void main(String[] args) throws InterruptedException { Thread thread = new MyDaemonThread(); thread.start(); } }The example application above terminates even though the daemon thread is still running in its endless while loop. 17. Is it possible to convert a normal user thread into a daemon thread after it has been started? A user thread cannot be converted into a daemon thread once it has been started. Invoking the method thread.setDaemon(true) on an already running thread instance causes a IllegalThreadStateException. 18. What do we understand by busy waiting? Busy waiting means implementations that wait for an event by performing some active computations that let the thread/process occupy the processor although it could be removed from it by the scheduler. An example for busy waiting would be to spend the waiting time within a loop that determines the current time again and again until a certain point in time is reached: Thread thread = new Thread(new Runnable() { @Override public void run() { long millisToStop = System.currentTimeMillis() + 5000; long currentTimeMillis = System.currentTimeMillis(); while (millisToStop > currentTimeMillis) { currentTimeMillis = System.currentTimeMillis(); } } });19. How can we prevent busy waiting? One way to prevent busy waiting is to put the current thread to sleep for a given amount of time. This can be done by calling the method java.lang.Thread.sleep(long) by passing the number of milliseconds to sleep as an argument. 20. Can we use Thread.sleep() for real-time processing? The number of milliseconds passed to an invocation of Thread.sleep(long) is only an indication for the scheduler how long the current thread does not need to be executed. It may happen that the scheduler lets the thread execute again a few milliseconds earlier or later depending on the actual implementation. Hence an invocation of Thread.sleep() should not be used for real-time processing. 21. How can a thread be woken up that has been put to sleep before using Thread.sleep()? The method interrupt() of java.lang.Thread interrupts a sleeping thread. The interrupted thread that has been put to sleep by calling Thread.sleep() is woken up by an InterruptedException: public class InterruptExample implements Runnable {public void run() { try { Thread.sleep(Long.MAX_VALUE); } catch (InterruptedException e) { System.out.println("["+Thread.currentThread().getName()+"] Interrupted by exception!"); } }public static void main(String[] args) throws InterruptedException { Thread myThread = new Thread(new InterruptExample(), "myThread"); myThread.start();System.out.println("["+Thread.currentThread().getName()+"] Sleeping in main thread for 5s..."); Thread.sleep(5000);System.out.println("["+Thread.currentThread().getName()+"] Interrupting myThread"); myThread.interrupt(); } }22. How can a thread query if it has been interrupted? If the thread is not within a method like Thread.sleep() that would throw an InterruptedException, the thread can query if it has been interrupted by calling either the static method Thread.interrupted() or the method isInterrupted() that it has inherited from java.lang.Thread. 23. How should an InterruptedException be handled? Methods like sleep() and join() throw an InterruptedException to tell the caller that another thread has interrupted this thread. In most cases this is done in order to tell the current thread to stop its current computations and to finish them unexpectedly. Hence ignoring the exception by catching it and only logging it to the console or some log file is often not the appropriate way to handle this kind of exception. The problem with this exception is, that the method run() of the Runnable interface does not allow that run() throws any exceptions. So just rethrowing it does not help. This means the implementation of run() has to handle this checked exception itself and this often leads to the fact that it its caught and ignored. 24. After having started a child thread, how do we wait in the parent thread for the termination of the child thread? Waiting for a thread’s termination is done by invoking the method join() on the thread’s instance variable: Thread thread = new Thread(new Runnable() { @Override public void run() {} }); thread.start(); thread.join();25. What is the output of the following program? public class MyThreads {private static class MyDaemonThread extends Thread {public MyDaemonThread() { setDaemon(true); }@Override public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { } } }public static void main(String[] args) throws InterruptedException { Thread thread = new MyDaemonThread(); thread.start(); thread.join(); System.out.println(thread.isAlive()); } }The output of the above code is “false”. Although the instance of MyDaemonThread is a daemon thread, the invocation of join() causes the main thread to wait until the execution of the daemon thread has finished. Hence calling isAlive() on the thread instance reveals that the daemon thread is no longer running. 26. What happens when an uncaught exception leaves the run() method? I can happen that an unchecked exception escapes from the run() method. In this case the thread is stopped by the Java Virtual Machine. It is possible to catch this exception by registering an instance that implements the interface UncaughtExceptionHandler as an exception handler. This is either done by invoking the static method Thread.setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler), which tells the JVM to use the provided handler in case there was no specific handler registerd on the thread itself, or by invoking setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler) on the thread instance itself. 27. What is a shutdown hook? A shutdown hook is a thread that gets executed when the JVM shuts down. It can be registered by invoking addShutdownHook(Runnable) on the Runtime instance: Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() {} });28. For what purposes is the keyword synchronized used? When you have to implement exclusive access to a resource, like some static value or some file reference, the code that works with the exclusive resource can be embraced with a synchronized block: synchronized (SynchronizedCounter.class) { counter++; }29. What intrinsic lock does a synchronized method acquire? A synchronized method acquires the intrinsic lock for that method’s object and releases it when the method returns. Even if the method throws an exception, the intrinsic lock is released. Hence a synchronized method is equal to the following code: public void method() { synchronized(this) { ... } }30. Can a constructor be synchronized? No, a constructor cannot be synchronized. The reason why this leads to an syntax error is the fact that only the constructing thread should have access to the object being constructed. 31. Can primitive values be used for intrinsic locks? No, primitive values cannot be used for intrinsic locks. 32. Are intrinsic locks reentrant? Yes, intrinsic locks can be accessed by the same thread again and again. Otherwise code that acquires a lock would have to pay attention that it does not accidently tries to acquire a lock it has already acquired. 33. What do we understand by an atomic operation? An atomic operation is an operation that is either executed completely or not at all. 34. Is the statement c++ atomic? No, the incrementation of an integer variable consist of more than one operation. First we have to load the current value of c, increment it and then finally store the new value back. The current thread performing this incrementation may be interrupted in-between any of these three steps, hence this operation is not atomic. 35. What operations are atomic in Java? The Java language provides some basic operations that are atomic and that therefore can be used to make sure that concurrent threads always see the same value:Read and write operations to reference variables and primitive variables (except long and double) Read and write operations for all variables declared as volatile36. Is the following implementation thread-safe? public class DoubleCheckedSingleton { private DoubleCheckedSingleton instance = null;public DoubleCheckedSingleton getInstance() { if(instance == null) { synchronized (DoubleCheckedSingleton.class) { if(instance == null) { instance = new DoubleCheckedSingleton(); } } } return instance; } }The code above is not thread-safe. Although it checks the value of instance once again within the synchronized block (for performance reasons), the JIT compiler can rearrange the bytecode in a way that the reference to instance is set before the constructor has finished its execution. This means the method getInstance() returns an object that may not have been initialized completely. To make the code thread-safe, the keyword volatile can be used since Java 5 for the instance variable. Variables that are marked as volatile get only visible to other threads once the constructor of the object has finished its execution completely. 37. What do we understand by a deadlock? A deadlock is a situation in which two (or more) threads are each waiting on the other thread to free a resource that it has locked, while the thread itself has locked a resource the other thread is waiting on: Thread 1: locks resource A, waits for resource B Thread 2: locks resource B, waits for resource A 38. What are the requirements for a deadlock situation? In general the following requirements for a deadlock can be identified:Mutual exclusion: There is a resource which can be accessed only by one thread at any point in time. Resource holding: While having locked one resource, the thread tries to acquire another lock on some other exclusive resource. No preemption: There is no mechanism, which frees the resource if one thread holds the lock for a specific period of time. Circular wait: During runtime a constellation occurs in which two (or more) threads are each waiting on the other thread to free a resource that it has locked.39. Is it possible to prevent deadlocks at all? In order to prevent deadlocks one (or more) of the requirements for a deadlock has to be eliminated:Mutual exclusion: In some situation it is possible to prevent mutual exclusion by using optimistic locking. Resource holding: A thread may release all its exclusive locks, when it does not succeed in obtaining all exclusive locks. No preemption: Using a timeout for an exclusive lock frees the lock after a given amount of time. Circular wait: When all exclusive locks are obtained by all threads in the same sequence, no circular wait occurs.40. Is it possible to implement a deadlock detection? When all exclusive locks are monitored and modelled as a directed graph, a deadlock detection system can search for two threads that are each waiting on the other thread to free a resource that it has locked. The waiting threads can then be forced by some kind of exception to release the lock the other thread is waiting on. 41. What is a livelock? A livelock is a situation in which two or more threads block each other by responding to an action that is caused by another thread. In contrast to a deadlock situation, where two or more threads wait in one specific state, the threads that participate in a livelock change their state in a way that prevents progress on their regular work. An example would be a situation in which two threads try to acquire two locks, but release a lock they have acquired when they cannot acquire the second lock. It may now happen that both threads concurrently try to acquire the first thread. As only one thread succeeds, the second thread may succeed in acquiring the second lock. Now both threads hold two different locks, but as both want to have both locks, they release their lock and try again from the beginning. This situation may now happen again and again. 42. What do we understand by thread starvation? Threads with lower priority get less time for execution than threads with higher priority. When the threads with lower priority performs a long enduring computations, it may happen that these threads do not get enough time to finish their computations just in time. They seem to “starve” away as threads with higher priority steal them their computation time. 43. Can a synchronized block cause thread starvation? The order in which threads can enter a synchronized block is not defined. So in theory it may happen that in case many threads are waiting for the entrance to a synchronized block, some threads have to wait longer than other threads. Hence they do not get enough computation time to finish their work in time. 44. What do we understand by the term race condition? A race condition describes constellations in which the outcome of some multi-threaded implementation depends on the exact timing behavior of the participating threads. In most cases it is not desirable to have such a kind of behavior, hence the term race condition also means that a bug due to missing thread synchronization leads to the differing outcome. A simple example for a race condition is the incrementation of an integer variable by two concurrent threads. As the operation consists of more than one single and atomic operation, it may happen that both threads read and increment the same value. After this concurrent incrementation the amount of the integer variable is not increased by two but only by one. 45. What do we understand by fair locks? A fair lock takes the waiting time of the threads into account when choosing the next thread that passes the barrier to some exclusive resource. An example implementation of a fair lock is provided by the Java SDK: java.util.concurrent.locks.ReentrantLock. If the constructor with the boolean flag set to true is used, the ReentrantLock grants access to the longest-waiting thread. 46. Which two methods that each object inherits from java.lang.Object can be used to implement a simple producer/consumer scenario? When a worker thread has finished its current task and the queue for new tasks is empty, it can free the processor by acquiring an intrinsic lock on the queue object and by calling the method wait(). The thread will be woken up by some producer thread that has put a new task into the queue and that again acquires the same intrinsic lock on the queue object and calls notify() on it. 47. What is the difference between notify() and notifyAll()? Both methods are used to wake up one or more threads that have put themselves to sleep by calling wait(). While notify() only wakes up one of the waiting threads, notifyAll() wakes up all waiting threads. 48. How it is determined which thread wakes up by calling notify()? It is not specified which threads will be woken up by calling notify() if more than one thread is waiting. Hence code should not rely on any concrete JVM implementation. 49. Is the following code that retrieves an integer value from some queue implementation correct? public Integer getNextInt() { Integer retVal = null; synchronized (queue) { try { while (queue.isEmpty()) { queue.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } } synchronized (queue) { retVal = queue.poll(); if (retVal == null) { System.err.println("retVal is null"); throw new IllegalStateException(); } } return retVal; }Although the code above uses the queue as object monitor, it does not behave correctly in a multi-threaded environment. The reason for this is that it has two separate synchronized blocks. When two threads are woken up in line 6 by another thread that calls notifyAll(), both threads enter one after the other the second synchronized block. It this second block the queue has now only one new value, hence the second thread will poll on an empty queue and get null as return value. 50. Is it possible to check whether a thread holds a monitor lock on some given object? The class java.lang.Thread provides the static method Thread.holdsLock(Object) that returns true if and only if the current thread holds the lock on the object given as argument to the method invocation. 51. What does the method Thread.yield() do? An invocation of the static method Thread.yield() gives the scheduler a hint that the current thread is willing to free the processor. The scheduler is free to ignore this hint. As it is not defined which thread will get the processor after the invocation of Thread.yield(), it may even happen that the current thread becomes the “next” thread to be executed. 52. What do you have to consider when passing object instances from one thread to another? When passing objects between threads, you will have to pay attention that these objects are not manipulated by two threads at the same time. An example would be a Map implementation whose key/value pairs are modified by two concurrent threads. In order to avoid problems with concurrent modifications you can design an object to be immutable. 53. Which rules do you have to follow in order to implement an immutable class?All fields should be final and private. There should be not setter methods. The class itself should be declared final in order to prevent subclasses to violate the principle of immutability. If fields are not of a primitive type but a reference to another object:There should not be a getter method that exposes the reference directly to the caller. Don’t change the referenced objects (or at least changing these references is not visisble to clients of the object).54. What is the purpose of the class java.lang.ThreadLocal? As memory is shared between different threads, ThreadLocal provides a way to store and retrieve values for each thread separately. Implementations of ThreadLocal store and retrieve the values for each thread independently such that when thread A stores the value A1 and thread B stores the value B1 in the same instance of ThreadLocal, thread A later on retrieves value A1 from this ThreadLocal instance and thread B retrieves value B1. 55. What are possible use cases for java.lang.ThreadLocal? Instances of ThreadLocal can be used to transport information throughout the application without the need to pass this from method to method. Examples would be the transportation of security/login information within an instance of ThreadLocal such that it is accessible by each method. Another use case would be to transport transaction information or in general objects that should be accessible in all methods without passing them from method to method. 56. Is it possible to improve the performance of an application by the usage of multi-threading? Name some examples. If we have more than one CPU core available, the performance of an application can be improved by multi-threading if it is possible to parallelize the computations over the available CPU cores. An example would be an application that should scale all images that are stored within a local directory structure. Instead of iterating over all images one after the other, a producer/consumer implementation can use a single thread to scan the directory structure and a bunch of worker threads that perform the actual scaling operation. Another example would be an application that mirrors some web page. Instead of loading one HTML page after the other, a producer thread can parse the first HTML page and issue the links it found into a queue. The worker threads monitor the queue and load the web pages found by the parser. While the worker threads wait for the page to get loaded completely, other threads can use the CPU to parse the already loaded pages and issue new requests. 57. What do we understand by the term scalability? Scalability means the ability of a program to improve the performance by adding further resources to it. 58. Is it possible to compute the theoretical maximum speed up for an application by using multiple processors? Amdahl’s law provides a formula to compute the theoretical maximum speed up by providing multiple processors to an application. The theoretical speedup is computed by S(n) = 1 / (B + (1-B)/n) where n denotes the number of processors and B the fraction of the program that cannot be executed in parallel. When n converges against infinity, the term (1-B)/n converges against zero. Hence the formula can be reduced in this special case to 1/B. As we can see, the theoretical maximum speedup behaves reciprocal to the fraction that has to be executed serially. This means the lower this fraction is, the more theoretical speedup can be achieved. 59. What do we understand by lock contention? Lock contention occurs, when two or more threads are competing in the acquisition of a lock. The scheduler has to decide whether it lets the thread, which has to wait sleeping and performs a context switch to let another thread occupy the CPU, or if letting the waiting thread busy-waiting is more efficient. Both ways introduce idle time to the inferior thread. 60. Which techniques help to reduce lock contention? In some cases lock contention can be reduced by applying one of the following techniques:The scope of the lock is reduced. The number of times a certain lock is acquired is reduced (lock splitting). Using hardware supported optimistic locking operations instead of synchronization. Avoid synchronization where possible. Avoid object pooling.61. Which technique to reduce lock contention can be applied to the following code? synchronized (map) { UUID randomUUID = UUID.randomUUID(); Integer value = Integer.valueOf(42); String key = randomUUID.toString(); map.put(key, value); }The code above performs the computation of the random UUID and the conversion of the literal 42 into an Integer object within the synchronized block, although these two lines of code are local to the current thread and do not affect other threads. Hence they can be moved out of the synchronized block: UUID randomUUID = UUID.randomUUID(); Integer value = Integer.valueOf(42); String key = randomUUID.toString(); synchronized (map) { map.put(key, value); }62. Explain by an example the technique lock splitting. Lock splitting may be a way to reduce lock contention when one lock is used to synchronize access to different aspects of the same application. Suppose we have a class that implements the computation of some statistical data of our application. A first version of this class uses the keyword synchronized in each method signature in order to guard the internal state before corruption by multiple concurrent threads. This also means that each method invocation may cause lock contention as other threads may try to acquire the same lock simultaneously. But it may be possible to split the lock on the object instance into a few smaller locks for each type of statistical data within each method. Hence thread T1 that tries to increment the statistical data D1 does not have to wait for the lock while thread T2 simultaneously updates the data D2. 63. What kind of technique for reducing lock contention is used by the SDK class ReadWriteLock? The SDK class ReadWriteLock uses the fact that concurrent threads do not have to acquire a lock when they want to read a value when no other thread tries to update the value. This is implemented by a pair of locks, one for read-only operations and one for writing operations. While the read-only lock may be obtained by more than one thread, the implementation guarantees that all read operation see an updated value once the write lock is released. 64. What do we understand by lock striping? In contrast to lock splitting, where we introduce different locks for different aspects of the application, lock striping uses multiple locks to guard different parts of the same data structure. An example for this technique is the class ConcurrentHashMap from JDK’s java.util.concurrent package. The Map implementation uses internally different buckets to store its values. The bucket is chosen by the value’s key. ConcurrentHashMap now uses different locks to guard different hash buckets. Hence one thread that tries to access the first hash bucket can acquire the lock for this bucket, while another thread can simultaneously access a second bucket. In contrast to a synchronized version of HashMap this technique can increase the performance when different threads work on different buckets. 65. What do we understand by a CAS operation? CAS stands for compare-and-swap and means that the processor provides a separate instruction that updates the value of a register only if the provided value is equal to the current value. CAS operations can be used to avoid synchronization as the thread can try to update a value by providing its current value and the new value to the CAS operation. If another thread has meanwhile updated the value, the thread’s value is not equal to the current value and the update operation fails. The thread then reads the new value and tries again. That way the necessary synchronization is interchanged by an optimistic spin waiting. 66. Which Java classes use the CAS operation? The SDK classes in the package java.util.concurrent.atomic like AtomicInteger or AtomicBoolean use internally the CAS operation to implement concurrent incrementation. public class CounterAtomic { private AtomicLong counter = new AtomicLong();public void increment() { counter.incrementAndGet(); }public long get() { return counter.get(); } }67. Provide an example why performance improvements for single-threaded applications can cause performance degradation for multi-threaded applications. A prominent example for such optimizations is a List implementation that holds the number of elements as a separate variable. This improves the performance for single-threaded applications as the size() operation does not have to iterate over all elements but can return the current number of elements directly. Within a multi-threaded application the additional counter has to be guarded by a lock as multiple concurrent threads may insert elements into the list. This additional lock can cost performance when there are more updates to the list than invocations of the size() operation. 68. Is object pooling always a performance improvement for multi-threaded applications? Object pools that try to avoid the construction of new objects by pooling them can improve the performance of single-threaded applications as the cost for object creation is interchanged by requesting a new object from the pool. In multi-threaded applications such an object pool has to have synchronized access to the pool and the additional costs of lock contention may outweigh the saved costs of the additional construction and garbage collection of the new objects. Hence object pooling may not always improve the overall performance of a multi-threaded application. 69. What is the relation between the two interfaces Executor and ExecutorService? The interface Executor only defines one method: execute(Runnable). Implementations of this interface will have to execute the given Runnable instance at some time in the future. The ExecutorService interface is an extension of the Executor interface and provides additional methods to shut down the underlying implementation, to await the termination of all submitted tasks and it allows submitting instances of Callable. 70. What happens when you submit() a new task to an ExecutorService instance whose queue is already full? As the method signature of submit() indicates, the ExecutorService implementation is supposed to throw a RejectedExecutionException. 71. What is a ScheduledExecutorService? The interface ScheduledExecutorService extends the interface ExecutorService and adds method that allow to submit new tasks to the underlying implementation that should be executed a given point in time. There are two methods to schedule one-shot tasks and two methods to create and execute periodic tasks. 72. Do you know an easy way to construct a thread pool with 5 threads that executes some tasks that return a value? The SDK provides a factory and utility class Executors whose static method newFixedThreadPool(int nThreads) allows the creation of a thread pool with a fixed number of threads (the implementation of MyCallable is omitted): public static void main(String[] args) throws InterruptedException, ExecutionException { ExecutorService executorService = Executors.newFixedThreadPool(5); Future<Integer>[] futures = new Future[5]; for (int i = 0; i < futures.length; i++) { futures[i] = executorService.submit(new MyCallable()); } for (int i = 0; i < futures.length; i++) { Integer retVal = futures[i].get(); System.out.println(retVal); } executorService.shutdown(); }73. What is the difference between the two interfaces Runnable and Callable? The interface Runnable defines the method run() without any return value whereas the interface Callable allows the method run() to return a value and to throw an exception. 74. Which are use cases for the class java.util.concurrent.Future? Instances of the class java.util.concurrent.Future are used to represent results of asynchronous computations whose result are not immediately available. Hence the class provides methods to check if the asynchronous computation has finished, canceling the task and to the retrieve the actual result. The latter can be done with the two get() methods provided. The first get() methods takes no parameter and blocks until the result is available whereas the second get() method takes a timeout parameter that lets the method invocation return if the result does not get available within the given timeframe. 75. What is the difference between HashMap and Hashtable particularly with regard to thread-safety? The methods of Hashtable are all synchronized. This is not the case for the HashMap implementation. Hence Hashtable is thread-safe whereas HashMap is not thread-safe. For single-threaded applications it is therefore more efficient to use the “newer” HashMap implementation. 76. Is there a simple way to create a synchronized instance of an arbitrary implementation of Collection, List or Map? The utility class Collections provides the methods synchronizedCollection(Collection), synchronizedList(List) and synchronizedMap(Map) that return a thread-safe collection/list/map that is backed by the given instance. 77. What is a semaphore? A semaphore is a data structure that maintains a set of permits that have to be acquired by competing threads. Semaphores can therefore be used to control how many threads access a critical section or resource simultaneously. Hence the constructor of java.util.concurrent.Semaphore takes as first parameter the number of permits the threads compete about. Each invocation of its acquire() methods tries to obtain one of the available permits. The method acquire() without any parameter blocks until the next permit gets available. Later on, when the thread has finished its work on the critical resource, it can release the permit by invoking the method release() on an instance of Semaphore. 78. What is a CountDownLatch? The SDK class CountDownLatch provides a synchronization aid that can be used to implement scenarios in which threads have to wait until some other threads have reached the same state such that all thread can start. This is done by providing a synchronized counter that is decremented until it reaches the value zero. Having reached zero the CountDownLatch instance lets all threads proceed. This can be either used to let all threads start at a given point in time by using the value 1 for the counter or to wait until a number of threads has finished. In the latter case the counter is initialized with the number of threads and each thread that has finished its work counts the latch down by one. 79. What is the difference between a CountDownLatch and a CyclicBarrier? Both SDK classes maintain internally a counter that is decremented by different threads. The threads wait until the internal counter reaches the value zero and proceed from there on. But in contrast to the CountDownLatch the class CyclicBarrier resets the internal value back to the initial value once the value reaches zero. As the name indicates instances of CyclicBarrier can therefore be used to implement use cases where threads have to wait on each other again and again. 80. What kind of tasks can be solved by using the Fork/Join framework? The base class of the Fork/Join Framework java.util.concurrent.ForkJoinPool is basically a thread pool that executes instances of java.util.concurrent.ForkJoinTask. The class ForkJoinTask provides the two methods fork() and join(). While fork() is used to start the asynchronous execution of the task, the method join() is used to await the result of the computation. Hence the Fork/Join framework can be used to implement divide-and-conquer algorithms where a more complex problem is divided into a number of smaller and easier to solve problems. 81. Is it possible to find the smallest number within an array of numbers using the Fork/Join-Framework? The problem of finding the smallest number within an array of numbers can be solved by using a divide-and-conquer algorithm. The smallest problem that can be solved very easily is an array of two numbers as we can determine the smaller of the two numbers directly by one comparison. Using a divide-and-conquer approach the initial array is divided into two parts of equal length and both parts are provided to two instances of RecursiveTask that extend the class ForkJoinTask. By forking the two tasks they get executed and either solve the problem directly, if their slice of the array has the length two, or they again recursively divide the array into two parts and fork two new RecursiveTasks. Finally each task instance returns its result (either by having it computed directly or by waiting for the two subtasks). The root tasks then returns the smallest number in the array. 82. What is the difference between the two classes RecursiveTask and RecursiveAction? In contrast to RecursiveTask the method compute() of RecursiveAction does not have to return a value. Hence RecursiveAction can be used when the action works directly on some data structure without having to return the computed value. 83. Is it possible to perform stream operations in Java 8 with a thread pool? Collections provide the method parallelStream() to create a stream that is processed by a thread pool. Alternatively you can call the intermediate method parallel() on a given stream to convert a sequential stream to a parallel counterpart. 84. How can we access the thread pool that is used by parallel stream operations? The thread pool used for parallel stream operations can be accessed by ForkJoinPool.commonPool(). This way we can query its level of parallelism with commonPool.getParallelism(). The level cannot be changed at runtime but it can be configured by providing the following JVM parameter: -Djava.util.concurrent.ForkJoinPool.common.parallelism=5. Ok, so now you are ready for your interview! Don’t forget to check our FREE Academy course Java Concurrency Essentials! If you enjoyed this, then subscribe to our newsletter to enjoy weekly updates and complimentary whitepapers! Also, check out JCG Academy for more advanced training! You are welcome to contribute with your comments and we will include them in the article! ...
jcg-logo

FREE Programming books with the JCG Newsletter

Here at Java Code Geeks we know how much you love books about programming; we are geeks ourselves. After all, a programmer that respects himself should always have his face in a book, he has to keep up with the latest technologies and developments. For this reason, we have decided to distribute 8 of our books for free. You can get access to them by joining our Newsletter. Additionally, you will also receive weekly news, tips and special offers delivered to your inbox courtesy of Java Code Geeks! The material covers a wide array of topics, from the new Java 8 release to JVM and Android programming. So let’s see what you get in detail!  JPA Mini Book One of the problems of Object Orientation is how to map the objects as the database requires. JPA allows us to work with Java classes as it provides a transparent layer to each database specific details; JPA will do the hard work of mapping table to class structure and semantics for the developer. Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.    JVM Troubleshooting Guide The Java bytecode produced when application are compiled, is eventually executed by the Java Virtual Machine (JVM). The JVM has grown to be a sophisticated tool, but it essentially remains a “black box” for most Java programmers. This is especially true when problems and issues arise from its erroneous use. With this guide, you will delve into the intricacies of the JVM and learn how to perform troubleshooting and problem resolution.  Android UI Design Android is an operating system based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablet computers. Android’s user interface is based on direct manipulation, using touch inputs that loosely correspond to real-world actions, like swiping, tapping, pinching and reverse pinching to manipulate on-screen objects. In this book, you will get a look at the fundamentals of Android UI design. You will understand user input, views and layouts, as well as adapters and fragments. Furthermore, you will learn how to add multimedia to an app and also leverage themes and styles.  Java 8 Features With no doubts, Java 8 release is the greatest thing in the Java world since Java 5 (released quite a while ago, back in 2004). It brings tons of new features to the Java as a language, its compiler, libraries, tools and the JVM (Java virtual machine) itself. In this guide we are going to take a look on all these changes and demonstrate the different usage scenarios on real examples. The tutorial consists of several parts where each one touches the specific side of the platform: language, compiler, libraries, tools, runtime (JVM)  Java Interview Questions In this guide we will discuss about different types of questions that can be used in a Java interview, in order for the employer to test your skills in Java and object-oriented programming in general. In the book’s sections we will discuss about object-oriented programming and its characteristics, general questions regarding Java and its functionality, collections in Java, garbage collectors, exception handling, Java applets, Swing, JDBC, Remote Method Invocation (RMI), Servlets and JSP.    Spring Interview Questions This is a summary of some of the most important questions concerning the Spring Framework, that you may be asked to answer in an interview or in an interview test procedure! There is no need to worry for your next interview test, because Java Code Geeks are here for you! The majority of the things you may be asked is collected in this guide. All core modules, from basic Spring functionality such as Spring Beans, up to Spring MVC framework are presented and described in short.  Java Annotations Tutorial Annotations in Java are a major feature and every Java developer should know how to utilize them. Annotations were introduced in Java in the J2SE update 5 already and the main reason was the need to provide a mechanism that allows programmers to write metadata about their code directly in the code itself. We have provided an abundance of tutorials here at Java Code Geeks and now it is time to gather all the information around Annotations under one reference guide for your reading pleasure!  JUnit Tutorial for Unit Testing A unit can be a function, a class, a package, or a subsystem. So, the term unit testing refers to the practice of testing such small units of your code, so as to ensure that they work as expected. For example, we can test whether an output is what we expected to see given some inputs or if a condition is true or false. The most popular testing framework in Java is JUnit and we have provided plenty of JUnit tutorials. Now, we decided to gather all the JUnit features in one detailed guide for your convenience. We hope you like it!   So, fellow geeks, hop on our newsletter and enjoy our kick-ass books!...
java-interview-questions-answers

Adding Gzip Compression in CXF APIs and Interceptors

Nowadays it has become mandatory to perform Gzipping to the APIs responses, due to huge amount of data we are sending in the response. It saves network bandwidth and delivery time, and of course space over the internet. CXF provides an option to use the Gzip Compression in a number of ways.Blueprint Annotation            Blueprint: <bean id="gZipInterceptor" class="org.apache.cxf.transport.common.gzip.GZIPOutInterceptor" /> <jaxrs:server id="rsServer" address="/gZip"> <jaxrs:outInterceptors> <ref component-id="gZipInterceptor" /> </jaxrs:outInterceptors> </jaxrs:server> Annotation: First you need to register the GZIPOutInterceptor in out interceptors list. For that you need to hook into CXF initialization classes. public class InterceptorManager extends AbstractFeature {private static final Logger LOGGER = Logger.getLogger( "simcore" ); private static final Interceptor< Message > GZIP = new GZIPOutInterceptor(); //private static final Interceptor< Message > GZIP = new GZIPOutInterceptor(512); /* (non-Javadoc) * @see org.apache.cxf.feature.AbstractFeature#initializeProvider(org.apache.cxf.interceptor.InterceptorProvider, org.apache.cxf.Bus) */ @Override protected void initializeProvider( InterceptorProvider provider, Bus bus ) { /** * Adding Gzip interceptor to all outbound requests/responses */ LOGGER.debug( " ############## Adding Gzip as OUT Interceptor ##############" ); provider.getOutInterceptors().add( GZIP ); } } GZIPOutInterceptor comes with an option to set the Threshold value as no of Bytes. If the response size is below this threshold value, then it will not be compressed. This will be extremely useful when we will be sending empty lists and status messages/codes only, since compressing those small responses will be an overhead at server side. But there is another factor we must look into, which is the number of users requesting the response. So, set this value appropriately by thinking over all the cases that might appear. @GZIP Now we can use this annotation on any of our web-services controller to implement compression on all the APIs provided in that class. @WebService @Consumes ( { MediaType.TEXT_PLAIN, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON } ) @Produces ( MediaType.APPLICATION_JSON ) @GZIP public interface WebServicesController {@GET @Path ( "/myGzipData" ) @Produces ( { MediaType.APPLICATION_JSON } ) Response getZipData( );} Moreover we can set different parameters in Gzip annotation. @GZIP ( force = true, threshold = 512 )Reference: Adding Gzip Compression in CXF APIs and Interceptors from our JCG partner Ch Shan Arshad at the Java My G.Friend blog....
java-interview-questions-answers

How To Setup Integration & SOA Tooling For JBoss Developer Studio 8

The release of the latest JBoss Developer Studio (JBDS) brings with it the questions around how to get started with the various JBoss Integration and BPM product tool sets that are not installed out of the box. In this series of articles we will outline for you how to install each set of tools and explain which products they are supporting. This should help you in making an informed decision about what tooling you might want to install before embarking on your next JBoss integration project. There are four different software packs that offer tooling for various JBoss integration products:JBoss Business Process and Rules Development JBoss Integration and SOA DevelopmentJBoss Data Virtualization Development JBoss SOA 5.x DevelopmentThis article will outline how to get started with the JBoss Data Integration & SOA tooling and JBDS 8. JBDS 8 can be obtained through the Customer Portal or via the early access downloads on jboss.org. After installing JBDS, start it up and you will see a welcoming JBoss Central tab with at the bottom a tab to look at the available tool sets labeled Software/Update. You will notice that there are no JBoss Integration stacks offered to install upon first inspection. This is due to, at the time of this writing, the integration stacks being early access.Eventually they will be shown by default once testing finished and they release, but for now you can obtain them by checking the Early Access box in the bottom right corner. This will reveal the integration stack tooling offerings and we will select JBoss Integration and SOA Development. Click on the Install/Update button to start the installation and restart at the end to complete the process. If you are interested in what is being installed, it can be examined by digging into the menu as follows: Help -> Install new software…pull down the menu offered by Work with: select JBoss Developer Studio 8.x – Early Access everything under JBoss Integration and SOA Development will be installed  Stay tuned for more articles in this series that will detail the installation of the remaining JBoss Integration Stack tools.Reference: How To Setup Integration & SOA Tooling For JBoss Developer Studio 8 from our JCG partner Eric Schabell at the Eric Schabell’s blog blog....
java-logo

First Java 8 Experience

Like the rest of the world, I fell deeply in love with Slack. Why? Lot of reasons, but the main one is it provides a new means of really building the SDLC process around communication rather than tools. Which do you think is more common these days, the team of disorganized nitwits meandering around in the wilderness unable to execute despite a brilliant idea and much talent? or the team of hopped up agilists preening with every card event, congratulating themselves endlessly as they hurtle forward on a trip to nowhere? Of course it‘s the latter. I saw some would be philosopher doling out his agile advice. First totemic incantation: focus on the process, not the product. Great advice. What these people are really saying is ‘I don‘t know if what I am doing is ever going to amount to anything, so damn it, I‘m going to feel good about myself for having ticked off the tickets that were assigned to me.‘ There‘s a certain fascism to it: this sense that the horrible fear of chaos has spawned a great desire to clean up the world by installing good old fashioned Order.We have been using YouTrack lately (having abandoned JIRA) and there was no Slack integration so I set about writing one, using Java 8. This too is a whole separate post, but I will pass over this for now, to get to the 8 experience, because there is something important to say about this, that ties into much of my bellyaching about Java, but about stack movements in general: it‘s not only that they end up proceeding at a glacial pace that makes a Stalin 5 year plan look like a nanosecond. No, it gets really special when, after the interminal entracte, the release ends up showing, even partially, like a Potemkin Village. Case in point. So I found in the YouTrack API that to find out what had happened, I had to query an RSS URL. Yeah, that‘s super stupid, and yes, that‘s yet another post. I thought to myself ‘wow, ok, in general, stream processing gets ugly really fast, this should be fun to do this with Streams and Lambdas!‘ Now, I can look back on that shiny faced nube (my prior self) anticipating battle like the turkey about to get stuffed into the machine in that Sarah Palin video. At first, my code was kind of looking pretty nice, but two things jumped out and caused a certain amount of cognitive dissonance:coming up with compact stream/lambda combinations is time-consuming and veers dangerously into oncoming traffic, making it seem vain and suicidal the XML stream tool that‘s part of the ludicrously vast Java API was, alas, still not using Java 8Eventually, I ended up reverting my code and crawling back to 7 because you see, there really is no way to write good code in a language if the stuff you are having to use is not using that language. You get something that looks new and decent, then you have to go stick it onto the rear end of an ass, and the result is a deformed satyr. Furthermore, each time you do a new piece and get a moment of enjoyment, going back taints it. I remember when 5 dropped, pretty instantly, pre-5 code had the smell of death on it that made touching it almost unimaginable. What is the proper reaction to this? Oh, of course, we waited 5 years for this release, but if you thought the whole set of APIs in the JDK would get ported (and arguably, the XML stream processing code is not even core), you are being ridiculous (you being me). Or maybe just that as Grisby says in Lady from Shanghia ‘it‘s a bright, guilty world.‘ In other words, even under the glare of klieg lights, the dusty shadow of sin taints all, so give up the dream of purity. Not sure I‘ve made up my mind, but I do know, hearing 8 promoted as ‘it was time for Java to have lambdas because all the cool kids are doing them,‘ rang in my ears making me think we have reached the Pyrrhic stage of late empire, where everything is just a gesture, a wiggling of the toe to prove that the giant has still beaten death…Reference: First Java 8 Experience from our JCG partner Rob Williams at the Rob Williams’ Blog blog....
jsf-logo

Don’t repeat expressions in facelets

Have you ever seen repeated EL expressions in JSF like this one?                     <h:inputText value="#{oneBean.name}" rendered="#{anotherBean.showPerson}"/> <h:inputText value="#{oneBean.birthday}" rendered="#{anotherBean.showPerson}"/> <h:selectOneMenu value="#{oneBean.children}" style="#{anotherBean.showPerson ? 'display:block' : 'display:none'}"/> usw. Another example: <ui:include src="/include/somesnippet.xhtml"> <ui:param name="age" value="#{someBean.isMan(person) ? 63 : 60}"/> <ui:param name="money" value="#{someBean.isMan(person) and someBean.getCountry(person) eq 'de' ? 1000 : 900}"/> <ui:param name="big" value="#{someBean.getCountry(person) eq 'zh' or someBean.getCountry(person) eq 'ru' ? true : false}"/> </ui:include> Expressions #{anotherBean.showPerson}, #{someBean.isMan(person)}, #{someBean.getCountry(person)} are repeated multiple times. How to optimize them? Well, you can use JSTL’s c:set like this code snippet: <c:set var="showPerson" value="#{anotherBean.showPerson}"/><h:inputText value="#{oneBean.name}" rendered="#{showPerson}"/> <h:inputText value="#{oneBean.birthday}" rendered="#{showPerson}"/> <h:selectOneMenu value="#{oneBean.children}" style="#{showPerson ? 'display:block' : 'display:none'}"/><c:set var="man" value="#{someBean.isMan(person)}"/> <c:set var="country" value="#{someBean.getCountry(person)}"/><ui:include src="/include/somesnippet.xhtml"> <ui:param name="age" value="#{man ? 63 : 60}"/> <ui:param name="money" value="#{man and country eq 'de' ? 1000 : 900}"/> <ui:param name="big" value="#{country eq 'zh' or country eq 'ru' ? true : false}"/> </ui:include> If you are scared about JSTL pitfalls (because you have heard that JSTL is not always JSF friendly!), there is an alternative and simple approach – ui:param. TagHandler ui:param uses JSF’s VariableMapper to save EL expressions in a map. This map maps EL variables on a page and the EL expressions they are associated with. And here you go: <ui:param name="showPerson" value="#{anotherBean.showPerson}"/><h:inputText value="#{oneBean.name}" rendered="#{showPerson}"/> <h:inputText value="#{oneBean.birthday}" rendered="#{showPerson}"/> <h:selectOneMenu value="#{oneBean.children}" style="#{showPerson ? 'display:block' : 'display:none'}"/><ui:param name="man" value="#{someBean.isMan(person)}"/> <ui:param name="country" value="#{someBean.getCountry(person)}"/><ui:include src="/include/somesnippet.xhtml"> <ui:param name="age" value="#{man ? 63 : 60}"/> <ui:param name="money" value="#{man and country eq 'de' ? 1000 : 900}"/> <ui:param name="big" value="#{country eq 'zh' or country eq 'ru' ? true : false}"/> </ui:include> The code is more readable, especially if you have very complex and long expressions. Note: we’re speaking here about readable code and not about performance optimization because JSF TagHandlers don’t evaluate EL expressions.Reference: Don’t repeat expressions in facelets from our JCG partner Oleg Varaksin at the Thoughts on software development blog....
agile-logo

Three Alternatives for Making Smaller Stories

When I was in Israel a couple of weeks ago teaching workshops, one of the big problems people had was large stories. Why was this a problem? If your stories are large, you can’t show progress, and more importantly, you can’t change. For me, the point of agile is the transparency—hey, look at what we’ve done!—and the ability to change. You can change the items in the backlog for the next iteration if you are working in iterations. You can change the project portfolio. You can change the features. But, you can’t change anything if you continue to drag on and on and on for a give feature. You’re not transparent if you keep developing a feature. You become a black hole. Managers start to ask, “What you guys doing? When will you be done? How much will this feature cost?” Do you see where you need to estimate more if the feature is large? Of course, the larger the feature, the more you need to estimate and the more difficult it is to estimate well. Pawel Brodzinski said this quite well last year at the Agile conference, with his favorite estimation scale. Anything other than a size 1 was either too big or the team had no clue. The reason Pawel and I and many other people like very small stories—size of 1—means that you deliver something every day or more often. You have transparency. You don’t invest a ton of work without getting feedback on the work. The people I met a couple of weeks ago felt (and were) stuck. One guy was doing intricate SQL queries. He thought that there was no value until the entire query was done. Nope, that’s where he is incorrect. There is value in interim results. Why? How else would you debug the query? How else would you discover if you had the database set up correctly for product performance? I suggested that every single atomic transaction was a valuable piece. That the way to build small stories was to separate this hairy SQL statement was at the atomic transaction. I bet there are other ways, but that was a good start. He got that aha look, so I am sure he will think of other ways. Another guy was doing algorithm development. Now, I know one issue with algorithm development is you have to keep testing performance or reliability or something else when you do the development. Otherwise, you fall off the deep end. You have an algorithm tuned for one aspect of the system, but not another one. The way I’ve done this in the past is to support algorithm development with a variety of tests. This is the testing continuum from Manage It! Your Guide to Modern, Pragmatic Project Management. See the unit and component testing parts? If you do algorithm development, you need to test each piece of the algorithm—the inner loop, the next outer loop, repeat for each loop—with some sort of unit test, then component test, then as a feature. And, you can do system level testing for the algorithm itself. Back when I tested machine vision systems, I was the system tester for an algorithm we wanted to go “faster.” I created the golden master tests and measured the performance. I gave my tests to the developers. Then, as they changed the inner loops, they created their own unit tests. (No, we were not smart enough to do test-driven development. You can be.) I helped create the component-level tests for the next-level-up tests. We could run each new potential algorithm against the golden master and see if the new algorithm was better or not. I realize that you don’t have a product until everything works. This is like saying in math that you don’t have an answer until you have the finished the entire calculation. And, you are allowed—in fact, I encourage you—to show your interim work. How else can you know if you are making progress? Another participant said that he was special. (Each and every one of you is special. Don’t you know that by now??) He was doing firmware development. I asked if he simulated the firmware before he downloaded to the device. “Of course!” he said. “So, simulate in smaller batches,” I suggested. He got that far-off look. You know that look, the one that says, “Why didn’t I think of that?” He didn’t think of it because it requires changes to their simulator. He’s not an idiot. Their simulator is built for an entire system, not small batches. The simulator assumes waterfall, not agile. They have some technical debt there. Here are the three ways, in case you weren’t clear:Use atomic transactions as a way to show value when you have a big honking transaction. Use tests for each atomic transaction to support your work and understand if you have the correct performance on each transaction. Break apart algorithm development, as in “show your work.” Support your algorithm development with tests, especially if you have loops. Simulate in small batches when you have hardware or firmware. Use tests to support your work.You want to deliver value in your projects. Short stories allow you to do this. Long stories stop your momentum. The longer your project, and the more teams (if you work on a program), the more you need to keep your stories short. Try these alternatives. Do you have other scenarios I haven’t discussed? Ask away in the comments.Reference: Three Alternatives for Making Smaller Stories from our JCG partner Johanna Rothman at the Managing Product Development blog....
docker-logo

Docker: VMs, Code Migration, and SOA Solved

It’s rare that a piece of software as new as Docker is readily adopted by startups along with huge, well established companies. dotCloud, the company that created and maintains Docker, recently nabbed $40 million in funding. Microsoft also announced on 11/18 a Docker CLI for Windows. Docker will also play a central role in Azure as well as the next release of Windows Server. So what is all the hype about? Docker solves two of the most difficult problems in deploying software: painlessly spinning up VMs, and bundling together application code with the deployment environment. Spinning up new, customized instance is as easy as a click of a button. Migrating code between platforms is trivial because our application code is packaged with its environment. We at Keyhole have been seeing a lot of traction around Docker in the past few months. We currently use it in one of our applications to manage our deployment process. The reasons we’ve seen the flurry of activity of it will become clear after I go into what separates Docker from other hypervisors and deployment tools. VMs on Steroids Virtual Machines (VMs) are an amazing tool that has helped further abstract the runtime environment from the physical hardware. VMs, unfortunately, come with a pretty steep performance penalty on startup and execution. The reason for most of the problems in VMs is a duplication of work. To understand this duplication, think of the structure of the Linux operating system. There is a clear separation between the Linux kernel, which manages deep-level tasks like networking and threads, and user space, which is everything outside of the kernel.Traditional VMs like VirtualBox and VMWare run their VMs in the user space. When a traditional VM starts an instance of the machine, it spins up a Linux kernel and user space inside of an existing user space.This is where the duplication comes into play. Why should the Linux kernel be inside of a user space when there is already a Linux kernel for it to use? It doesn’t. That is what the makers of Docker realized. As long as the Linux kernel of the VM matches that of the host machine, there is already a clear separation that the VM user space can take advantage of.When a Docker VM starts up, it attaches the VM user space to the host Linux kernel. This means that boot happens in a manner of milliseconds. The performance is 97% of software running on the host machine. Docker has all of the advantages without any of the drawbacks. Plus… Deployment Solved A Docker VM is generated from a well-defined script called a Dockerfile. The Dockerfile specifies what flavor and version of Linux to use, what software to install, what ports to open, how to pull the source code in, etc. Everything you need is bundled together in one file. This is an example Dockerfile from a project I did a few months ago: FROM ubuntu:12.04 MAINTAINER Zach Gardner <zgardner@keyholesoftware.com># Update apt-get RUN apt-get update# Create container RUN mkdir /container RUN mkdir /container/project# Install NodeJS RUN apt-get --yes install python g++ make checkinstall fakeroot wget RUN src=$(mktemp -d) && cd $src && \ wget -N http://nodejs.org/dist/node-latest.tar.gz && \ tar xzvf node-latest.tar.gz && cd node-v* && \ ./configure && \ fakeroot checkinstall -y --install=no --pkgversion $(echo $(pwd) | sed -n -re"s/.+node-v(.+)$/\1/p") make -j$(($(nproc)+1)) install && \ dpkg -i node_* && \ rm -rf $src# Install NPM RUN apt-get --yes install curl RUN curl --no-check-certificate https://www.npmjs.org/install.sh | sh# Install Bower's dependencies RUN apt-get install --yes git# Install PhantomJS dependencies RUN apt-get install --yes freetype* fontconfig# Move source code to container ADD / /container/project# Install NPM dependencies RUN cd /container/project/ && npm install# Install Project's Bower dependencies RUN cd /container/project && (echo -e "n" | ./node_modules/bower/bin/bower install --allow-root)# Compile code RUN cd /container/project && ./node_modules/grunt-cli/bin/grunt build# Start server CMD /container/project/node_modules/grunt-cli/bin/grunt --gruntfile /container/project/Gruntfile.js prod The first thing I do in this script is define that I’m using Ubuntu 12.04. I install NodeJS, NPM, and git. I copy my source code from my repository, download the runtime dependencies, compile my code, and start my server. When you pass a Dockerfile to Docker, it generates a Docker image. The best way to think of a Docker image is that it is a self-contained zip file that contains everything an application needs to run.The source code and execution environment being combined is a complete paradigm shift from traditional deployment methods. Rather than moving code, having a human execute shell scripts to update the environment, and wishing for the best, you can instead transition fresh Docker images to the different platforms. There is virtually no human intervention required, which reduces the chance of mistakes. Best of all is that once the QA’s have signed off on a specific version of the application, you can be sure that application won’t change as you migrate it up platforms. Interestingly, the Docker paradigm of transferring Docker images falls inline with some of the research that’s been done in Reactive programming. Managing state is one of the most difficult things to do in an application. Mutability makes creating thread-safe code a non-trivial job. By shifting the thinking over to process immutable pieces of data, the CPU is allowed to optimize threads and operations in a much easier manner than before. Docker follows that paradigm by getting rid of the concept of software updates on platforms. It is much easier to scrap the running Docker image and replace it with a new one than to worry about how to upgrade the current running image. No scripts are necessary to upgrade things like Java if the version is out of date, or other worrisome things like that. Docker takes the guesswork out of what software is running on your platforms: it’s what ever the developer specified in the Dockerfile. Migrating the image across platforms is a trivial job. Docker images can be pushed to a Docker registry (public or private), and pulled down onto the desired platform. The syntax is very similar to git: On a development platformdocker push zgardner/myapp On a production platform docker pull zgardner/myapp docker run -i -t zgardner/myapp In the example above, I first pushed myapp up to a Docker registry. I then pull it down from a higher platform, and run it.The term for a running Docker image is a Docker container. There are some steps that I omitted to show the shutting down of the existing container, how to specify the port on which the Docker container should communicate through, etc. Those are things though that the organization that uses Docker can specify for themselves. The idea of being able to migrate code so easily is a welcome revolution compared to every in-house, proprietary, home-brewed solution I’ve ever seen. Combining the power of a Linux-kernel attached VM with a simplified migration process has some pretty powerful ramifications. We at Keyhole have been experimenting with CoreOS and Fleet to deploy new servers, set them up with Docker, and download Docker images all from the Amazon AWS console. We’re also starting to experiment with… Service Oriented Architecture: Easy as Sliced Bread Docker is the first true DevOps tool. It allows developers to easily specify the environment in which their code should be executed. It also removes the stress of worrying about upgrading the environment. Docker images being static containers means they need to offload persistent data outside of the application itself. This is commonly done by mounting an AWS drive when defining the Docker container. This also means that the code contained inside of the application needs to be small, concise, and very focused. Because the application will run inside of an isolated environment, it needs to be defined as if it can be ran on an island by itself. That tends to lend itself very well with a SOA (Service Oriented Architecture). A SOA is a different way of thinking about an API and how applications in general are composed. The traditional way of thinking of an application is that it is a system composed of smaller technical parts. These technical parts may be things like Products or Customers or Funds or cron jobs. Some of these technical parts may needed in other applications that a software company has written. The traditional approach is to either try sharing the code, which often doesn’t work because it was written with only the original application in mind, or copying the code all together, which really doesn’t work. An application written with a SOA in mind is designed with a completely different goal in mind. In a SOA, applications are pieced together by composing business needs together with application code. Some of the technical parts mentioned above are actually business needs. These tend to be the same across applications, though they may be used in different ways depending upon the UI. If each of these business needs can be siloed into a well-defined, consistent interface, they are reusable by design.Making a SOA a primary focus of an organization allows new applications to be pieced together quickly and effectively. Amazon was one of the first companies to pioneer this approach. SOA is catching on with a lot of other small and large companies because it works so well. Docker lends itself very nicely to a SOA. Each service can be conceived of as a separate Dockerfile. Migrating the services across platforms is as easy as pushing a static Docker image and pulling it down. They are isolated by their very nature of running in an independent VM. Using API documentation tools like Swagger can help make the service even more well defined. Docker is also used by companies like Netflix to implement micro-services. These services are different than traditional services in a SOA by their scope. Traditional services have a lot going on, and are often difficult to isolate and contain. Micro-services focus on extremely small, reusable components that have as little knowledge of their environment as possible. The isolation that Docker gives works well with enforcing tiny micro-services that can be deployed anywhere. At Keyhole, we’re working to rewrite our Q&A system along with our Timesheet application to use a SOA with Docker. The results have been very promising so far. We have future blog posts that will detail different things we’ve found out and experienced during this process. To Wrap It All Up Docker is a very powerful tool that we believe will be an industry standard within the next few years. It flips the VM and application migration process on its head. It will be exciting for us to help clients implement it in their stack, and see serious savings in time and energy. Docker will allow developers and enterprise system engineers to stop worrying about build and deployment issues, and instead focus on what matters: building beautiful applications.Reference: Docker: VMs, Code Migration, and SOA Solved from our JCG partner Zach Gardner at the Keyhole Software blog....
software-development-2-logo

Use MySQL’s Strict Mode on all new Projects!

MySQL is a database that has been bending the SQL standard in ways that make it hard to move off MySQL. What may appear to be a clever technique for vendor lockin (or maybe just oversight of the standard) can be quite annoying in understanding the real meaning of the SQL language. One such example is MySQL’s interpretation of how GROUP BY works. In MySQL, unlike any other database, you can put arbitrary expressions into your SELECT clause, even if they do not have a formal dependency on the GROUP BY expression. For instance:     SELECT employer, first_name, last_name FROM employees GROUP BY employer This will work in MySQL, but what does it mean? If we only have one resulting record per employer, which one of the employees will be returned? The semantics of the above query is really this one: SELECT employer, ARBITRARY(first_name), ARBITRARY(last_name) FROM employees GROUP BY employer If we assume that there is such an aggregation function as ARBITRARY(). Some may claim that this can be used for some clever performance “optimisation”. I say: Don’t. This is so weakly specified, it is not even clear if the two references of this pseudo-ARBITRARY() aggregate function will produce values from the same record. Just look at the number of Stack Overflow questions that evolve around the “not a GROUP BY expression” error:http://stackoverflow.com/q/26680353/521799 http://stackoverflow.com/q/25103756/521799 http://stackoverflow.com/q/17746590/521799 http://stackoverflow.com/q/5587648/521799 http://stackoverflow.com/q/5463838/521799 many more…I’m sure that parts of this damage that has been caused to a generation of SQL developers is due to the fact that this works in some databases. ONLY_FULL_GROUP_BY But there is a flag in MySQL called ONLY_FULL_GROUP_BY, and Morgan Tocker, the MySQL community manager suggests eventually turning it on by default. MySQL community members tend to agree that this is a good decision in the long run. While it is certainly very hard to turn this flag on for a legacy application, all new applications built on top of MySQL should make sure to turn on this flag. In fact, new applications should even consider turning on “strict SQL mode” entirely, to make sure they get a better, more modern SQL experience. For more information about MySQL server modes, please consider the manual: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.htmlReference: Use MySQL’s Strict Mode on all new Projects! from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog....
agile-logo

Different Ways of Scaling Agile

At this year’s Construx Software Executive Summit one of the problems that we explored was how to scale software development, especially Agile development, across projects, portfolios, geographies and enterprises. As part of this, we looked at 3 different popular methods for scaling Agile: LeSS (Large Scale Scrum), SAFe (Scaled Agile Framework), and DAD (Disciplined Agile Delivery). LeSS and LeSS Huge – Large Scale Scrum Craig Larman, the co-author of LeSS (and LeSS Huge – for really big programs), started off by criticizing the “contract game” or “commitment game” that management, developers and customers traditionally play to shift blame upfront for when things (inevitably) go wrong on a project. It was provocative and entertaining, but it had little to do with scaling Agile. He spent the rest of his time building the case for restructuring organizations around end-to-end cross-functional feature teams who deliver working code rather than specialist component teams and functional groups or matrices. Feature teams can move faster by sharing code and knowledge, solving problems together and minmizing handoffs and delays. Enterprise architecture in LeSS seems easy. Every team member is a developer – and every developer is an architect. Architects work together outside of teams and projects in voluntary Communities of Practice to collaborate and shape the organization’s architecture together. This sounds good – but architecture, especially in large enterprise environments, is too important to try and manage out-of-band. LeSS doesn’t explain how eliminating specialization and working without upfront architecture definition and architectural standards and oversight will help build big systems that work with other big systems. LeSS is supposed to be about scaling up, but most of what LeSS lays out looks like Scrum done by lots of people at the same time. It’s not clear where Scrum ends and LeSS starts. SAFe – Scaled Agile Framework There’s no place for management in LeSS (except for Product Owners, who are the key constraint for success – like in Scrum). Implementing Less involves fundamentally restructuring your organization around business-driven programs and getting rid of managers and specialists. Managers (as well as architects and other specialists) do have a role in SAFe’s Scaled Agile Framework – a more detailed and heavyweight method that borrows from Lean, Agile and sequential Waterfall development approaches. Teams following Scrum (and some XP technical practices) to build working code roll up into programs and portfolios, which need to be managed and coordinated. In fact, there is so much for managers to do in SAFe as “Lean-Agile Leaders” that Dean Leffingwell spent most of his time enumerating and elaborating the roles and responsibilities of managers in scaling Agile programs and leading change. Some of the points that stuck with me:The easiest way to change culture is to have success. Focus on execution, not culture, and change will follow. From Deming: Only managers can change the system – because managers create systems. Change needs to come from the middle. Managers need to find ways to push decisions down to teams and individuals, giving them strong and clear “decision filters” so that they understand how to make their own decisions.DAD – Disciplined Agile Delivery Scott Ambler doesn’t believe that there is one way to scale Agile development, because in an enterprise different teams and projects will deliver different kinds of software in different ways: some may be following Scrum or XP, or Kanban, or Lean Startup with Continuous Deployment, or RUP, or SAFe, or a sequential Waterfall approach (whether they have good reasons, or not so good reasons, for working the way that they do). Disciplined Agile Development (DAD) is not a software development method or project management framework – it is a decision-making framework that looks at how to plan, build and run systems across the enterprise. DAD layers over Scrum/XP, Lean/Kanban or other lifeycles, helping managers make decisions about how to manage projects, how to manage risks, and how to drive change. Projects, and people working in projects, need to be enterprise-aware – they need to work within the constraints of the organization, follow standards, satisfy compliance, integrate with legacy systems and with other projects and programs, and leverage shared resources and expertise and other assets across the organization. Development isn’t the biggest problem in scaling Agile. Changes need to be made in many different parts of the organization in order to move faster: governance (including the PMO), procurement, finance, compliance, legal, product management, data management, ops, … and these changes can take a long time. In Disciplined Agile Development, this isn’t easy, and it’s not exciting. It just has to be done. Scaling Agile is Hard, but it’s worth it Almost all of us agreed with Dean Leffingwell that “nothing beats Agile at the team level”. But achieving the same level of success at the organizational level is a hard problem. So hard that none of the people who are supposed to be experts at it could clearly explain how to do it. After talking to senior managers from many different industries and different countries, I learned that most organizations seem to be finding their own way, blending sequential Waterfall stage-gate development and large-scale program management practices at the enterprise-level with Agile at the team level. Using Agile approaches to explore ideas and requirements, prototyping and technical spikes to help understand viability and scope and technical needs and risks early, before chartering projects. Starting off these projects with planning and enough analysis and modeling upfront to identify key dependencies and integration points, then getting Agile teams to fill in the details and deliver working software in increments. Managing these projects like any other projects, but with more transparency into the real state of software development – because you get working software instead of status reports. The major advantage of Agile at scale isn’t the ability to react to continuous changes or even to deliver faster or cheaper. It’s knowing sooner whether you should keep going, or if you need to keep going, or if you should stop and do something else instead.Reference: Different Ways of Scaling Agile from our JCG partner Jim Bird at the Building Real Software blog....
javafx-logo

Call to verify responsiveness of your JavaFX UIs

Recently Jim Weaver installed my little picture indexing app “picmodo” for a demo on his Surface Pro and the GUI became a pice of junk. Obviously the basic font size of JavaFX on a Windows Tablet is to high:                I assume, too absolute sizes and positions are not always the best idea, even if the resize behaviour works like expected and the UI it looks quite ok on the developers machine… ! So I suppose to frequently give it a try by simply adding this to your CSS to increase default font size a bit and change the button sizes to verify the responsiveness of your layouts:.root{ -fx-font-size: 16pt; } .button, .toggle-button, .choice-box { -fx-padding: 10px 20px 10px 20px; }Somehow that forced me to modify some things. So basically I now prefer to use a combination of VBox, HBox and Region(s) (as a kind of spring-delimiter) for the major layout and to give a bit more respect to “USE_COMPUTED_SIZE” of controls and panes …Reference: Call to verify responsiveness of your JavaFX UIs from our JCG partner Jens Deters at the JavaFX Delight blog....
docker-logo

Continuous Deployment with Java EE 7, WildFly, and Docker – (Hanginar #1)

This blog is starting a new hanginar (G+ hangout + webinar) series that will highlight solutions, frameworks, application servers, tooling, deployment, and more content focused on Java EE. These are not the usual conference-style monologue presentations, but are interactive hackathons where real working stuff is shown, and is mostly code-driven. Think of this as a mix of, and inspired by, Nighthacking (@_nighthacking), Virtual JUG (@virtualjug), and virtual JBUG (@vjbug) but focussing purely on Java EE technology. There are so many cool things happening in the Java EE platform and ecosystem around it, and they need to be shared with the broader community, more importantly at a location where people can go back again and again. Voxxed.com has graciously offered to host all the videos and be the central place for this content. The first such webinar, with none other than Adam Bien (@adambien), in that series just went live. It discusses how to do Continuous Deployment with Java EE 7 and Docker. It will also show how to go from “git push” to production in less than a minute, including rebooting your Docker containers and restarting all your microservices.A tentative list of speakers is identified at github.com/javaee-samples/webinars. Each speaker is assigned an issue which allows you to ask questions. Feel free to file an issue for any other speaker that should be on the list. What would you like to see ? Spec leads ? App servers ? Why this over that ? Design patterns and anti-patterns ? Anonymous customer use cases ? What frequency would you like to see ? Use G+ hangout on air ? As with any new effort, we’ll learn and evolve and see what makes best sense for the Java EE community. So what’s the mantra ? Code is king, give some love to Java EE!Reference: Continuous Deployment with Java EE 7, WildFly, and Docker – (Hanginar #1) from our JCG partner Arun Gupta at the Miles to go 2.0 … blog....
Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close