Access over 20 million homework & study documents


Java Basic/Advanced Interview questions




Exam Practice


Showing Page:
Table of Contents

Sign up to view the full document!

lock_open Sign Up
1 Concurrency
Why do threads block on I/O?
Threads block on I/O (that is enters the waiting state) so that other threads may
execute while the I/O Operation is performed.
What is round-robin algo/Time slicing?
Round-robin (RR) is one of the simplest scheduling algorithms for processes in
an operating system. As the term is generally used, time slices are assigned to
each process in equal portions and in circular order, handling all processes
without priority. It is starvation free algo. In order to schedule processes fairly, a
round-robin scheduler generally employs time-sharing, giving each job a time
slot or quantum
(its allowance of CPU time), and interrupting the job if it is not
completed by then. The job is resumed next time a time slot is assigned to that
What is Preemptive scheduling
Preemptive scheduling enables the highest priority task execution until waiting
or dead states entered. It also executes, until a higher priority task enters.
What is daemon thread
A daemon thread is a thread, that does not prevent the JVM from exiting when
the program finishes but the thread is still running. An example for a daemon
thread is the garbage collection.
You can use the setDaemon() method to change the Thread daemon properties.
How interrupt works in Java?
An interrupt is an indication to a thread that it should stop what it is doing and
do something else. It's up to the programmer to decide exactly how a thread
responds to an interrupt, but it is very common for the thread to terminate.
How does a thread support its own interruption? This depends on what it's
currently doing. If the thread is frequently invoking methods that throw
InterruptedException, it simply returns from the run method after it catches
that exception.
Many methods that throw InterruptedException, such as sleep, are designed to
cancel their current operation and return immediately when an interrupt is
What if a thread goes a long time without invoking a method that throws
InterruptedException? Then it must periodically invoke Thread.interrupted,
which returns true if an interrupt has been received.

Sign up to view the full document!

lock_open Sign Up
  
What is difference between CyclicBarrier and CountdownLatch in Java ?
New java thread interview questions mostly to check familiarity with JDK 5
concurrent packages. One difference is that you can reuse CyclicBarrier once
barrier is broken but you can not reuse ContdownLatch.
What is difference between thread and process?
Thread: light weight, thread shares process resources like all threads share
memory assigned to process
Process: Process can have multiple threads. Different tasks with in a process
may be assigned to different threads, heavy weight, memory is assigned to one
process separately and it doesn’t share memory with other process.
Thread vs daemon thread?
A daemon thread runs in background and doesn’t prevent JVM from
terminating. When there are no user threads running, JVM shutdown the
program and quits. A child thread created from daemon thread is also a daemon
Can we call run() method of a Thread class?
Yes, we can call run() method of a Thread class but then it will behave like a
normal method. To actually execute it in a Thread, we need to start it
using Thread.start() method.
What do you understand about Thread Priority?
Every thread has a priority, usually higher priority thread gets precedence in
execution but it depends on Thread Scheduler implementation that is OS
dependent. We can specify the priority of thread but it doesn’t guarantee that
higher priority thread will get executed before lower priority thread. Thread
priority is an int whose value varies from 1 to 10 where 1 is the lowest priority
thread and 10 is the highest priority thread.
What is context-switching in multi-threading?
Context Switching is the process of storing and restoring of CPU state so that
Thread execution can be resumed from the same point at a later point of time.
How does thread communicate with each other?
When threads share resources, communication between Threads is important
to coordinate their efforts. Object class wait(), notify() and notifyAll() methods
allows threads to communicate about the lock status of a resource.

Sign up to view the full document!

lock_open Sign Up

Really great stuff, couldn't ask for more.