Adobe Interview Question
Member Technical StaffsCountry: India
we can use callable to run two tasks t1,t2 and a Countdown Latch (gate) to stop one and allow other one to execute Eg:
CountDownLatch gate = new CountDownLatch(1);
Callable<String> task1 = new J1();
Callable<String> task2 = new J2();
Future f1= executor.submit(task1);
Future f2= executor.submit(task2);
gate.CountDown();
Override Call Method in such a way stop j1 (identify j1 by having name) and keep it wait for certain time(gate.await()) in the mean while j2 - task2 gets executed
(To make sure that j1 is the last job that is done update the public name variable by the two tasks and see the name variable contains j1 value)
import java.util.concurrent.CountDownLatch;
public class AdobeThreadDemo {
private static CountDownLatch latch = new CountDownLatch(1);
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable(){
@Override
public void run() {
System.out.println("T1 job start");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("T1 job done");
latch.countDown();
}
});
Thread t2 = new Thread(new Runnable(){
@Override
public void run() {
try {
System.out.println("T2 job waiting for T1 job to be finished");
latch.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("T2 job start");
System.out.println("T2 job done");
}
});
t1.start();
t2.start();
}
}
we can use a common variable , we can use pipedInputstreams and PipedOutputstreams , we can use wait() and notify .
##########################################################
package com.CareerCup;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ThreadSynch implements Runnable{
volatile boolean job1Done = false;
public static void main(String str[])
{
ThreadSynch obj = new ThreadSynch();
Thread t1 = new Thread(obj,"T1");
Thread t2 = new Thread(obj,"T2");
t1.start();
t2.start();
}
@Override
public void run() {
try{
if(Thread.currentThread().getName().equals("T1"))
job2();
else
job1();
}
catch(Exception e){}
}
synchronized void job1()
{
System.out.println("entering the job1");
for(int i=0;i<100000;i++)
{
System.out.println("this is running on Thread1 Job1 "+ i);
}
notify();
job1Done= true;
}
synchronized void job2() throws InterruptedException
{
System.out.println("entering the job2");
if(!job1Done)
{
wait();
}
for(int i=0;i<100000;i++)
{
System.out.println("this is running on Thread 2 Job2 "+ i);
}
}
}
simple program without join, I use a boolean volatile variable : so do the snyching
#################################################
public class ThreadSynch implements Runnable{
volatile boolean job1Done = false;
public static void main(String str[])
{
ThreadSynch obj = new ThreadSynch();
Thread t1 = new Thread(obj,"T1");
Thread t2 = new Thread(obj,"T2");
t1.start();
t2.start();
}
@Override
public void run() {
try{
if(Thread.currentThread().getName().equals("T1"))
job2();
else
job1();
}
catch(Exception e){}
}
synchronized void job1()
{
System.out.println("entering the job1");
for(int i=0;i<100000;i++)
{
System.out.println("this is running on Thread1 Job1 "+ i);
}
notify();
job1Done= true;
}
synchronized void job2() throws InterruptedException
{
System.out.println("entering the job2");
if(!job1Done)
{
wait();
}
for(int i=0;i<100000;i++)
{
System.out.println("this is running on Thread 2 Job2 "+ i);
}
}
}
public static void main(String[] args) {
final ReentrantLock lock1 = new ReentrantLock();
final Condition job1 = lock1.newCondition();
final AtomicBoolean job1Done = new AtomicBoolean(false);
Thread tjob1 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
lock1.lock();
System.out.println("JOb 1 entered");
Thread.sleep(1000);
job1Done.set(true);
System.out.println("JOb 1 done");
job1.signalAll();
}
catch (Exception e){
}finally{
lock1.unlock();
}
}
});
Thread tjob2 = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
lock1.lock();
while(!job1Done.get()){
System.out.println("JOb 2 Waiting");
job1.await();
}
System.out.println("JOb 2 DONE Waiting");
}
catch (Exception e){
}finally{
lock1.unlock();
}
}
});
tjob2.start();
tjob1.start();
}
We can implement it using two locks, one for job1 and second one for job2.
- Ajeet September 01, 2013