Posts

Showing posts from April, 2019

[Java] Maven Archetype and pom.xml

1 - Project Template Archetype are project templates that can be generated for use. To begin, run: mvn archetype:generate Then, the cmd will prompt you for: Archetype project templates - eg. web applications, JavaEE projects, Hiberate, Spring Group Id Uniquely identifies your project across all projects Eg. org.apache.maven or org.apache.maven.reporting Artifact Id Name your jar (or output) Version Usually looks like 1.0 or 1.1.1 SNAPSHOT are nightly builds; meaning not released/in-development RELEASE means released. Avoid using dates Packaging The project's packaging; in other words, the output  file By default, it will be jar (if none specified) Valid values: pom, maven-plugin, ejb, ear, rar, war, jar Once done, it will generate a seed maven project. 2 - pom.xml (Basic) Basic Structure of pom.xml is: maven coordinates defining the maven project metadata build information resources and dependencies 2.1 - Dependencies Most

[Java - Synchronization] Queues

1) Blocking Queue A blocking queue is a queue that blocks when you attempt to: dequeue from it and the queue is empty, or  enqueue items to it and the queue is already full.  Example use case: if you want some action to be executed only after 100 clients log-in. 1.1) Implementation BlockingQueue<TYPE> queue = new  LinkedTransferQueue <>(); BlockingQueue<TYPE> queue = new  ArrayBlockingQueue <>(); 1.2) Methods put(..): Inserts the specified element at the tail of this queue, waiting for space to become available if the queue is full. take(..):  Retrieves and removes the head of this queue, waiting if necessary until an element becomes available. 2) Delay Queue An unbounded blocking queue of Delayed elements, in which an element can only be taken when its delay has expired.  3) Priority Queue An unbounded blocking queue that orders the elements based on the generic's compareTo method.  If generic is a custom class, then you wil

[Java - Synchronization] Latches

1) Countdown Latch A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. A CountDownLatch is initialized with a given count. The await method is blocked until the current count reaches zero; we can reduce current count using the method,  countDown() . The count cannot be reset.  2) Cyclic Barrier Similar to countdown latch, but the count can reset. Resources https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

[Java - Synchronization] ExecutorService and Futures

1) Overview While it is easy to create one or two threads and run them, it becomes a problem when your application requires creating 20 or 30 threads for running tasks concurrently. It won't be exaggerating to say that large multi-threaded applications will have hundreds, if not thousands of threads running simultaneously. So, it makes sense to separate thread creation and management from the rest of the application. ExecutorService is a framework provided by the JDK which simplifies the execution of tasks in asynchronous mode. Generally speaking, ExecutorService automatically provides a pool of threads and API for assigning tasks to it. Executors framework helps you with: Thread Creation : Provide methods for creating threads, more specifically a pool of threads, that your application can use to run tasks concurrently. Thread Management : Manage life cycle of the threads in the thread pool. You don’t need to worry about whether the threads in the thread pool