Skip to main content

Posts

The Basics of Classloading in Java

The Java Classloader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine. classes are loaded on demand in java run time environment. There is concept called delegation which is used in java class loading. Compiled java classes (.class file)  are packaged in JAR files in the Java language. A class with a given name can only be loaded once by a given classloader. Each Java class must be loaded by a class loader. Furthermore, Java programs may make use of external libraries (that is, libraries written and provided by someone other than the author of the program) or they may be composed, at least in part, of a number of libraries. There are three class loaders which are responsible for Classloadng inside JVM - 1) Bootstrap class loader: The bootstrap class loader loads the core Java libraries located in the /jre/lib directory. It loads the core classes which is available in rt.jar file also called runtime jar. It is...
Recent posts

Important points on Classes and Methods in Java as per Java Language Specification

Class Declarations: A class declaration specifies a new named reference type. There are two kinds of class declarations: normal class declarations and enum declarations. It is a compile-time error if a class has the same simple name as any of its enclosing classes or interfaces. Class Modifiers: A class declaration may include class modifiers. The access modifier public pertains only to top level classes and member classes, not to local classes or anonymous classes. The access modifiers protected and private pertain only to member classes within a directly enclosing class declaration. The modifier static pertains only to member classes, not to top level or local or anonymous classes. It is a compile-time error if the same keyword appears more than once as a modifier for a class declaration. abstract Classes: An abstract class is a class that is incomplete, or to be considered incomplete. It is a compile-time error if an attempt is made to create an instance o...

Java Memory Model

Processors generally have one or more layers of memory cache (eg. L1 cache, L2 cache and L3 cache etc.) in multiprocessor systems which improves performance by speeding access to data (because the data is closer to the processor) and reducing traffic on the shared memory bus (because many memory operations can be satisfied by local caches.) Memory caches can improve performance tremendously, but they present a host of new challenges. For example - What would happens when two processors examine the same memory location at the same time? Under what conditions will they see the same value? A memory model defines necessary and sufficient conditions for knowing that writes to memory by other processors are visible to the current processor, and writes by the current processor are visible to other processors. Some processors exhibit a strong memory model, where all processors see exactly the same value for any given memory location at all times. Other processors exhibit a weaker memo...

Travel Sales Man Problem (TSP) and Solution using Dynamic Programming

Given a graph of n vertices, determine the minimum cost path to start at a given vertex and travel to each other vertex exactly once, returning to the starting vertex. In some versions, the starting and ending points are different and fixed, and all other points have to be visited exactly once from start to end. A standard way to solve these problems is to try all possible orders of visiting the n points, which results in a run-time of O(n!). To calculate cost using Dynamic Programming, we need to establish recursive relation in terms of sub-problems. Let us define a term C(S, i) be the cost of the minimum cost path visiting each vertex in set S exactly once, starting at 1 and ending at i. We start with all subsets of size 2 and calculate C(S, i) for all subsets where S is the subset, then we calculate C(S, i) for all subsets S of size 3 and so on. Note that 1 must be present in every subset. So the algorithm is like below - If size of S is 2, then S must be {1, i},  C(S, ...

Implementing the Producer Consumer Pattern Using Wait and Notify in Java

Below are the conditions for producer consumer pattern. We need to ensure that the thread should not be blocked either if the buffer is empty or full. This is achieved by calling wait() and notify() method on the lock object which is used to synchronized the block of code. Please note that it is important for both threads to synchronize on the same monitor / lock object. A producer produces values inside a buffer. A consumer consumes the values from this buffer. The buffer can be empty or full. Producer and consumer runs in their own thread. Note: wait() and notify() should not be called outside the synchronized code block Below is the example code : package com.refermynotes; public class ProducerConsumerExample { public static int[] buffer; public static Object lock = new Object(); public static int count; static class Producer{ public void produce(){ synchronized (lock) { if(isFull(buffer)){ try { lock.wait(); } catch...