Adobe Interview Question for Member Technical Staffs


Country: India




Comment hidden because of low score. Click to expand.
1
of 1 vote

We can implement it using two locks, one for job1 and second one for job2.

public class ProducerConsumer {
	
	Lock lock1 = new ReentrantLock();
	Lock lock2 = new ReentrantLock();
	
	public void job1() throws InterruptedException{
		try{
			lock1.lock();
//			while(true){
//				System.out.println("Job 1 started by " + Thread.currentThread().getName());
//				Thread.currentThread().sleep(100);
//			}
		} catch(Exception ex){
			ex.printStackTrace();
		} finally {
			lock1.unlock();
		}	
		
	}
	
	public void job2() throws InterruptedException{
		try{
			lock2.lock();
//			while(true){
//				System.out.println("Job 2 started by " + Thread.currentThread().getName());
//				Thread.currentThread().sleep(100);
//			}
		} catch(Exception ex){
			ex.printStackTrace();
		} finally {
			lock2.unlock();
		}
	}
	//Test client
	public static void main(String[] args) {
		final ProducerConsumer pc = new ProducerConsumer();
		
		Thread thread1  = new Thread(new Runnable() {			
			@Override
			public void run() {
				try {
					pc.job1();
					Thread.currentThread().sleep(100);
					pc.job2();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}, "Thread1");
		Thread thread2  = new Thread(new Runnable() {			
			@Override
			public void run() {
				try {
					pc.job2();
					Thread.currentThread().sleep(100);
					pc.job1();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}, "Thread2");
		
		thread1.start();
		thread2.start();
	}
}

- Ajeet September 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this is a kind of producer consumer problem where consumer can consume only until producer has produced

- blackfever May 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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)

- Teja May 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

executor- EXecutorService provided by java frame work

- Teja May 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using a shared variable 'var' initialized with 0.
Thread t2 will loop through in an infinite while loop to check 'var' !=1.
Thread t1 can set var to 1 once it completes the task.

- Hello world May 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use wait and notify on a condition variable

- Anonymous May 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();
		
	}
}

- Prashant Gupta June 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}


}


}

- Mahanth Hiremath (mahanthopensource@gmail.com) August 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}

}

}

- Mahanth HIremath ( mahanthopensource@gmail.com) August 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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();

}

- easy solution January 01, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More