JAVA: Add two numbers without using any arithmetic operators

Solution:

int add(int a, int b)
{
     if(b == 0)
         return sum;
     sum = a ^ b;
     carry = (a & b) << 1;
     return add(sum,carry);

}

Explanation: Think bit by bit addition, there are only 4 cases;

0+0=0
0+1=1
1+0=1
1+1=0 (and generates carry)

The two lines handle different cases

sum = a ^ b

Handles case 0+1 and 1+0, sum will contain the simple case, all bit positions that add up to 1.

carry = (a & b) << 1

The (a & b) part finds all bit positions with the case 1+1. Since the addition results in 0, it’s the carry that’s important, and it’s shifted to the next position to the left (<<1). The carry needs to be added to that position, so the algorithm runs again.The algorithm repeats until there are no more carries, in which case sum will contain the correct result.

Difference between FixedThreadPool and CachedThreadPool

Cached Thread Pool :

  • A thread pool that creates as many threads it needs to execute the task in parallel.
  • The old available threads will be reused for the new tasks.
  • If a thread is not used for 60 seconds, it will be terminated and removed from the pool.
  • Suitable for applications that launch many short-lived tasks.

Method :

ExecutorService executor = Executors.newCachedThreadPool()

Fixed Thread Pool :

  • At any point, at most nThreads threads will be active processing tasks.
  • If a thread is not available for the task, the task is put in queue waiting for another task to ends.
  • If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks.
  • The threads in the pool will exist until it is explicitly shutdown.

Method :

ExecutorService executor = Executors.newFixedThreadPool(10)

3 ways to create thread in java

1: By extending thread class


class MyClass extends Thread{

	@Override
	public void run() {
		System.out.println("Thread created by extending thread class");
	}
}
public class MainClass {

	public static void main(String[] args) {

		MyClass thread = new MyClass();
		thread.start();
	}

}

2: By implementing Runnable interface


class MyClass implements Runnable{

	@Override
	public void run() {
		System.out.println("Thread created by implementing Runnable interface");
	}
}
public class MainClass {

	public static void main(String[] args) {
		Thread thread = new Thread(new MyClass());
		thread.start();
	}

}

3: By using Executor framework

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyClass implements Runnable{

	@Override
	public void run() {
		System.out.println("Thread created by using Executor Framework");
	}
}
public class MainClass {

	public static void main(String[] args) {
		ExecutorService executor = Executors.newSingleThreadExecutor();
		executor.execute(new MyClass());

//		ExecutorService executor = Executors.newFixedThreadPool(10);
//		ExecutorService executor = Executors.newCachedThreadPool();
	}

}

Which are the different method name resolvers in MultiActionController ?

Spring framework tutorial | Spring framework tutorial pdf downloads | Spring Interview Questions | Spring MVC Interview Questions
MultiActionController needs some way to resolve which method to call when handling an incoming http request. Spring provides MethodNameResolver interface to achieve this. The MultiActionController class provides a property named ‘methodNameResolver’ so that you can inject a MethodNameResolver.
There are three different types of MethodNameResolver listed below
1) InternalPathMethodNameResolver
2) ParameterMethodNameResolver
3) PropertiesMethodNameResolver

PropertiesMethodNameResolver example in Spring MVC

Spring framework tutorial | Spring framework tutorial pdf downloads | Spring Interview Questions | Spring MVC Interview Questions
PropertiesMethodNameResolver : It uses a Properties object containing request URLs mapped to method names. Consider the mapping as “/multiaction/add.dev=add”, a request with URL “/multiaction/add.dev” will invoke “add(HttpServletRequest, HttpServletResponse)”.
Given below is the required configuration for PropertiesMethodNameResolver

<bean name="/multiaction/*.dev" class="com.thecafetechno.com.MultiProcessor">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
	<property name="mappings">
		<value>
	 	    /multiaction/add.dev=add
		    /multiaction/remove.dev=remove
		    /multiaction/listAll.dev=listAll
		</value>
	</property>
</bean>
</property>
</bean>

Given below are the request URLs that will be resolved into method invocation.

<a href="multiaction/add.dev">Add</a>
<a href="multiaction/remove.dev">Remove</a>
<a href="multiaction/listAll.dev">ListALL</a>