Showing posts with label multithreading faqs. Show all posts
Showing posts with label multithreading faqs. Show all posts

Java threading interview Questions

Java threading interview Questions
1 What are the two types of multitasking?
Ans:a. Process-based.
       b. Thread-based.
2 .What is a Thread?
Ans: A thread is a single sequential flow of control within a program.
3 what are the two ways to create a new thread?
Ans: A.Extend the Thread class and override the run () method.
B.Implement the Runnable interface and implement the run () method.
4 If you have ABC class that must subclass XYZ class, which option will you use to create a thread?
Ans: I will make ABC implement the Runnable interface to create a new thread, because ABC class will not be able to extend both XYZ class and Thread class.
5 which package contains Thread class and Runnable Interface?
Ans: java. Lang package
6 what is the signature of the run () mehod in the Thread class?
Ans: public void run ()
7 Which methods calls the run () method?
Ans: start () method.
8 which interface does the Thread class implement?
Ans: Runnable interface
9 what are the states of a Thread?
Ans: Ready, Running, Waiting and Dead.
10 where does the support for threading lie?
Ans: The thread support lies in java.lang.Thread, java.lang.Object and JVM.
11 In which class would you find the methods sleep () and yield ()?
Ans: Thread class
12 In which class would you find the methods notify (), notify All () and wait ()?
Ans: Object class
13 what will notify () method do?
Ans: notify() method moves a thread out of the waiting pool to ready state, but there is no guaranty which thread will be moved out of the pool.
14 Can you notify a particular thread?
Ans: No.
15 what is the difference between sleep () and yield ()?
Ans: When a Thread calls the sleep () method, it will return to its waiting state. When a Thread calls the yield () method, it returns to the ready state.
16 what is a Daemon Thread?
Ans: Daemon is a low priority thread which runs in the backgrouund.
17 How to make a normal thread as daemon thread?
Ans: We should call set Daemon (true) method on the thread object to make a thread as daemon thread.
18 what is the difference between normal thread and daemon thread?
Ans: Normal threads do mainstream activity, whereas daemon threads are used low priority work. Hence daemon threads are also stopped when there are no normal threads.
19 Give one good example of a daemon thread?
Ans: Garbage Collector is a low priority daemon thread.
20 what does the start () method of Thread do?
Ans: The thread’s start () method puts the thread in ready state and makes the thread eligible to run. Start () method automatically calls the run () method.

Multithreading vs Multiprocessing,Difference between multithreading and Multiprocessing

Multi-threading refers to an application with multiple threads running within a process, while multi-processing refers to an application organised across multiple OS-level processes.
A thread is a stream of instructions within a process. Each thread has its own instruction pointer, set of registers and stack memory. The virtual address space is process specific, or common to all threads within a process. So, data on the heap can be readily accessed by all threads, for good or ill.
Multi-threading is a more "light weight" form of concurrency: there is less context per thread than per process. As a result thread lifetime, context switching and synchronisation costs are lower. The shared address space (noted above) means data sharing requires no extra work.
Multi-processing has the opposite benefits. Since processes are insulated from each other by the OS, an error in one process cannot bring down another process. Contrast this with multi-threading, in which an error in one thread can bring down all the threads in the process. Further, individual processes may run as different users and have different permissions.

what is deadlock?

what is deadlock?
Deadlock is permanent blocking of the set processes that
either compete for system resources or communicate each
other.
we can avoid deadlock by avoiding the following conditions:
1.Mutual Exclusion
2.Hold and wait.
3.No preemption.
4.circular wait.

Difference between a Thread and a Process?- C# interview faqs

Difference between thread and process
Threads are similar to processes, but differ in the way that they share resources. Threads are distinguished from processes in that processes are typically independent, carry considerable state information and have separate address spaces. Threads typically share the memory belonging to their parent process.