/* related posts with thumb nails */

Multithreading:

1. What is multithreading?

Multithreading is a programming ability to perform multiple process in parallel based on time-sharing technique.

2. What is a thread?

Thread is a task or flow of execution that can be made to run using time-sharing principle.

3. What are lightweight processes and heavyweight processes?

Lightweight Process: A thread is similar to a separate process, in that it can run independently of other threads. But it is lightweight, since the operating system doesn't have to give it its own memory space, and it shares memory with the other threads in the process. It runs within the context of a program because threads or sub-programs of a main application program.

Heavyweight Process: In heavyweight processes in between threads that belong to different programs. They demand separate memory.

4. What are the different ways of creating threads.

In java, we can create threads in two different ways.

1. By extending Thread class: Here, we create a thread class that extends Thread class and override its run() method with the code required by the thread. It includes the following steps:

Declare the class as extending the Thread class.

Override the run( ) method that defines the task of the thread

Create a thread object and call the start( ) method to initiate the thread execution.

2. By implementing Runnable interface: Here, we define a class that implements Runnable interface. The Runnable interface has only one method, run( ), that is to be implemented by the class. It includes the following steps:

Declare a class that implements Runnable interface.

Implement the run( ) method.

Create a thread by defining an object that is instantiated from this "runnable" class or create it within that class.

Call the thread's start( ) method to run the thread.

5. What are Thread methods

start(), stop(), run(), getName(), setName(), getPriority(), setPriority(), isAlive(), join(), wait(), suspend(), resume(), notify(), sleep(), yield()

6. What is the Life cycle of a thread?

During the life time of a thread, there are many states it can enter. They include:

1. Newborn state

2. Runnable state

3. Running state

4. Blocked state

5. Dead state

7. What are Thread Priorities?

The threads of the same priority are given equal treatment by the Java scheduler and, therefore, they share the processor on a first-come, first-serve basis. The Thread class defines several priority constants. The default setting is NORM_PRIORITY.

MIN_PRIORITY = 1

NORM_PRIORITY = 5

MAX_PRIORITY = 10

8. What is synchronization?

Synchronization allows us to lock a method or a block of code in order to execute it completely without being interrupted by other competing threads.

In case of Java, the keyword synchronized keeps a watch on a function. For example, the method that that will update a file may be declared as synchronized as shown below:

synchronized void update( )

{

………….. // code here is synchronized

}

When we declare a method synchronized, Java creates a "monitor" and hands it over to the thread that calls the method first time. As long as the thread runs, no other thread can enter the synchronized section of code i.e. other threads cannot interrupt this thread with that object until its complete execution. It is like locking a function.

9. What is dead-lock situation?

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. This results in a situation called as deadlock. But, java automatically recognizes this situation and terminates some processes automatically to ensure safer execution.

Related Topics:

0 comments:

Post a Comment