Wednesday, March 27, 2024

Tuesday, April 25, 2017

Installing angular and node.js

This is painful to install angular on ubuntu. I'd like to do it in a fancy way using npm tool. The instruction says that there are ony a few things to do:

install nodejs
install npm
install angular via npm

Okay.... I'm starting with apt-get update, apt-get install nodejs, apt-get install npm, .
And then I see versions conflicts, unexpected errors on creating angular project etc.

I had to uninstall all the packages and start from the very beggining,
Fortunately I found a nice explanation how to install it:

https://www.digitalocean.com/community/tutorials/node-js-ubuntu-14-04-ru

It's better to skip the first section and just consequentely execute commands from section 'Установка при помощи PPA'.

After you got npm installed, just run npm install -g @angular/cli

I hope your time and nerves will be saved

Wednesday, October 26, 2016

Friday, November 6, 2015

A few words about java 8 performance, sorting complexity and leetcode task


There is an easy leetcode task:

Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.
And the function signature is 
public boolean isAnagram(String s, String t) {
}
At the first glance it could be solved with two algorithms.
The first algorithm:
1. convert input strings to char arrays
2. sort char arrays
3. compare arrays symbol-by-symbol, if they are not identical return false, otherwise we have valid anagram and must return true
Coplexity of the first algorithm equals to complexety of sorting char array which "offers O(n log(n)) performance" 

The second algorithm:
1. Iterate through all symbols of the 's' string and put each symbol to the map where key is the symbol itself and the value is the counter (if we met a symbol we need to increment the counter)
2. Iterate through all symbols of the 't' string and decrement the counter for each symbol in the map 
3. Iterate through all map values and check if counter equals '0' for every symbol (map key), it means that we got valid anagram
Complexity of the second algorithm equals to complexity of iterating through the arrays and symbol-counter map which is no more than O(n).
Looks like the second solution should work faster but leetcode checker engine doesn't think so...

Tuesday, March 31, 2015

Concurrent collections: CopyOnWriteArrayList


Working with collections in a multi-thread application is a challenge. Just imagine the list which is accessible by a few threads and every thread is seeking for a chance to change the list data, that’s a typical showcase of the ConcurrentModificationException. In order to get rid of this and other problems JDK (since version 1.5) provides improved mechanisms for storing ‘Iterable’ data in multithread application.
This article is an overview of widely used concurrent collection such as CopyOnWriteArrayList

The name of the collection is very straightforward, every ‘write’ operation (add, remove, set) causes the copying and creation of the modified collection. It allows us to prevent ConcurrentModificationException as long as every thread’s iterator will have its own copy of the collection. The official Oracle documentation names such iterators as “snapshot” style iterator; this iterator “uses a reference to the state of the array at the point that the iterator was created”

This is the add operation for CopyOnWriteArrayList


public boolean add(E e) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] elements = getArray();
        int len = elements.length;
        Object[] newElements = Arrays.copyOf(elements, len + 1);
        newElements[len] = e;
        setArray(newElements);
        return true;
    } finally {
        lock.unlock();
    }
}

Every time you modify collection the new copy of the whole collection is created – it’s quite expensive operation and that’s why it’s not recommended to use CopyOnWriteArrayList for frequently modified data. Another interesting method from the listing is setArray(newElements); it updates actual array and every new thread’s iterator will have updated version of the array – other thread iterators (which run in parallel) won’t be affected using their own local copies of the array.
Question: two or more threads modify the CopyOnArrayList simultaneously, what will be the result of their job?

Saturday, March 28, 2015

What is ThreadLocal?

Take a look on the following piece of code:

private int var;

public synchronized void changeVar() {
 var++;
}

Using the synchronized block we define that the var is available only for a thread which captures the monitor/lock of the synchronized block. For example, thread-A changes the value of var and leaves the block (releases monitor), after that thread-B captures the monitor and modifies the value of var changed by thread-A, var gets value 2. That’s a normal and expected behavior, but sometimes we need to have our variables with thread visibility scope.

