Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'Thread' - 59 question(s) found - Order By Rating | ||||
![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Java provides its own implementations of the thread pool pattern, through objects called executors. These can be used through executor interfaces or directly through thread pool implementations which does allow for finer-grained control. The java.util.concurrent package contains the following interfaces: Executor : a simple interface for executing tasks.ExecutorService a more complex interface which contains additional methods for managing the tasks and the executor itself. ScheduledExecutorService: extends ExecutorService with methods for scheduling the execution of a task.Alongside these interfaces, the package also provides the Executors helper class for obtaining executor instances, as well as implementations for these interfaces. Generally, a Java thread pool is composed of: The pool of worker threads, responsible for managing the threads. A thread factory that is responsible for creating new threads. A queue of tasks waiting to be executed. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Context Switching is the process of storing and restoring of CPU state so that Thread execution can be resumed from the same point at a later point of time. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Synchronization, Concurrent classes, Volatile keyword, Implementing concurrent Lock interface, Immutable classes | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Thread Scheduler is the Operating System service that allocates the CPU time to the available runnable threads. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Time Slicing is the process to divide the available CPU time to the available runnable threads. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Every process in it's timeline require different resources. Utilization of resources can be optimized when they are shared among different processes or threads. When one thread is sleeping waiting for a peripheral to complete (e.g. a disk write, or a key press from the keyboard), other threads can continue using processor time and hence would lead to better usage of resources. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. We can use HashMap for tracking response status for all threads. We can wait every n second by using Thread.sleep and exit the main thread only once we have received response for all threads. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. By using Executor Framework , we can create Thread pool | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. https://howtodoinjava.com/java/multi-threading/writing-a-deadlock-and-resolving-in-java/ | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. By using synchronized static method or synchronized block | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. ConcurrentHashMap | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Each has it's own advantages Batching requires less resources but may result in loosing whole batch in case of failure whereas concurrency even though is little more expensive in terms of resources but would result in minimal loss in case of failure. In case messages are to be consumed in a particular order, batching them in that order and then consuming them makes better sense. if incoming messages are not continuous , it makes more sense to do concurrency as we need not wait for all messages to form a batch and flush. Though time sensitivity can be added but that would add unnecessary complexity. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Games are very good examples of threading.You can use multiple objects in games like cars, motor bikes, animals, people etc. All these objects are nothing but just threads that run your game application. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Through common memory area. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. After a thread is started using a call to start method, JVM invokes the thread’s run method when the thread is initially executed. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. public class TotalUsingThreads extends Thread{ private static List<TotalUsingThreads> collector = new ArrayList<TotalUsingThreads>(); private int startFrom = 0; private Integer threadTotal = null; Test(int startFrom){ this.startFrom = startFrom; } public static void main(String[] args) throws InterruptedException{ int totalSum = 0; // Create all the threads and set the count starting point for all of them for(int count=0;count<10;count++){ TotalUsingThreads newThread = new TotalUsingThreads(count*100); newThread.start(); collector.add(newThread); } boolean allCollected = false; // Make sure that all thread totals are collected and all threads have completed their tasks while(!allCollected){ for(int count=0;count<10;count++){ if(collector.get(count).threadTotal == null){ Thread.sleep(100); break; } } allCollected = true; } // sum totals of all threads for(int count=0;count<10;count++){ totalSum += collector.get(count).threadTotal; } System.out.println(totalSum); } public void run(){ threadTotal = 0; for(int count=startFrom;count<startFrom+100;count++){ threadTotal += count; } } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No we cannot start the same thread twice. Each thread has a lifecycle. But Yes, we can run the same code in parallel using different threads. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. Synchronize is used to achieve mutual exclusion i.e at one time, the segment of the code, method that has been declared synchronized should be executed by single thread only and hence the lock needs to be retrieved before executing the segment and then released. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Just the method gets locked. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. 1. Switching Overheads - Even though multi threading aims at improving performance by reducing the wait time and hence improving overall throughput, there is a cost of switching resources between threads and sometime this cost can surpass the benefits if there isnt much wait for IO or external communication. 2. Debugging is hard with multi threaded code. 3. Deadlock - Execution of multi threaded code many a times lead to deadlock due to shared resources. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. 1. when an OS wants to start running program it creates new process means a process is a program that is currently executing and every process has at least one thread running within it. 2). A thread is a path of code execution in the program, which has its own local variables, program counter(pointer to current execution being executed) and lifetime. 3. When the JavaVirtual Machine (JavaVM, or just VM) is started by the operating system, a new process is created. Within that process, many threads can be created. 4. Consider an example : when you open Microsoft word in your OS and you check your task manger then you can see this running program as a process. now when you write something in opened word document, then it performs more than one work at same time like it checks for the correct spelling, it formats the word you enter , so within that process ( word) , due to different path execution(thread) all different works are done at same time. 5. Within a process , every thread has independent path of execution but there may be situation where two threads can interfere with each other then concurrency and deadlock come is picture. 6. like two process can communicate ( ex:u open an word document and file explorer and on word document you drag and drop another another file from file explorer), same way two threads can also communicate with each other and communication with two threads is relatively low. 7. Every thread in java is created and controlled by unique object of java.lang.Thread class. 8. prior to jdk 1.5, there were lack in support of asynchronous programming in java, so in that case it was considered that thread makes the runtime environment asynchronous and allow different task to perform concurrently. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. 1. Multithreading provides better interaction with the user by distribution of task 2. Threads in Java appear to run concurrently, so it provides simulation for simultaneous activities.The processor runs each thread for a short time and switches among the threads to simulate sim-ultaneous execution (context-switching) and it make appears that each thread has its own processor.By using this feature, users can make it appear as if multiple tasks are occurring simultaneously when, in fact, each is running for only a brief time before the context is switched to the next thread. 3. We can do other things while waiting for slow I/O operations.In the java.iopackage, the class InputStreamhas a method, read(), that blocks until a byte is read from the stream or until an IOExceptionis thrown. The thread that executes this method cannot do anything elsewhile awaiting the arrival of another byte on the stream. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||