Samsung Interview Question
Developer Program EngineersCountry: United States
Interview Type: In-Person
The code will look like this.But the problem is even after calling the returnObject the client class can keep on using the Object.I am still thinking on a fullproof code.
class TwentyOnly{
private static List<TwentyOnly> twenty= new ArrayList(20);
private static int counter=0;
private TwentyOnly(){
}
public static synchronized TwentyOnly borrow(){
if(counter<20){
twenty.add(0,new TwentyOnly());
counter++;
return twenty.get(0);
}
throw new RUntimeException("All Allowed instances allowed");
}
public static synchronized void returnObject(TwentyOnly t){
twenty.remove(t);
counter--;
}
}
you might want to do sth like this.
public static synchronized void returnObject(TwentyOnly t){
twenty.remove(t);
counter--;
t=null;
}
I made some change based on onlinesoumitra code
class TwentyOnly{
private static List<TwentyOnly> twenty= new ArrayList(20);
private static int counter=0;
private TwentyOnly(){
}
public synchronized void borrowObject(){
if(counter<=20){
twenty.add(0,new TwentyOnly());
counter++;
//return twenty.get(0);
}else
{
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.notify();
//throw new RuntimeException("All Allowed instances allowed");
}
public synchronized void returnObject(TwentyOnly t){
if(counter>0){
twenty.remove(t);
counter--;
}else
{
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.notify();
}
}
public class TwentyObjectPool{
private static class ExpensiveObject implements FancyInterface{
private boolean dirty = false;
private void sayHello(){
if(!dirty){
System.out.println("Hello");
}else{
throw new IllegalStateException("I am dirty");
}
}
}
private List<ExpensiveObject> objects = Arrays.asList(new ExpensiveObject(), new ExpensiveObject() //18 more times);
private int count = 20;
public FancyInterface borrowObject(){
if(count => 0){
objects.remove(--count);
}else{
throw new IllegalStateException("Out of Objects");
}
}
public void returnObject(FancyObject obj){
assert obj instanceof ExpensiveObject;
ExpensiveObject expObj = (ExpensiveObject)obj;
expObj.dirty = true;
objects.add(new ExpensiveObject());
count++;
}
}
package com.java.basic.datastructure;
public interface ObjectPool <E>{
public static final int NUMBER_OF_OBJECT_CREATED=20;
public E borrowObject() throws InterruptedException ;
public void returnObject(E object) throws InterruptedException ;
/**
* Get the number of Live Objects(Objects currently in use by other classes).
* @return
*/
public int aliveObjectCount();
}
package com.java.basic.datastructure;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TwentyOnly implements ObjectPool<TwentyOnly>{
private static BlockingQueue<TwentyOnly>queue;
static{
queue=new ArrayBlockingQueue<TwentyOnly>(20);
for(int index=0;index<NUMBER_OF_OBJECT_CREATED;index++) {
queue.add(new TwentyOnly());
}
}
@Override
public TwentyOnly borrowObject() throws InterruptedException {
return queue.take();
}
@Override
public void returnObject(TwentyOnly object) throws InterruptedException {
queue.put(object);
}
@Override
public int aliveObjectCount() {
// TODO Auto-generated method stub
return (NUMBER_OF_OBJECT_CREATED-queue.size());
}
}
package com.java.basic.datastructure;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TwentyOnly implements ObjectPool<TwentyOnly>{
private static BlockingQueue<TwentyOnly>queue;
private TwentyOnly(){
}
static{
queue=new ArrayBlockingQueue<TwentyOnly>(20);
for(int index=0;index<NUMBER_OF_OBJECT_CREATED;index++) {
queue.add(new TwentyOnly());
}
}
@Override
public TwentyOnly borrowObject() throws InterruptedException {
return queue.take();
}
@Override
public void returnObject(TwentyOnly object) throws InterruptedException {
queue.put(object);
}
@Override
public int aliveObjectCount() {
// TODO Auto-generated method stub
return (NUMBER_OF_OBJECT_CREATED-queue.size());
}
}
package com.java.basic.datastructure;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class TwentyOnly {
/**
* BlockingQueue implementations are thread-safe. All
* queuing methods achieve their effects atomically using internal
* locks or other forms of concurrency control
* BlockingQueue can safely be used with multiple
* producers and multiple consumers.
*/
private static BlockingQueue<TwentyOnly>queue;
private static int NUMBER_OF_OBJECT_CREATED=20;
/**
* Constructor must be private
*/
private TwentyOnly(){}
static{
queue=new ArrayBlockingQueue<TwentyOnly>(20);
for(int index=0;index<NUMBER_OF_OBJECT_CREATED;index++) {
queue.add(new TwentyOnly());
}
}
public static TwentyOnly borrowObject() throws InterruptedException {
return queue.take();
}
public static void returnObject(TwentyOnly object) throws InterruptedException {
queue.put(object);
}
public static int aliveObjectCount() {
return (NUMBER_OF_OBJECT_CREATED-queue.size());
}
}
We can write a solution using producer & consumer(Sharing a list of objects) and singleton pattern.
- Raghava April 28, 2013