What I mean is having var independently modified by thread-A and thread-B, if the thread-A calls changeVar method 10 times then var will have value 10 only for thread-A, thread-B may have var equals 0 if the thread has not call changeVar method; to do that we may use ThreadLocal (http://docs.oracle.com/javase/7/docs/api/java/lang/ThreadLocal.html) instance.

Monday, March 23, 2015

What is Future?



"The future depends on what you do today" M. Gandhi



Future is java interface: public interface Future<V>
A Future represents the result of an asynchronous computation.
Methods of Future interface are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.  The class that implements Future might look as follows


  public class FutureImpl implements Future<String> {   

    public boolean cancel(boolean mayInterruptIfRunning) {      
      return false;
    }

    public boolean isCancelled() {      
      return false;
    }

    public boolean isDone() {     
      return false;
    }

    public String get() throws InterruptedException, ExecutionException {     
      return null;
    }

    public String get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException,
        TimeoutException {      
      return null;
    }       
  }
 

Methods of Future:
cancel() - attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason.
isDone() - Returns true if this task completed. Completion may be due to normal termination, an exception, or cancellation -- in all of these cases, this method will return true
get() - Waits if necessary for the computation to complete, and then retrieves its result.
get(long timeout, TimeUnit unit) - Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available

Thread pool and executors

In order to prevent overhead of thread creation and reuse existed threads Java supports thread pools since version 1.5. The mechanism of thread pool is based on the equally called pattern. Roughly speaking, thread pool is a queue of initialized threads which makes its usage less expensive neither than using classic single Thread approach.
Java Virtual Machine provides fast access to the queue and every thread has its own instruction to be performed; instructions are customized with objects that implement Callable or Runnable.

Instead of creating threads directly with operations like Thread th = new Thread(); you may use instance of Executor interface.


Executor executor = new Executor() {
    public void execute(Runnable command) {
        command.run();
     }
};
executor.execute(someRunnableInstance);


Executor uses already existed thread and makes the thread to perform operations defined at someRunnableInstance.
ExecutorService (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ExecutorService.html) extends Executor interface within new very useful methods. If you’d like to have custom class which implements ExecutorService you must override about 13 methods (jdk 1.8.0.31)
Let’s take a look on a few of them

  •  Future<?> submit(Runnable task), <T> Future<T> submit(Callable<T> task) – submits task to be executed.
  • void shutdown() – initiates the shutdown of the submitted task (no guarantee that the task will be stopped)

Saturday, March 21, 2015

Deadlock

Chip and Dale must save Zipper and beat Fat Cat then but they cannot decide who is responsible for saving Zipper and who is responsible for beating their enemy.


public class Deadlock {
  
 public void init() {
  
  RescueRangers chip = new RescueRangers();
  RescueRangers dale = new RescueRangers();
  chip.setMate(dale);
  dale.setMate(chip);
  
  Thread t1 = new Thread(chip, "Chip");
  Thread t2 = new Thread(dale, "Dale");
  t1.start();  
  t2.start();
 }  
 
 class RescueRangers implements Runnable {
  
  private RescueRangers mate;
  
  public void setMate(RescueRangers mate) {
   this.mate = mate;
  }
  
  public synchronized void saveZipper() {
   System.out.println("Saving Zipper " + Thread.currentThread().getName());
   mate.attackFatCat();   
  }
  
  public synchronized void attackFatCat() {
   System.out.println("Attack Fat Cat " + Thread.currentThread().getName());   
  }
  
  public void run() {
   saveZipper();
  }
 } 
}


Once you launch this code you will see that Chip and Dale cannot attack Fat Cat because they are stuck saving Zipper – it is Fat Cat’s trap.

Output:
Saving Zipper Chip
Saving Zipper Dale

Chip and Dale wait for each other saving Zipper, neither can proceed attacking Fat Cat until the other does first, so both are stuck.

Synchronization block and atomic operations

When two or more threads modify some single object state we can get unexpected result. For example, we expect to get the value of object modified by Thread-A, but we are getting some other unexpected value because object was also modified by Thread-B.


class MyObject {
 private String val;

 public String getVal() {
  return val;
 }

 public void setVal(String val) {
  this.val = val;
  for (int iter = 0; iter < 1000; ++iter) {
   String str = "Useless Operation";
   str += iter;
  }
  System.out.println(this.val);  
 }
  
}


Thread-A changed the state of the object using the setVal, then Thread-B also changed the object state and Thread-A will output the value that has been set by Thread-B. That’s the code snippet of threads initialization


Thread thread_a = new Thread(new Runnable() {

 @Override
 public void run() {
  obj.setVal("1");

 }
});

Thread thread_b = new Thread(new Runnable() {

 @Override
 public void run() {
  obj.setVal("9");

 }
});
thread_a.start();
thread_b.start();

The output is going to be “9”, “9”. Surely, if you removed the useless cycle from setVal then you would get proper results because the value would be printed by Thread-A faster than the Thread-B changes it. Another option to prevent this behavior is using synchronized keyword.

Friday, March 20, 2015

What is Thread?

Thread in java is:
1) an instance of java.lang.Thread
2) a thread of execution – some kind of “lightweight” process which has its own call stack
To initialize the instance of Thread it’s enough to use the default Thread constructor
Thread t = new Thread();
This instruction is useless, because thread does not have any functionality. Another Thread’s constructor which takes an instance of java.lang.Runnable interface allows us to bring some functionality to our thread.

