Morgan Stanley Interview Question
AssociatesCountry: India
Interview Type: Phone Interview
static int system;
static int number;
public void run(){
System.out.println(Thread.currentThread().getName());
//System.out.println(number);
if(number ==0){
System.out.println("running runnable");
int value =new IntegerThreadCall().execute(3);
System.out.println(value);
system =value;
}else{
int value =new IntegerThreadCall().execute(3);
if(value == system){
try {
Thread.currentThread().sleep(1000000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
System.out.println(value);
system=value;
}
}
number ++;
}
public static void main(String ... args){
System.out.println("here");
IntegerThreadCall ith1=new IntegerThreadCall();
ith1.start();
IntegerThreadCall ith2=new IntegerThreadCall();
ith2.start();
IntegerThreadCall ith3=new IntegerThreadCall();
ith3.start();
}
public int execute(int i){
//System.out.println(i);
return i;
}
I think an implementation
{{
public void print (Integer value){
synchronized(value){
System.out.println(value);
}
}
}}
when the function called with same integer as apramaters only one thread will get lock on Integer Object
You are using Integer parameter as if its an enum (which is singleton), so you are assuming that if the value is same then the parameters for two different threads with same int value would be same object and synchornized() will do the trick ? The instance of Integer will be a different object for two different threads even with same value... so synchronizing on Integer would not help - It would if it was an enum ,,,
My solution would be:
1. first create a function (synchronized) to convert an integer to corresponding integer object using a map/hashtable such that this map returns same object with same integer value.
2. then print function should be like:
public void print (Integer value){
synchronized(value){
System.out.println(value);
}
}
I hope this would work.. :)
for object ineterning for Integer it will work. But above 128 it will not work. Please reconsider the case.
But the duplicate values will be printed after the lock removed..how to discard the duplicate values??
import java.util.*;
public class Test{
private Map<Integer,String> lM=new HashMap<Integer,String>();
public static void main(String[] args){
final Test t=new Test();
Thread t1=new Thread(new Runnable(){
public void run(){
t.printNumber(1);
}
});
Thread t2=new Thread(new Runnable(){
public void run(){
t.printNumber(1);
}
});
Thread t3=new Thread(new Runnable(){
public void run(){
t.printNumber(2);
}
});
t1.start();
t2.start();
t3.start();
}
public void printNumber(int n){
String lo=stringFactory(n);
synchronized(lo){
for(int i=0;i<10;i++){
try{
Thread.sleep(1000);
}catch(Exception e){}
System.out.println(Thread.currentThread()+":"+n);
}
}
}
private String stringFactory(int n){
if(lM.get(n)==null)
lM.put(n,new String());
return lM.get(n);
}
}
package Morgan_Stanley;
import java.util.HashMap;
public class Locking_On_Same_Integer_Obj {
HashMap<Integer, String> map = new HashMap<Integer, String>();
String val;
public void print(Integer obj){
if(map.get(obj)==null){
val = obj.toString();
map.put(obj, val);
}else
val = map.get(obj);
synchronized(val){
int i =0;
while(i<1000){
System.out.println(Thread.currentThread().getName());
System.out.println(obj);
System.out.println(i);
i++;
if(i>100)
break;
}
}
}
public static void main(String args[]){
Locking_On_Same_Integer_Obj obj1 = new Locking_On_Same_Integer_Obj();
Thread t1 = new Thread(new ThreadTest(130,obj1));
Thread t2 = new Thread(new ThreadTest(130,obj1));
t1.start();
t2.start();
}
}
class ThreadTest implements Runnable{
Integer value;
Locking_On_Same_Integer_Obj testObj;
public ThreadTest(int k,Locking_On_Same_Integer_Obj lock){
value = k;
testObj = lock;
//testObj.print(value);
}
@Override
public void run(){
testObj.print(value);
}
}
how about this :)
public class Test{
private AtomicReference<Integer> intVal = new AtomicReference<Integer>();
public void print(Integer i){
intVal.compareAndSet(-1, i);
System.out.println(i);
intVal.compareAndSet(i, -1);
}
}
public static void main(String...str){
int a[] = {1,2,3,4,5,6,18, 50};
int b[] = {1,3,5,6,7,8, 9, 10};
int i = 0;
int j = 0;
while (i < a.length && j < b.length ){
if(a[i] > b[j ]){
System.out.print(b[j ++] + " ");
}else if(a[i] < b[j] ){
System.out.print(a[i ++] + " ");
}else if(a[i] == b[j]) {
i ++;
j ++;
}
}
while (i < a.length){
System.out.print(a[i ++] + " ");
}
while (j < b.length){
System.out.print(b[j ++] + " ");
}
}
public class TestConditionLock {
private Lock lock = new ReentrantLock();
private Integer value = null;
private Condition condition = lock.newCondition();
void method(final int i) throws InterruptedException
{
if(i == value)
{
System.out.println("Integer: "+ i);
}
else
{
lock.lock();try
{
while(value!=null && value!=i)
{
condition.await();
}
value = i;
System.out.println("Integer: "+ i);
value = null;
condition.signalAll();
}
finally
{
lock.unlock();
}
}
}
}
public class ThreadQues {
public static void main(String[] args) {
Resource r = new Resource();
new Thread(new RunnableTarget(r), "T1").start();
new Thread(new RunnableTarget(r), "T2").start();
new Thread(new RunnableTarget(r), "T3").start();
}
static class Resource {
int item;
int prev;
public synchronized void addAndPrint() {
prev=item;
item++;
System.out.println(Thread.currentThread().getName() + " added 1 to prev value [ "+prev +" ] so update value is [ " + item+"]");
}
}
static class RunnableTarget implements Runnable {
Resource r;
public RunnableTarget(Resource r) {
super();
this.r = r;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
r.addAndPrint();
}
}
}
}
I solution i can think of (works well with small set of intergers, say "N"). We'll have an array of CriticalSection/ mutex defined and if a thread with particular integer call this function, this thread will take the mutex (acquire the lock) and will print the value. Other thread with same integer value will wait.
- Mahi. May 10, 2013