Credit Suisse Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
if its synchronized lock is automatically removed and other thread continues to execute. if there is Lock mechanism we need the remove the lock explicitly in finally block. since there is no exception handling done this thread will have the lock and the program hangs. correct me if am wrong
No. Other threads will continue to run without impact.
You can test it with following program. It does not matter if its an Checked - Unchecked Exception / Error
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadLocalExample {
static int i=0;
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
ThreadLocal<String> threadLocal = new ThreadLocal<String>();
ThreadLocal<Long> threadLocalInt = new ThreadLocal<Long>();
threadLocal.set(Thread.currentThread().getName());
threadLocalInt.set(Thread.currentThread().getId());
while(true){
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("I am Thread "+Thread.currentThread().getName()+ " with variable "+threadLocal.get()+" Integer "+threadLocalInt.get());
}
}
};
Runnable runnableException = new Runnable() {
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
throw new Error("I am getting killed "+Thread.currentThread().getName());
}
};
ExecutorService service = Executors.newFixedThreadPool(5);
service.execute(runnableException);
service.execute(runnable);
service.execute(runnable);
service.execute(runnable);
service.execute(runnable);
service.shutdown();
}
}
No. As thread is running in process address space, it will terminate you process.
Here is the linux code where thread 2 wont reach at the second printf.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
static void *thread_fun1 (void *arg)
{
char *msg = (char *) arg;
char *p = NULL;
printf (" Thread Func : Message = \"%s\"\n",msg);
*p='a';
}
static void *thread_fun (void *arg)
{
char *msg = (char *) arg;
int i=0;
printf (" Thread Func : Message = \"%s\"\n",msg);
while ( i++ < 10000) sleep (1000);
printf (" Thread Func : Message = \"%s\"\n",msg);
}
int main ()
{
pthread_t thread1, thread2;
const char *msg1=" Thread 1";
const char *msg2=" Thread 2";
int iret1, iret2;
/* concrete independent threads each of which will execute function */
iret1 = pthread_create (&thread1, NULL, thread_fun, (void *) msg1);
if (iret1) {
fprintf (stderr, "Error : pthread_create () return code %d\n", iret1);
exit (EXIT_FAILURE);
}
/* concrete independent threads each of which will execute function */
iret1 = pthread_create (&thread2, NULL, thread_fun1, (void *) msg2);
if (iret1) {
fprintf (stderr, "Error : pthread_create () return code %d\n", iret1);
exit (EXIT_FAILURE);
}
pthread_join (thread1, NULL);
pthread_join (thread2, NULL);
printf (" At the End\n");
}
Yes. It will terminate the whole program. Other threads will also stop.
- Brandy May 23, 2014