JP Morgan Interview Question
Java DevelopersCountry: United States
Interview Type: Phone Interview
Solution using classes from ConcurrentPackage:
Algorithm:
1. Create a Semaphore and initialize with N -the number of tasks.
2. Create a task X which needs Y permits to execute. ( values of Y -> 1 to N)
3. The value of Y to assign for a task depends on after how many tasks it should start execute.
3. once the task X completes, it release the Y permits.
4. For example, the task which needs to be executed at first assigned value 1 to Y, similarly the second task assigned with value 2 for Y, like that the last task assigned N value for Y
5. Start all the tasks.
This Semaphore will ensure all the tasks run sequentially
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class SequentialTaskExecutor {
ExecutorService service = Executors.newFixedThreadPool(10);
Semaphore sem = new Semaphore(10);
void runTasks()
{
try
{
sem.acquire(9);
service.submit(new CustomTask(2, sem));
service.submit(new CustomTask(4, sem));
service.submit(new CustomTask(3, sem));
service.submit(new CustomTask(1, sem));
service.submit(new CustomTask(7, sem));
service.submit(new CustomTask(6, sem));
service.submit(new CustomTask(8, sem));
service.submit(new CustomTask(9, sem));
service.submit(new CustomTask(10, sem));
service.submit(new CustomTask(5, sem));
}
catch (InterruptedException exc)
{
exc.printStackTrace();
}
}
public static void main(String[] args)
{
SequentialTaskExecutor executor = new SequentialTaskExecutor();
executor.runTasks();
}
}
class CustomTask implements Runnable
{
private int seqPosition;
private Semaphore sem;
CustomTask(int seqPosition, Semaphore sem)
{
this.seqPosition = seqPosition;
this.sem = sem;
System.out.println("Created Task : " + seqPosition);
}
public void run()
{
try
{
for (;;)
{
boolean isAcquired = sem.tryAcquire(seqPosition, 1000, TimeUnit.MILLISECONDS);
if (!isAcquired)
{
Thread.sleep(2000);
continue;
}
else
break;
}
System.out.println("Task " + seqPosition + " is completed");
sem.release(seqPosition+1);
}
catch (InterruptedException exc)
{
//do nothing
}
}
}
package practise;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by rajeswav on 6/18/2016.
*/
public class task {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
for(int i=0;i<100;i++){
executor.submit(new Runnable() {
public void run() {
System.out.print("call task methods");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
executor.shutdown();
executor.awaitTermination(2, TimeUnit.DAYS);
}
}
I guess interviewer meant executorService.
package practise;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by rajeswav on 6/18/2016.
*/
public class task {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newSingleThreadExecutor();
for (int i = 0; i < 100; i++) {
executor.submit(new Runnable() {
public void run() {
System.out.print("call task methods");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
executor.shutdown();
executor.awaitTermination(2, TimeUnit.DAYS);
}
}
}
package practise;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Created by rajeswav on 6/18/2016.
*/
public class task {
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(1);
for (int i = 0; i < 100; i++)
executor.submit(new Runnable() {
public void run() {
System.out.print("call task methods");
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
System.out.println("All threads are submitted");
executor.shutdown();
executor.awaitTermination(2, TimeUnit.DAYS);
}
}
Add the tasks to ordered list and iterate over the list to get the task and execute
- vsalaksh August 25, 2015