What is a thread? What are the advantages we derived by programming with thread?
Threads allow programs to execute simultaneously. A thread is an independent path of execution in a program. These threads can be executed synchronously or asynchronously. All threads have a priority attached. Thread with a higher priority is executed first.
Allows multiple programs to be executed concurrently
Cost of thread is low compared to processes in terms of space and communication.
Threads are lightweight.
Difference between Thread and Process in Java?
Thread is subset of Process, in other words one process can contain multiple threads. Two process runs on different memory space, but all threads share same memory space. Don't confuse this with stack memory, which is different for different thread and used to store local data to that thread.
Explain the term thread safety and synchronization.
The term Thread safety means each method in a multithreaded environment doesn’t access data by multiple threads at the same time. Synchronization is the way to provide a secured environment and thus ensure Thread Safety.
What is blocking method in Java?
A blocking method is a method which blocks until task is done, for example accept() method of ServerSocket blocks until a client is connected. here blocking means control will not return to caller until task is finished. On the other hand there are asynchronous or non-blocking method which returns even before task is finished.
Explain how to create a thread and start it running.
Creating a thread:
Declare a class as a sub class of Thread
Class SampleThread extends Thread
SampleThread( Long minSample);
Public void run()
Program goes here
Run the thread: An instance is created to start the thread.
SampleThread s = new SampleThread(100);