10 Nisan 2015 Cuma

Object.wait() & Object.notifyAll() methods in Synchronization Block

Example Code:
public class MyClass {
  private List temp = new ArrayList();

  public void addItem(V obj) {
    synchronized (temp) {
      while (temp.size() >= maxSize) {
        temp.wait();
      }
      temp.add(obj);
    }
  }

  public void clear() {
    synchronized (temp) {
      temp.clear();
      temp.notifyAll();
    }
  }
}
At the first look, some questions come to my mind such as;

1) If "temp"(monitor object) is locked in sychronized block of addItem method, how can processor pass over "synchronized (temp)" in clear method and run "temp.notifyAll()" to wake up monitor object?

Answer: Object.wait() releases the monitor object! Hence, processor can get into synchronized code block in clear method and notify all threads that are put waiting.

2) Is it possible that Object.wait() is used without synchronized block?

Answer: Maybe not in this example above, but the condition in "while" may be set by a separate thread. Hence, synchronization code block is required to have this work correctly.

2 Nisan 2015 Perşembe

How to update the page when some events are occurred at the server on JSF

In simple JSF project, client request the server dynamically to get javascript and HTML codes. But, how can we see changes on HTML page when some events are occurred at the server-side?

Standart JSF 2.0 components does not supply this feature, that's why third party libraries are used to push HTML page, such as; Primefaces(<p:push>), RichFaces(<a4j:push>) and IceFaces(IcePush). However, while applying these features of these libraries, we may get in trouble with the server that we use. It may take time to configure the server and reach the solution!

Hence, I recommend <p:poll> of Primefaces. In polling mechanism, client sends ajax request to the server every 'X' seconds that we can configure. Then, server can update HTML page within necessities.

 It is very easy to apply it on JSF projects;
<p:poll interval="10" listener="#{bean.checkNotification}" oncomplete="prepareUI()" update=":form:growl :form:isUpdated" />

Specification of Port Number on Socket Programming

In applications that provide communication via socket, it is important that port number should not be between 1 and 1024. Ports between these numbers are used by root for other specific processes. If we choose a number between 1 and 1024 to specify port, we meet "Bind Permission Error" in our program!