click below
click below
Normal Size Small Size show me how
Java Thread
Question | Answer |
---|---|
i.Deadlock:There are two threads waiting for each other to complete the task and none get executed. ii.Starvation:Due to high priority process, less priority process gets blocked Which of the above statements are correct? | i and ii |
Is Synchronized keyword the only reason that most of the times it leads to deadlock condition? | TRUE Explanation : Synchronized keyword is the only reason for deadlock situation, which allows only one thread that can access the resource at a given point of time. |
Which of the following method suspend the thread for mentioned time duration in argument? | Thread.sleep() Explanation : Method gives chance to other Threads for the execution. |
Can we detect the deadlock condition? | TRUE Explanation : Deadlock situations can be detected by running the executable code on cmd and subsequently collecting the thread dump. If it occurs, the cmd will throw up a message. |
Thread.yield() | Thread.yield()-When the thread which is in the process after releasing the lock gets a fair chance to occupy the C.P.U. and can get time to complete its execution till the original thread again gets the control over the C.P.U. |
Can we completely avoid deadlock in Java? | FALSE Explanation : We cannot completely avoid deadlock but can follow these measures to them. Avoid Nested Locks - avoid giving locks to multiple threads. Avoid Unnecessary Locks - lock is for important thread not for unnecessary threads. |
Thread.join | Using Thread Join- use Thread.join with a maximum time that a thread will take to avoid a situation where a thread waiting for other thread to release. |
Which of the following is said to occur when a particular thread does not get access to the object or the resource which leads to an increase in waiting and execution time? | Starvation Explanation : In Starvation, Threads will wait for each other. But waiting time is not infinite after some interval of time, waiting thread gets the resources whatever is required to execute thread run() method. |
A _______ is a low priority thread which always runs in the background. | Daemon thread |
Can we create threads in Java? | TRUE Explanation : We can create threads in two ways. By implementing Runnable interface and By extending Thread class |
Which method is used to return a boolean value to check if daemon thread is present or not? | isDaemon() |
What are the methods related to the thread priority. | public final int getPriority():java.lang.Thread.getPriority() method returns priority of the given thread public final void setPriority(int newPriority): java.lang.Thread.setPriority() method updates or assign priority of the thread to newPriority |
Is there any class in Java which helps us to create a thread? | Yes, there is a Thread class in java helps us to create a thread. Create a class and extend that class by Thread class. Then, the created class will act as thread in Java. After extending Thread class, the class should override the run method. |
Thread start() | After creating object for the class, we can use start() method to run the method. When the method gets started by calling start() method, a new thread is created and starts executing it. By this time, the main thread will continue the remaining part. |
If the value of the parameter "newPriority" of the method "getPriority()" goes out of the range (1 to 10), then the method throws _______. | IllegalArgumentException |
public class ThreadPriority extends Thread { public static void main(String argvs[]) { _____________________.setPriority(6); ThreadPriority th1 = new ThreadPriority(); } } | Thread.currentThread() |
Which of the following is the process of executing one or more threads simultaneously? | Thread is termed as a lightweight process. A process is divided into parts, each part called Thread. Multithreading is the process of executing one or more threads simultaneously. Threads will share a common memory and the execution will also be faster. |
Which scheduling algorithm is supported by the JVM to serve the thread with the highest priority first, since all Java threads have a priority. | Fixed-priority pre-emptive scheduling |
A _______ is a self-contained sequence of instructions that can execute in parallel with other threads that are part of the same root process. | thread |
Which of the following is TRUE. | All of the above A process is a part of the program in execution A core can have upto 2 threads and we can utilize the 2 threads based on the process A thread is a lightweight process |
Which of the following is/are correct statements. | All of the above A processor is madeup of multiple cores A core is physical component where threads are virtual component Thread is a virtual division of cores |
We can pass lambda expression while creating the object of Thread class instead of creating lambda expression of Runnable interface. | true |
Which method is used to find the state of the thread? | getState() |
Which method is used to check whether the thread is terminated or not? | isAlive() |
Which method is used to allow the thread to complete its execution while another thread is waiting? | join() |
Which method is used to name a thread? | setName() |
Which method is used to wake a thread from waiting state? | notify() |
When synchronized keyword is used before the method, the method becomes thread safe. | true |
All the methods by default is synchronized. | false |
Which method is used to pause the thread for require time? | sleep() |
Which method is used to move the current thread to the waiting state. | wait() |
Which method is used for inter communication between the threads? | wait() |
What is multi-threading? | |
In what ways can you create a thread? | run(), start()? |
List the methods in the Thread class and Runnable interface | |
Explain the lifecycle of the thread? | |
What is deadlock? | |
What is the synchronized keyword? | |
Which of the following are virtual machine states of a thread? | Runnable Blocked Terminated Waiting |
Which of the following are created on a per-thread basis? | JVM stack Native Stack PC Register |
Thread dumps are snapshots of the current state of all threads that are part of the process | true |
The run() method comes from the Thread class | false It comes from the Runnable functional interface |
Each thread gets its own stack and heap | false Threads receive their own stack, not their own heap |
Threads can be created by extending the Runnable class | false Runnable is an interface, not a class |
Which of the following Thread methods is used to wait for the thread to finish execution? | Thread.join(); |
What is a thread? | A representation of a path of execution, allowing for concurrent processing |
Threads can be created by | passing a Runnable into the constructor of Runnable: new Runnable(Thread) |
Which of the following is a Thread state in Java? | Blocked Waiting New |
Which one of the following is a valid class declaration? | class example implements Serializable, Runnable |
volatile | related to the visibility of variables that are modified by multiple threads at the time of concurrent programming. |