Java Threads Interview Questions And Answers for freshers and experienced

Threading Interview questions And Answers for freshers as well 1 to 3 year experienced so Why threads? – Need to handle concurrent processes
Definition – Single sequential flow of control within a program – For simplicity, think of threads as processes executed by a program
– Example:
Operating System
HotJava web browser


Threading interview questions and answers for freshers and experienced below

Questions : 1 What’s difference between thread and process?
Answers : 1

A thread is a path of execution that run on CPU, a process is a collection of threads that share the same virtual memory. A process has at least one thread of execution, and a thread always run in a process context. A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes. One more significant difference between process and thread is every process has its own data memory location but all related threads can share same data memory and have their own individual stacks. Thread is a light weighted process, collection of threads become process.

   
Questions : 2 What is thread safety and synchronization ?
Answers : 2

Thread safe is used to describe a method that can run safely in multithreaded environments allowing accessing to data in a safe and efficient way. Thread safety is achieved by synchronization. Synchronization assures that the object data is not accessed by multiple threads at the same time.

   
Questions : 3 What is semaphore ?
Answers : 3

Semaphore is an object which helps two threads to communicate with one another in order to synchronize there operation.

   
Questions : 4 What are monitors ?
Answers : 4

Monitor is a body of code which can be executed by only one thread at a time. If any other thread tries to get access at the same time, it will be suspended until the current thread releases the Monitor.

   
Questions : 5 How do we create threads ?
Answers : 5

There are two ways to create a thread.

extend the java.lang.Thread class.
Create a class which extends the thread class. When the class is instantiated, the thread and object are created together and the object is automatically bound to the thread. By calling the object's start() method, the thread is started and immediately calls the object's run() method. implement the java.lang.Runnable interface
Create the thread and supply it an object with a run() method. This object will be permanently associated with the thread. The object's run() method will be invoked when the thread is started. This method of thread creation is useful if you want many threads sharing an object. .

   
Questions : 6 what’s the difference in using runnable and extends in threads?
Answers : 6

Main differences between threads using runnable interface and by extending the thread class are
Thread is a class and Runnable is an interface in java.
If a class is not extending another class we can use either extending Thread class or implementing Runnable interface. If our class is already extending another class we can't extend Thread class because java doesn't support multiple inheritance so we have left only one choice that is implementing Runnable interface.
When you extend a thread each of your threads has a unique object associate it, whereas with Runnable, many threads share the same object instance.

   
Questions : 7 explain Thread.sleep?
Answers : 7

Thread.sleep() is a static method of the Thread class that causes the currently executing thread to delay for a specified number of milliseconds.

   
Questions : 8 How to stop a thread ?
Answers : 8

By use of thread.stop;

   
Questions : 9 What is wait() and notify() ?
Answers : 9

By using wait() and notify(), a thread can give up its hold on a lock at an arbitrary point, and then wait for another thread to give it back before continuing. By executing wait() from a synchronized block, a thread gives up its hold on the lock and goes to sleep.Later, when the necessary event happens, the thread that is running it calls notify() from a block synchronized on the same object. Now the first thread wakes up and begins trying to acquire the lock again.

   
Questions : 10 explain Yielding in threading?
Answers : 10

It causes the currently executing thread object to temporarily pause and allow other threads to execute.

   
Questions : 11 what are daemon threads ?
Answers : 11

Daemon threads are designed run in background. One example of a daemon thread is the garbage collector thread. You can use setDaemon() to mark a thread as daemon.

   
Questions : 12 explain how Scheduling and Priority works in threads?
Answers : 12

Every thread has priority.If, at any time, a thread of a higher priority than the current thread becomes runnable, it preempts the lower priority thread and begins executing. By default, threads at the same priority are scheduled round robin, which means once a thread starts to run

   

Search More interview questions and answers on Threading

advertisements
Ask your interview questions on Threads

Write Your comment or Questions if you want the answers on Threads from Threads Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---