Skip to main content

Posts

Showing posts from January, 2019

Java Concurrency - Atomic classes

Greetings! Java let us use update operations on single variables in non-blocking, thready safe programming with Atomic variables. ( atomic-package-summary ) This is like a enhanced version of volatile variables. It should be note that these classes are not replacement for Integer and related classes. These are not only thread safe, but also provides better performance than using synchronized blocks. Source code for Synchronized vs Atomic classes performance can be found here . Main classes, AtomicBoolean AtomicInteger AtomicLong AtomicReference Say we want a counter, AtomicInteger and AtomicLong has below methods. incrementAndGet() - Atomically increments by one the current value. decrementAndGet() - Atomically decrements by one the current value. import java.util.concurrent.atomic.AtomicInteger; public class AtomicCounter implements Counter { private AtomicInteger atomicInteger = new AtomicInteger(0); public void increment() { atomicInteger

MongoDB - Basic Commands

Greetings! These are the basic MongoDB commands we use in our day-today work. Adding as a reference to myself :) Create database use DATABASE_NAME $ use my_database Show databases $ show dbs Connect to database use DATABASE_NAME $ use my_database Drop database db.dropDatabase() Create Collection DB.COLLECTION_NAME.(OPTIONS) $ db.products.({ name: "Fruit Juice" }) Show collections $ show collections Drop collection db.COLLECTION_NAME.drop() $ db.products.drop() Insert document db.COLLECTION_NAME.insert(DOCUMENT) $ db.products.insert({ item_name: "Frui Juice", item_description: "sdfsdklf", price: "100" }) Read document db.COLLECTION_NAME.find() db.COLLECTION_NAME.find().pretty() $ db.products.find().pretty() $ db.products.find({ item_name: "Fruit Juice" }).pretty() Like statement - we have to use regex $ db.products.find( { item_name: { $regex: /789$/ } } ) AND $ db.COLLECTION_NAME.f

Java Concurrency - Executor Framework

Greetings! What is the basic abstraction for creating threads in Java? Is it new Thread(new Runnable()) ? NO. It is Executors! Decoupling is a good programming practice and it is better to separate thread creation from the application. Java's way of encapsulating these functions are known as executors. ( Executors ) Executor Interfaces Executor - simple interface used to submit new tasks ExecutorService - sub interface of Executor which supports life cycle management of both of the tasks and Executor itself. ScheduledExecutorService - sub interface of ExcecutorService which supports task scheduling. ExecutorService This has more features than Executor interface. Mainly it support thread pooling, return the result via Callable, shutting down the service. execute(Runnable) submit(Runnable) submit(Callable) shutdown() This has following implementations in java.util.concurrent package. ThreadPoolExecutor ScheduledThreadPoolExecutor Creating a ExecutorServic

Java Concurrency - SimpleDateFormat Is Not Simple

Greeting! Formatting, parsing dates is a painful task. It always give us errors :(. A common way to format, parse dates in Java is using SimpleDateFormat. Here is a common class we can use. import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public final class DateUtils { public static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd"); private DateUtils() {} public static Date parse(String target) { try { return SIMPLE_DATE_FORMAT.parse(target); } catch (ParseException e) { e.printStackTrace(); } return null; } public static String format(Date target) { return SIMPLE_DATE_FORMAT.format(target); } } Do you think this is working. Let's test it. private static void testSimpleDateFormatInSingleThread() { final String source = "2019-01-11"; System.out.println(DateUtils.parse(s