Thread t = new Thread(new Runnable() {

 @Override
 public void run() {
   System.out.println("Hey from the thread");
 }
});

Note: as long as any instance of java.lang.Thread implements java.lang.Runnable we can pass any thread instance as a constructor parameter of other thread. To run our thread we need to invoke start 
method:

t.start();
Then we will see ‘Hey from the thread’ console message. 
Don’t be fooled by Thread’s run() method – it doesn’t launch your thread instructions for asynchronous execution but it in the same thread where this method has been invoked.
There are other Threads frequently used methods such as: sleep [static], yield [static], join. In order to remember what do they all need for it’d be better to take a look on a figure with possible thread states: 



Thursday, March 19, 2015

Apache Spark checkpoint issue on windows

"To keep track of the log statistics for all of time, state must be maintained between processing RDD's in a DStream.

To maintain state for key-pair values, the data may be too big to fit in memory on one machine - Spark Streaming can maintain the state for you. To do that, call the updateStateByKey function of the Spark Streaming library.

First, in order to use updateStateByKey, checkpointing must be enabled on the streaming context. To do that, just call checkpoint on the streaming context with a directory to write the checkpoint data." (from http://databricks.gitbooks.io/databricks-spark-reference-applications/content/logs_analyzer/chapter1/total.html)

When you enable checkpointing for your streaming context with ssc.checkpoint(<PATH_TO_DIRECTORY>); you may get the error messages

java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
...
Exception in thread "pool-8-thread-1" java.lang.NullPointerException
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1010)
at org.apache.hadoop.util.Shell.runCommand(Shell.java:404)


Also, looking inside checkpoint directory you can find some files created at the moment of the stream processing, however these files are empty. These files store state information (act as checkpoint) without this data we cannot use updateStateByKey properly.

To solve this issue you need:

Thursday, January 22, 2015

Regular Expression for Timestamp value

Given string: 2012-02-10 00:00:00
Task: Check if it matches java.sql.Timestamp format.
Solution: use the regular expression
((1\d{3})|(20\d{2}))-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(3[0-1])) (([0-1]\d)|(2[0-3])):([0-5]\d):([0-5]\d)|(^$)

Java way:
String pattern = "(((1\\d{3})|(20\\d{2}))-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\\d)|(3[0-1])) (([0-1]\\d)|(2[0-3])):([0-5]\\d):([0-5]\\d))|(^$)";

