Java Multithreading Interview Questions and Answers for Freshers - 2

Question: 6

What is the use of volatile in Java?

Threads are allowed to hold the values of variables in local memory (e.g. in a machine register)

If a variable is marked as volatile, every time when the variable is used, it must be read from the main memory, similarly every time the variable is written, the value must be stored in main memory.

Question: 7

What is the difference between multithreading and multitasking?

A Web Server understands and support only HTTP protocol whereas the Application Server supports HTTP, TCP/IP and many more protocols.

Also many more features such as Caches, Clusters, Load Balancing are there in Application Servers which are not available in Web Servers.

We can also Configure Application Servers to work as Web Server.

In Application Server is a super set of which Web Servers is a subset.

Question: 8

What is the difference between process and thread?

Process

Thread

A program

Can run on its own

Unit of allocation

Have its own memory space

Each process has one or more tables

Expensive, need to context switch

More sucure. One process cannot corrupt another process.

Similar to a sequential program

Cannot run on its own

Unit of execution

Share with others

Each thread belongs to one process

Cheap, can use process memory and may not need to context switch

Less secure. A thread can write the memory used by another thread.

 

Question: 9

Is there a separate stack for each thread in java?

No

Question: 10

Can a lock be acquired on a class?

Yes, a lock can be acquired on a class. This lock is acquired on the class’s Class object.

Related Questions