Posts

Showing posts from December, 2018

Semaphore

Semaphore is a simply a variable. This variable is used to solve critical section problem and to achieve process synchronization in the multi processing environment. The two most common kinds of semaphores are counting semaphores and binary semaphores. Counting semaphore can take non-negative integer values and Binary semaphore can take the value 0 & 1. only. Semaphore have below API functions wait(S s) {     while(s <= 0) ;     s--; } signal(S s) {     s++; } init(S, A) {     S = A; } Below are some problems to understand how it works. 1. Design multiple readers or 1 writer at a time 2. Dog and Master Problem: Master initialise N food in bucket.  Only one Dog at a time can get the food. When food get empty, Dog notify to Master. When get notified, master put +N food when food get empty and food should not overflow. Only one Master/Dog can access the resource at a time. //Inicialize Semaphores init(D, 1) //Semaphore to track Dog threads init(M, 0) // S

What’s the difference between @PathVariable, @PathParam, and @RequestParam?

Even though  @PathVariable  and  @RequestParam  are both used to extract values from the URL, their usage is largely determined by how a site is designed. The  @PathVariable  annotation is used for data passed in the URI (e.g. RESTful web services) while  @RequestParam  is used to extract the data found in query parameters. If we had chosen to use query parameters, our URL would be: http://localhost:8080/orders?id=100 This would be implemented in a controller method like the following: @GetMapping("/orders") @ResponseBody public String getOrder(@RequestParam(value = "id", required = true) String id) {   return "Order ID: " + id; } These annotations can be mixed together inside the same controller. @PathParam is a JAX-RS annotation that is equivalent to @PathVariable in Spring.   

Types of References in Java

Image
Types of References in Java In Java there are four types of references differentiated on the way by which they are garbage collected. Strong References Weak References Soft References Phantom References Strong References:  This is the default type/class of Reference Object. Any object which has an active strong reference are not eligible for garbage collection. The object is garbage collected only when the variable which was strongly referenced points to null. MyClass obj = new MyClass (); Here ‘obj’ object is strong reference to newly created instance of MyClass, currently obj is active object so can’t be garbage collected. obj = null; //'obj' object is no longer referencing to the instance. So the 'MyClass type object is now available for garbage collection. // Java program to illustrate Strong reference class Gfg {      //Code.. } public class Example {      public static void main(String[] args) {           //Strong