NOTE: the regular expression above allows empty values as well, remove |(^$) from the end of the expression to get rid of it.

PS: if you'd like to know more about regular expressions and play with it go to regexpal.com

Wednesday, November 12, 2014

Make a List

Just to summarize all the ways of list creation in Java.
Taken from: http://stackoverflow.com/questions/858572/how-to-make-a-new-list-in-java


JDK

1. List listA = new ArrayList<string>();
2. List listB = Arrays.asList("A", "B", "C")

Guava

1. List names = Lists.newArrayList("Mike", "John", "Lesly");
2. List chars = Lists.asList("A","B", new String [] {"C", "D"});

Immutable List

1. Collections.unmodifiableList(new ArrayList<string>(Arrays.asList("A","B")));
2. ImmutableList.builder()                                      // Guava
            .add("A")
            .add("B").build();
3. ImmutableList.of("A", "B");                                  // Guava
4. ImmutableList.copyOf(Lists.newArrayList("A", "B", "C"));     // Guava

Empty immutable List

1. Collections.emptyList();
2. Collections.EMPTY_LIST;

List of Characters

1. Lists.charactersOf("String")                                 // Guava
2. Lists.newArrayList(Splitter.fixedLength(1).split("String"))  // Guava

List of Integers

Ints.asList(1,2,3);                                             // Guava

A little java collections memo:

Tuesday, October 28, 2014

Initialize object with Java reflection mechanism

Java 7 has an improved way of working with object's reflection in comparison with older versions.
For example, at version 6 in order to initialize object and its fields we need.
1) initialize class object instance using type name of the target object
2) if some object's methods (for example 'setXxx') should be invoked, need to find it and pass corresponding parameters - be aware of parameter type, it must me consistent with original method signature.


Object param = new SomeType(); // SomeType must be included in a signature of the target method 
String type = "com.vbashur.MyType"
Class<?> clazz = Class.forName(type);
Object paramObject = clazz.newInstance();
String setterName = "setMyValue"

for (Method m : clazz.getDeclaredMethods()) { // if parameters are known, use getDeclaredMethod
    if (m.getName().equals(setterName)) {
        m.invoke(paramObject, param);
        break;
    }
}

The piece of code above may throw a bunch of ugly exceptions (llegalAccessException,
IllegalArgumentException, InvocationTargetException, InstantiationException, ClassNotFoundException) and works quite slow. In order to access private method it requires Method.setAccessible() to be invoked.

MethodHandle is a Java-7-way

1) What we need to do is to declare MethodType object firstly. A MethodType is an immutable object that represents the type signature of a method.

Friday, October 3, 2014

Merge Sort in Java (+JavaScript)

Merge sort algorithm has a worst-case performance of O(n log n) - that's a good performance of comparison-based sorting algorithm, moreover this algorithm is easy to understand, remember and repeat after while.
For implementation I'm going to use mergeSort method with the following signature:
<T extends Comparable<T>> void mergeSort(T[] arrayToSort, T[]resArray)
arrayToSort - array to be sorted
resArray - empty array with the same length as arrayToSort which will have all arrayToSort's items in a sorted order

In step one of merge sort we need to copy the content of arrayToSort into resArray and specify array's start and end indicies.


int lo = 0;
int hi = arrayToSort.length - 1;
for (int iter = 0; iter < arrayToSort.length; ++iter) {
 resArray[iter] = arrayToSort[iter];
} 

Now it's time to split an array in a central element and get two subarrays
int mid = lo + (hi - lo) / 2; // this is a central element index of array
1-st subarray elements will be from lo to mid indicies
2-nd subarray elements will be from mid + 1 to hi indicies


The following function can break up into subarrays recursively:

<T extends Comparable<T>> void sortMerge(T[] arrayToSort, T[] resArray, int lo, int hi) {
    if (lo >= hi)
  return;
 int mid = lo + (hi - lo) / 2;
 sortMerge(arrayToSort, resArray, lo, mid);
 sortMerge(arrayToSort, resArray, mid + 1, hi);
 //merge(arrayToSort, resArray, lo, mid, hi);
}

