Infibeam Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




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

public class OneMoreDeadlock
{
public static void main(String args[])
{
Thread t1 = new Thread(new Runnable()
{
public void run()
{
synchronized(String.class)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Waiting for integer");
synchronized(Integer.class)
{
System.out.println("Thread1");
}
}
}
});

Thread t2 = new Thread(new Runnable()
{
public void run()
{
synchronized(Integer.class)
{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Waiting for string");
synchronized(String.class)
{
System.out.println("Thread2");
}
}
}
});

t1.start();
t2.start();
}
}


Fix: change the order of resources.

- raghava.javvaji September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package threading;

class DeadLock1 {

public static synchronized void method() {
System.out.println("Thread executed Deadlock1" + Thread.currentThread());
DeadLock2.method();
}

}

class DeadLock2 {

public static synchronized void method() {
System.out.println("Thread executed Deadlock2" + Thread.currentThread());
DeadLock1.method();
}
}

public class DeadLock {
public static void main(String [] args) {
Thread t1 = new Thread(new Runnable() {

@Override
public void run() {
DeadLock1.method();
}
});
t1.start();

Thread t2 = new Thread(new Runnable() {

@Override
public void run() {
DeadLock2.method();
}
});
t2.start();
}
}


In the main class... I started 2 threads, t1 --> Invoking DeadLock1.method() [ thus it gains the lock on DeadLock1 class object] , t2 --> Inboking DeadLock2.method() [ thus it gains the lock on DeadLock2 class object]

Now... DeadLock1.method is trying to call DeadLock2.method , hence forcing the thread t1 to make hold the lock of DeadLock2 class object, which is already held by t2.

Similarly t2 is not able to access DeadLock1's lock. Both threads are waiting for each other to release the lock, hence DeadLock!!

- Santhosh Bhat March 21, 2012 | 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