Créer un site internet

Java Concurrency

1.What is the difference between process and thread?

Process is a program in execution. Process has its own memory space and has at least one thread.

Threads are light-weight processes within a process (the smallest unit of processing). Thread uses the process's resources and share it with other threads of that process.

Process communicate with each other using Inter process communication (IPC) whereas thread can communicate directly with other threads of the same process using inter thread communication which is much cheaper than Inter process communication IPC.

2. The benefits of threads:

     2.1 Resource utilization: 

When using single threaded program in single process system, the processor will be blocked while waiting for synchronous I/O operations to complete. It is better to use that wait time to run other independent operations. 

    2.2 Improve responsiveness: 

For example, when a loop takes too long to execute, the user GUI appears to be blocked until the running operation is completed. So, threads are very important to improve the responsiveness of user interface in Gui applications. 

3. What is the difference between user and deamon threads?

The main difference between user and daemon threads is what happens when it exits. User threads outlive the main thread i.e if any of the user thread still running, the JVM will not shutdown. Unlike user threads, the daemon threads do not prevent the JVM from shutting down. Daemon threads will exist when the main thread exits. In order for an application to continue running, it must always have at least one live user thread.

4. What is thread safety:

A program is thread safe when it behaves correctly in a concurrent environment. In the absence of proper synchronization, concurrent programs' can be corrupted due to:  

     4.1 Race conditions : occurs when an application depends on the sequence or timing of threads in order to operate properly. There are two common types of race condition  check-then-act and read-modify-write. We will explain an example of check-then-act type.

The reference example of check-then-act  race condition example is lazy initialization . The method getInstace() checks if the instance is null then it creates a new instance, otherwise it returns the existing instance. The problem that could occur in this example is when two threads or more access to the getInstance() simultaneously. When  thread 1 is initializing the instance after a null check, it is possible that the second thread gets also a null from the check so thread 2 initializes another new instance. The two threads instantiate two different instances even though only one instance is supposed to be used. 

     4.2.  Reordering and visibility:

Reordering occurs when the order of the written code in the program is changed by the JVM during at compile time or runtime which causes unexpected or incorrect results.

In the above example, the main thread and the reader thread share data without synchronization. When the expected and obvious result to be printed is 42. In fact, it is possible to obtain different results.

a.  The program print 0 instead of 42 because the write to  ready was made visible to the reader thread before the write to number --> Reordering.

b. The program never ends (it will never exit the while loop) because the writes to the variables number and ready are not visible to the reader thread --> Visibility. 

In order to execute correctly the concurrent program that sharing data, you must use synchronization.

 

 

 

 

 

Add a comment