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.
Showing posts with label multithreading. Show all posts
Showing posts with label multithreading. Show all posts
Java Multi Threading Interview Questions
Java Multi Threads Interview Questions
- Do I need to use synchronized on setValue(int)? - It depends whether the method affects method local variables, class static or instance variables. If only method local variables are changed, the value is said to be confined by the method and is not prone to threading issues.
- Do I need to use synchronized on setValue(int)? - It depends whether the method affects method local variables, class static or instance variables. If only method local variables are changed, the value is said to be confined by the method and is not prone to threading issues.
- What is the SwingUtilities.invokeLater(Runnable) method for? - The static utility method invokeLater(Runnable) is intended to execute a new runnable thread from a Swing application without disturbing the normal sequence of event dispatching from the Graphical User Interface (GUI). The method places the runnable object in the queue of Abstract Windowing Toolkit (AWT) events that are due to be processed and returns immediately. The runnable object’s run() method is only called when it reaches the front of the queue. The deferred effect of the invokeLater(Runnable) method ensures that any necessary updates to the user interface can occur immediately, and the runnable work will begin as soon as those high priority events are dealt with. The invoke later method might be used to start work in response to a button click that also requires a significant change to the user interface, perhaps to restrict other activities, while the runnable thread executes.
- What is the volatile modifier for? - The volatile modifier is used to identify variables whose values should not be optimized by the Java Virtual Machine, by caching the value for example. The volatile modifier is typically used for variables that may be accessed or modified by numerous independent threads and signifies that the value may change without synchronization.
- Which class is the wait() method defined in? - The wait() method is defined in the Object class, which is the ultimate superclass of all others. So the Thread class and any Runnable implementation inherit this method from Object. The wait() method is normally called on an object in a multi-threaded program to allow other threads to run. The method should should only be called by a thread that has ownership of the object’s monitor, which usually means it is in a synchronized method or statement block.
- Which class is the wait() method defined in? I get incompatible return type for my thread’s getState( ) method! - It sounds like your application was built for a Java software development kit before Java 1.5. The Java API Thread class method getState() was introduced in version 1.5. Your thread method has the same name but different return type. The compiler assumes your application code is attempting to override the API method with a different return type, which is not allowed, hence the compilation error.
- What is a working thread? - A working thread, more commonly known as a worker thread is the key part of a design pattern that allocates one thread to execute one task. When the task is complete, the thread may return to a thread pool for later use. In this scheme a thread may execute arbitrary tasks, which are passed in the form of a Runnable method argument, typically execute(Runnable). The runnable tasks are usually stored in a queue until a thread host is available to run them. The worker thread design pattern is usually used to handle many concurrent tasks where it is not important which finishes first and no single task needs to be coordinated with another. The task queue controls how many threads run concurrently to improve the overall performance of the system. However, a worker thread framework requires relatively complex programming to set up, so should not be used where simpler threading techniques can achieve similar results.
- What is a green thread? - A green thread refers to a mode of operation for the Java Virtual Machine (JVM) in which all code is executed in a single operating system thread. If the Java program has any concurrent threads, the JVM manages multi-threading internally rather than using other operating system threads. There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations. Current JVM implementations make more efficient use of native operating system threads.
- What are native operating system threads? - Native operating system threads are those provided by the computer operating system that plays host to a Java application, be it Windows, Mac or GNU/Linux. Operating system threads enable computers to run many programs simultaneously on the same central processing unit (CPU) without clashing over the use of system resources or spending lots of time running one program at the expense of another. Operating system thread management is usually optimised to specific microprocessor architecture and features so that it operates much faster than Java green thread processing.
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.
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.
Subscribe to:
Posts (Atom)