JP Morgan Interview Question
Senior Software Development EngineersCountry: India
Interview Type: In-Person
/**
* How could you make sure that thread A ,B and C run sequentially without using join method?
*
* @author manoj.sharma
* @source Barclays
*/
public class ThreadJoiningWithoutJoin {
public static void main(String[] args) {
final JoinTask task = new JoinTask();
Thread A = new Thread(){
public void run(){
task.doJob(1, "JOB A DONE...");
}
};
Thread B = new Thread(){
public void run(){
task.doJob(2, "JOB B DONE...");
}
};
Thread C = new Thread(){
public void run(){
task.doJob(3, "JOB C DONE...");
}
};
C.start();
B.start();
A.start();
}
}
// Shared Class or Object
class JoinTask {
private int currentRank = 1;
public void doJob(int rank, String msg) {
synchronized(this) {
while (rank != currentRank) {
try {wait();}catch(InterruptedException ex) {ex.printStackTrace();};
}
System.out.println("Job:" + currentRank + " : " + msg );
currentRank++;
notifyAll();
}
}
}
Here is the code......
import java.util.concurrent.TimeUnit;
public class MyJoinTest{
public static void main(String[] args) {
MyThread t1 = new MyThread(new Runnable() {
@Override
public void run() {
for(int i=0;i<5;i++){
System.out.println("-----"+i+"-----");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
MyThread t2 = new MyThread(new Runnable() {
@Override
public void run() {
for(int i=5;i<10;i++){
System.out.println("-----"+i+"-----");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
MyThread t3 = new MyThread(new Runnable() {
@Override
public void run() {
for(int i=10;i<15;i++){
System.out.println("-----"+i+"-----");
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
t1.start();
t1.myJoin();
t2.start();
t2.myJoin();
t3.start();
t3.myJoin();
System.out.println("In Main, After Join");
}
}
class MyThread{
private Runnable runnable = null;
private Thread thread = null;
public MyThread(Runnable runnable) {
this.runnable = runnable;
}
public void start(){
thread = new Thread(runnable);
thread.start();
}
public void myJoin(){
while(thread.isAlive()){
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
can we use condition variables in simulating the join (in C++)?
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>
std::mutex mtx;
std::condition_variable cv;
int counter = 0;
void print_dummy(const int id) {
std::lock_guard<std::mutex> lck(mtx);
std::cout << "---" << id << "+++" << std::endl;
std::this_thread::sleep_for(std::chrono::microseconds(100));
counter++;
cv.notify_one();
}
int main() {
const int threads_counter = 10;
std::vector<std::thread> tLst;
for (int i = 0; i < threads_counter; ++i) {
tLst.push_back(std::thread(print_dummy, i));
}
std::unique_lock<std::mutex> lck(mtx);
while (counter != threads_counter) cv.wait(lck);
std::cout << "Press enter to continue ..." << std::endl;
std::cin.get();
for (auto& t : tLst) t.detach();
return 0;
}
wait also does the same thing as join.
Once the thread completes its execution, it just notifies everyone waiting on the Thread object.
Below program worked fine and the output is:
Thread-0 Thread3.run()
Thread-1 Thread3.run()
main Thread3.run()
package threads;
public class MyJoinClass {
public static void main(String[] args) {
Thread t1 = new Thread3();
Thread t2 = new Thread3();
t1.start();
myJoin(t1);
t2.start();
myJoin(t2);
System.out.println(Thread.currentThread().getName() + " Thread3.run()");
}
private static void myJoin(Thread t) {
synchronized (t) {
if(t.getState() != Thread.State.TERMINATED)
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Thread3 extends Thread
{
@Override
public void run() {
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " Thread3.run()");
}
}
- SumitGaur April 15, 2014