Threading Interview Questions | Glassdoor

Threading Interview Questions

10

interview questions shared by candidates

Threading Interview Questions

Sort: RelevancePopular Date

5. The user of this class expected the output “Num: 1000000”. They got something else. What went wrong and how would you fix it? public class Test { public final static int NUMTHREADS = 1000; public final static int NUMLOOP = 1000; public static int num = 0; static class Mythread extends Thread { @Override public void run() { for (int i = 0; i < NUMLOOP; i++) { ++num; } } } public static void main(String argv[]) { Mythread threads[] = new Mythread[NUMTHREADS]; for (int i = 0; i < NUMTHREADS; ++i) { threads[i] = new Mythread(); threads[i].start(); } try { for (int i = 0; i < NUMTHREADS; ++i) { threads[i].join(); } } catch (InterruptedException e) { } System.out.println("Num: " + num); } }

2 Answers

public class Test{ public final static int NUMTHREADS = 1000; public final static int NUMLOOP = 1000; public static int num = 0; static class Mythread extends Thread{ @Override public void run(){ for (int i = 0; i < NUMLOOP; i++){ add(); } } private synchronized static void add(){ ++num; } } public static void main(String argv[]){ Mythread threads[] = new Mythread[NUMTHREADS]; for (int i = 0; i < NUMTHREADS; ++i){ threads[i] = new Mythread(); threads[i].start(); } try{ for (int i = 0; i < NUMTHREADS; ++i){ threads[i].join(); } }catch (InterruptedException e){} System.out.println("Num: " + num); } }

package com.some; public class threadtest { public final static int NUMTHREADS = 1000; public final static int NUMLOOP = 1000; public static int num = 0; private static final Object OBJ_LOCK = new Object(); static class Mythread extends Thread { @Override public void run() { for (int i = 0; i < NUMLOOP; i++) { synchronized(OBJ_LOCK) { ++num; } } } } public static void main(String argv[]) { Mythread threads[] = new Mythread[NUMTHREADS]; for (int i = 0; i < NUMTHREADS; ++i) { threads[i] = new Mythread(); threads[i].start(); } try { for (int i = 0; i < NUMTHREADS; ++i) { threads[i].join(); } } catch (InterruptedException e) { } System.out.println("Num: " + num); } }

I don't remember exactly, but some standard low-level C/C++/Java questions. Maybe something about string manipulation, bit play, recursive algorithms, threads, networking, etc.

1 Answer

concurrency in java

1 Answer

I remember they asked a question about threading in .NET that seemed ambigious.

1 Answer

What is re-entrant code?

1 Answer

How do you implement a concurrent hash map?

1 Answer

What are different ways to create threads in Java?

1 Answer

What are the various ways an Android developer can apply multi-threading to their app? Be specific, and give examples.

1 Answer

Design a thread pool, threaded queue, any threaded resource producer/consumer structure

4. Implement a basic Thread Pool in Java. When it’s completed, discuss the trade offs of your implementation. If you had unlimited time, discuss what improvements you would make to it and how these improvements would help developers that used your thread pool.

110 of 10 Interview Questions