• Concurrency programming in Java

    11 comments

    Volatile

    The Java volatile keyword is used to mark a Java variable as 'being stored in main memory'. More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.

    Refs: Jenkov.com: Java Volatile Keyword - http://tutorials.jenkov.com/java-concurrency/volatile.html,
    Java67: How to stop a thread in Java? Example - http://www.java67.com/2015/07/how-to-stop-thread-in-java-example.html

    AtomicInteger

    There are two main uses of AtomicInteger:

    1. As an atomic counter that can be used by many threads concurrently - incrementAndGet()
    2. As a primitive that supports compare-and-swap instruction to implement non-blocking algorithms - compareAndSet()
    private volatile int counter;
    public int getNextUniqueIndex() {
        return counter++; // Not atomic operation
    }
    

    Performing an operation 'counter++' will cause huge problems in concurrency programming. It is not an atomic operation and can have different values in different threads. Therefore always use AtomicInteger to share variables:

    private AtomicInteger counter;
    public int getNextUniqueIndex() {
        return counter.getAndIncrement();
    }
    

    Ref: StackOverflow: Practical uses for AtomicInteger - http://stackoverflow.com/questions/4818699/practical-uses-for-atomicinteger

    Read More
  • JSP (JavaServer Pages)

    11 comments

    JSTL - Core <c:if> Tag

    The <c:if> tag evaluates an expression and displays its body content only if the expression evaluates to true. The <c:if> tag has the following attributes −

    Attribute Description Required Default
    test Condition to evaluate Yes None
    var Name of the variable to store the condition's result No None
    scope Scope of the variable to store the condition's result No Page
    Example
    <c:if test="${condition}">
    </c:if>
    
    <c:choose>
        <c:when test="${condition}">
        </c:when>
        <c:otherwise>
        </c:otherwise>
    </c:choose>
    

    Ref: TutorialsPoint: JSP - https://www.tutorialspoint.com/jsp/jstl_core_if_tag.htm

    Read More
  • Intellij IDEA

    11 comments

    If we want to import a module, but it is not a library, you can associate the project with the main application as follows in IntelliJ IDEA:

    1. Project Structure -> Modules -> Add new Module (+)
    2. Project Structure -> Modules -> Dependencies -> Add Module Dependency
    Read More
  • Linux & Server Administration

    11 comments

    Finding files

    $ grep -rnw '/path/to/somewhere/' -e 'pattern'
    
    • -r or -R is recursive,
    • -n adds line numbers,
    • -w stands for match the whole word,
    • -l (lower-case L) can be added to just give the file name of matching files

    Ref: How do I find all files containing specific text on Linux? - https://stackoverflow.com/questions/16956810/how-do-i-find-all-files-containing-specific-text-on-linux

    Network

    Show ports based on PID of a process

    $ sudo netstat -tulpn | grep ${PID}
    

    Show information about CPU

    $ cat /proc/cpuinfo
    

    Check the connection to the server and open port on a specific remote computer

    $ telnet SERVER_ADDRESS PORT
    

    Connect to a remote server, the best way through the command 'ssh'

    $ ssh USER@SERVER_ADDRESS
    

    Less

    Less is a program similar to more (1), but which allows backward movement in the file as well as forward movement. Useful commands when browsing logs:

    • Waiting for latest lines at EOF - SHIFT + F
    • Finish of waiting at EOF - CTRL + C
    • Search for a specific text - /${TEXT}
    • Leave or finish - Q

    Nano

    GNU nano is a small and friendly text editor. Besides basic text editing, nano offers many extra features like an interactive search and replace, go to line and column number, auto-indentation, feature toggles, internationalization support, and filename tab completion.

    • Search for a specific text - CTRL + W

    Grep

    grep is a command-line utility for searching plain-text data sets for lines that match a regular expression. Its name comes from the ed command g/re/p (globally search a regular expression and print), which has the same effect: doing a global search with the regular expression and printing all matching lines. Grep was originally developed for the Unix operating system, but later available for all Unix-like systems.

    grep -E option is for extended regexp. If you use the grep command with -E option, you just need to use | to separate multiple patterns for the or condition.

    $ ps aux | grep -E 'ssh|PID'
    

    It is useful for the showing the title of listing processes when filter for a specific command.

    Read More
  • Copying on Mac OS X

    11 comments

    To copy files on Mac OS X on a remote server can be used FileZilla program, because it uses a secure FTPS protocol. Another good alternative to browsing the file system is e.g. Double Commander (similar to Total Commander) and Midnight Commander.

    • https://filezilla-project.org/
    • https://doublecmd.sourceforge.io/
    • https://midnight-commander.org/
    Read More