I have just gotten out of yet another interview with a “senior” Java candidate who doesn’t know threading at all. Please don’t claim you are a senior candidate unless you can answer this basic question.
public class Foo {
public static final Foo INSTANCE_1 = new Foo();
public static final Foo INSTANCE_2 = new Foo();
public static synchronized method1() { /* really long execution */ }
public synchronized method2() { /* really long execution */ }
public static method3() { /* really long execution */ }
public method4() { /* really long execution */ }
public static synchronized method5() { /* really long execution */ }
public synchronized method6() { /* really long execution */ }
}
Now pretend that there are these threads running on a 64000-core machine. Each thread, at the exact same picosecond, makes a call to the method listed:
| Thread | Method call |
|---|---|
| Thread 101 |
|
| Thread 102 |
|
| Thread 103 |
|
| Thread 104 |
|
| Thread 105 |
|
| Thread 106 |
|
| Thread 111 |
|
| Thread 112 |
|
| Thread 113 |
|
| Thread 114 |
|
| Thread 115 |
|
| Thread 116 |
|
| Thread 201 |
|
| Thread 202 |
|
| Thread 203 |
|
| Thread 204 |
|
| Thread 205 |
|
| Thread 206 |
|
| Thread 211 |
|
| Thread 212 |
|
| Thread 213 |
|
| Thread 214 |
|
| Thread 215 |
|
| Thread 216 |
|
| Thread 301 |
|
| Thread 302 |
|
| Thread 303 |
|
| Thread 304 |
|
| Thread 305 |
|
| Thread 306 |
|
Questions:
- Now which threads will block each other and why?
- Where are locks stored? (The JVM has to store the information some place!)
- What is a deadlock?
- When do deadlocks occur? Does the above code experience a deadlock?
No, the answer is not going to be given.
This post is labeled Part 1 because there will be more rants in this area I am sure!
Update: 12 April 2011: more detailed!
Pingback: why people bother to interview here – if you can’t teach (Part 2) « Just wondering….