Thursday, September 18, 2014

Java + Vaadin + Spring: creating a basis for application's user interface. From the very beginning.

This article shows the way of creating java application with Vaadin user interface and Spring framework features.

Create a Vaadin application is very easy if you have installed corresponding eclipse plugin. Read http://vaadin.com/eclipse to get to know how to install the plugin and create vaadin application in a few mouse clicks.

Another way of creating vaadin application quickly is using Vaadin "application" archetype. Type the following string in the command line once you're getting into workspace directory:

mvn archetype:generate -DarchetypeGroupId=com.vaadin -DarchetypeArtifactId=vaadin-archetype-application -DarchetypeVersion=7.1.8

NOTE: if you got an error : "The desired archetype does not exist (com.vaadin:vaadin-archetype-application:7.1.8)" execute this command: mvn archetype:update-local-catalog

I'd like to show old-school way of Vaadin integration into a simple web application. There are two solutions. Second solution I find more attractive, however it cannot be applied to Vaadin 6. Read more for details.

Thursday, July 31, 2014

Spring MVC, OSGI bundles and forwarding requests

I have a post in this blog with example of using OSGI servlet bridge and embedded OSGI framework. Now it's time to extend it.
Let's play with OSGI combining it with Spring MVC application.



With stackoverflow you can find some links to samples and explanations about Sprng MVC and OSGI
http://stackoverflow.com/questions/12832697/looking-for-an-osgi-with-spring-specifically-spring-mvc-tutorial
http://stackoverflow.com/questions/12331722/osgi-spring-mvc-bundle-nightmare-java-lang-classnotfoundexception-org-springf

In this article I'd like to show how does spring MVC application may use OSGI servlet-bundles.
1. My spring MVC application is going to be the main application with a fundamental business logic and it's going to be a bridge for bundles
2. I'd like to have a special bundle with a logic that is common for osgi bundles installed. Some kind of super bundle with general functionality.
3. Other bundles have it's logic implemented inside and also could interact with general bundle and main spring application forwarding its queries.

Step-by-step

Thursday, July 17, 2014

Simple jUnit which compares two files

I have a piece of code that generates some file an I need to cover this code with unit-tests. With a sample file I may check my code whether the file was generated correctly. Maven creates src/test/resources directory automatically, there is no better place to put the sample file.
If the file has a path /src/test/resources/tables/sample.xml we can read them in a following way

URL url = this.getClass().getResource("/tables/sample.xml");
File sampleFile = new File(url.getFile());

Don't need to compare files line-by-line, just use commons-io util

FileUtils.contentEquals(file1, file2);

Finally, three lines of code below can be applied for unit tests (and not only for tests) to check for files equality:

private boolean isFileDataEqual(File target) throws IOException {
 URL url = this.getClass().getResource("/tables/sample.xml");
 File sampleFile = new File(url.getFile());
 return FileUtils.contentEquals(target, sampleFile);
}

Monday, July 7, 2014

OSGI servlet bridge sample


Servlet bridge is a mechanism for extending your servlet functionality adding other servlets. In this case your main servlet is not affected within some changes however the number of processed request is increased by newly added servlets (modules).
We don't want to have some sophisticated way to register/unregister new modules, don't want to be forced to rebuild the whole application when the new servlet is registered. OSGI is a keyword which helps us to avoid of this issues. In the scope of Servlet Bridge feature OSGI is a powerful mechanism which can help you to extend the functionality of your java servlet application. It can be achieved by using HttpService implementation provided by OSGI standards. Image source: http://www.jayway.com/2007/05/01/osgi-not-just-a-four-letter-word/
(image from: http://www.jayway.com/2007/05/01/osgi-not-just-a-four-letter-word/)
This post is a good way to start with theoretical basics of OSGI, let's try to implement the OSGI servlet bridge.