35 odd questions about Java threads

Feel free to add your own.

 


0- Unrealted warmup question:

Will the folowing code compile?

import java.util.ArrayList; 
/**
 * Created by IntelliJ IDEA.
 * User: sk
 * Date: 7/20/11
 * To change this template use File | Settings | File Templates.
 */
public class Test {

    public <ArrayList> ArrayList strangeMethod(){        
        return null;
    }

 


1-How does one generate a Thread dump?

2-What is the default (stack) memory allocated for each thread on a JVM on a 64 bit Windows operation system?

3-What is the most probable runtime error when running a JVM with 500 threads and setting JAVA_OPTIONS ="-Xss10m" ?
(same as 3 only with "-Xss64k")

4-Given a thread dump how does one detect a deadlock?

5- What does the following command do:

Collections.synchronizedSet(new HashSet());   


6-Provide an example for using the following code snippet:
 

private ThreadLocal tc= new ThreadLocal(); 



7-What can you conclude given the following stack trace of a single thread:
 

at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156)



8-Under which circumstances would you opt to create your own ThreadFactory?

9-Can you suggest a scenario in which the following code is useful?
 

private static final ExecutorService pool = Executors.newScheduledThreadPool(13,new UsefulThreadFactory());

    static class UsefulThreadFactory implements ThreadFactory {
        AtomicInteger threadCount = new AtomicInteger(1);

        public  Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setName("UsefulThreadFactory-ThreadPool[" + threadCount.getAndIncrement() + "]");
            return t;
        }
    }


10-What is the use of a CyclicBarrier?

11-Given:

private  CyclicBarrier barrier;

try
            {
                barrier.await();
            } catch (InterruptedException ex)
            {
                ex.printStackTrace();
            } catch (BrokenBarrierException ex)
            {
                ex.printStackTrace();
            }


Under which circumstances will we reach the second Exception (e.g. BrokenBarrierException)?

12- Use a CountDownLatch to coordinate the start of N threads.

13- Given class A in , and two static methods static Syncronized void foo() and static Syncronized  void bar(), can you invoke both methods concurrently from two different threads? if no, which object is being locked?

14- describe precisely what would be the effect of invoking:

setDaemon(boolean isDaemon);


On a thread.

15-Assuming an 64bit Linux OS kernel with 2GB RAM and 8 CPU's out of which 500m are allocated/reserved to the kernel itself, and given the following JAVA_OPTS passed as a parameter to the JVM, will a JVM be successfully created? 

 

-XX:PermSize=512m -Xms1g -Xmx1g -XX:NewSize=256m -XX:MaxNewSize=756m -XX:SurvivorRatio=6 -XX:GCTimeRatio=2 -XX:ParallelGCThreads=8 -XX:+UseParNewGC -XX:MaxGCPauseMillis=2000 -XX:+DisableExplicitGC


16- What does the following command do:

jstack [-l] <pid>


17-Can you disable garbage collection in java?

18- What is the distributed RMI garbage collector?

19-What does the following command do?

jstat -gcutil -t <pid> 1s 30


20- What is the use of the following code?
 

ThreadMXBean threadMXBean =ManagementFactory.getThreadMXBean();


21- Is this a blocking call?

Thread.sleep(500)


22-How can you block a thread?

23- Given the following declaration, behind the scenes,how many threads can be queued if all the threads are currently busy serving requests?
 

ExecutorService pool = Executors.newFixedThreadPool(3);


24-What is the use of a RejectedExecutionHandler?

25- Are the operations on ArrayBlockingQueue/ LinkedBlockingQueue thread safe?

26-When would you opt using a CompletionService whreas using an ExecutorService?

27- What are the benefits of ConcurrentHashMap over a regular synchronized HashMap if any?

28- What is a thread-local variable?

29- How do you AVOID sharing a variable between threads?

30-Can you infer the usage for the following declaration?
 

private ConcurrentMap<Integer, FutureTask<Action>>    cache   = new ConcurrentHashMap<Integer, FutureTask<Action>>();



31-Explain what is a Java binary mutex

32-Suggest a way to create a non-blocking thread in Java using a Callable<T> .

33-Suggest a way to create a blocking thread in Java using a Runnable.

34-Suggest a way to synchronize access to a BlockingQueue without using the synchronized keyword

35-Explain what is an AtomicInteger and how to use it.

 

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com