Multi Threading
Multi Threading
In preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. In time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors. Briefly explain daemon thread. Daemon thread is a low priority thread which runs in the background performs garbage collection operation for the java runtime system.
Java multithreading
5. What is timeslicing?
Timeslicing is the method of allocating CPU time to individual threads in a priority schedule.
10. Name the mechanism defined by java for the Resources to be used by only one Thread at a time.
Synchronisation
11. Data type for the parameter of the sleep() method is ____________
long
12. Provide the values for max-priority, min-priority and normal-priority level.
10,1,5
14. Name the default thread at the time of starting the program.
main thread
But a thread of execution is an individual light weight process that has its own call stack. Even if you don't create any new threads in your program, threads are there running in background. The main() method runs the main thread. So in main call stack the main() method would be the first. When a new thread is created then it will have its own stack separate from the main() call stack.
19) Can you tell some ways in which a thread can enter the waiting state?
A thread can enter the waiting state by the following ways: We can invoke sleep() method of the thread. An attempt to acquire the objects lock can put the thread in waiting mode. We can also invoke wait() method of the thread. A thread can also be entered in waiting state by invoking its suspend() method.
20) What the difference is between notify and notify All methods?
The notify will run only first thread which has called wait() on same object. i.e. the object which has called the wait() must be the same as the object that calls The notifyAll will run all threads which has called wait() on same object. While using notifyAll the highest priority thread will run first.
22) What is the difference between preemptive scheduling and time slicing?
In Preemptive scheduling, highest priority task will executes until it enters in waiting or dead states. It also executes, until a higher priority task enters. In Time slicing, a task will execute for a fixed time slice and after that it will go in ready state. At that time the scheduler will find the executable task, according to the priority and various other tasks. In preemptive scheduling, the running task will be preempted by the higher priority task. In time slicing methods, a task executes until the specified period of time. Once the execution of that task is complete then the higher priority task will be executed if
available.
which it is created. Thread can access the data segment of its process directly while processes can access their own copy of the data segment of its parent process. Threads have almost no overheads while processes may have overheads. We can create a new thread easily but to create new process we have to duplicate the parent process. If any changes in main thread are occurred it can affect the behavior of the other threads of the same process. While in case of process if any changes is occurred in parent process it wont affect the behavior of parent process. The direct communication is possible between threads of same process while in case of same sibling processes only interprocess communication is possible.
30) What happens if we invoke run method without calling the start method for a thread instance?
If we instantiate a thread it is called in new state until the Start() method is called. If we don't call a start() method for that thread instance, the thread is not called alive. If we invoke run method without calling the start method for a thread instance, the code in run() method wil not be executed by a new thread but it will be executed by the existing thread only.
For example public class MyThread implements Runnable{ public void run(){ // code to execute under the thread } public static void main(String [] args){ MyThread c = new NewThread(); Thread t = new Thread(c); t.start(); } }
33) What are the methods of the thread class used to schedule the threads?
The methods of the thread class used to schedule the threads are as follows: -public -public -public -public -public -public -public final void join() throws InterruptedException final void notify() final void notifyAll() static void yield() final void setPriority(int priority) static void sleep(long millis) throws InterruptedException final void wait() throws InterruptedException
} private static ThreadLocalConnection con = new ThreadLocalConnection(); public static Connection MyConnection() { return (Connection) con.get(); } }
35) How do you debug your application for issues when multiple threads are being executed?
Following are some way to debug issues in multi-threaded applications in Java. -By using logging and print statements along with thread names. In this way we can know about the flow of thread execution. -With the use of debugging functionality available in Eclipse and JDeveloper. -We can write a thread dump of the application which will give the information about the active threads at a point of time. This is most effective way for detecting deadlocks in production systems.
In Java, Starvation can be caused by inappropriate allocation of thread priorities. A thread with low priority can be starved by the threads of higher priority if the higher priority threads do not release shared resources time to time.