Posts

‘volatile’ keyword in java programming

                 ‘ volatile ’ is a keyword used in java. It provides the changes that can be visible across the multiple threads . Most recent value is applicable for all threads. Features: Visibility across the multiple threads. Even though Caching, atomicity cannot be done, it is light weight and overhead is reduced. It makes latest value is read/written is visible. Use ‘volatile’ keyword when the following condition occurs… Multiple threads access the same variable for reading and writing. Singleton patterns are used. Thread safety is needed. Java program using ‘volatile’ keyword: class SFlag {     private volatile boolean f_run = true;     public void stop() {         f_run = false; // this statement makes this variable visible to all threads     }     public void doIt() {       ...

Synchronized block in java

                 It is a block of code which makes the program control takes over a single thread over multithreads in a critical region . Why synchronized block ? It deals with single thread. So, it improves the performance. Specific block is locked here. It considers a set of code instead of complete method. The race condition is effectively handled. Data inconsistency also improved. How to create a synchronized block? The code is given below. Code: synchronized ( lockObject ) {     // critical section code }   Let us create a java program to implement the counting application using synchronized block. Program: A class sample is created with private variable ‘s_incre’ and incre() function’s part of code as synchronized block. It increments the value ‘s_incre’. A function getIt() displays the counted value. In the main() function, object is created for sample class . Two threads are create...

Static synchronization

       Static synchronization makes a single thread to execute the static method. Generally, this method deals with class level itself. 👉Why static synchronization?   It provides the consistency for all objects in the class. It ensures the other threads does not interfere the thread using static data . Program: This program creates a class with static member variable and static method . In the main function, a single thread can access it at a time in class level, not object level. class increment {     static int inc = 0;     // Static synchronized method     public static synchronized void s_incre() {         inc++;         System.out.println("The value is incremented as " + inc);     } } public class SampleCode {     public static void main(String[] args) {    ...

Lock interface in thread synchronization

               It is the built-in interface in java.util.concurrent.locks package.it is very easy to handle the synchronization in a flexible way. It has three types of locking such as interruptible locking , try-locking and timed locking . Let us create a java program to illustrate the lock interface. ·        This program starts from including the built-in package. ·        A class is written as SResource. ·        It creates a lock object as reentrantlock . ·        A method “SafeMethod()” is act as critical region. It locks and unlocks the thread based on the situation. ·        At last, the thread is unlocked using finally block. ·        To get and display the output, the LockEg class is used. ·        It creates the object for ...

Thread synchronization in java

                This is another type of synchronization which deals with thread. Once the threads are created, it has to be coordinated and mange multithreading. The types of thread synchronization  are given below. ·        Inter thread communication ·        Mutual exclusive It can be achieved by following ways. Atomic classes Lock interface Synchronized block Static synchronization Volatile keyword Each and all types are explained as follows. Atomic classes:               Atomic means unique. Atomic classes use single variables to process the thread. The process includes the increment,decrement and compare the value. It makes the thread to be safe in all times. Program: This program starts with including the java package java.util.concurrent.atomic.AtomicInteger. Two classes are created. ...

Process Synchronization

  Process synchronization is one of the techniques which is used in shared resources . When multiple processes are executed, this is used to coordinate the resources. Here, we have taken the producer, consumer problem. Producer produces the item. Consumer uses the item. Java Program: ·        Include the semaphore package. ·        Create a buffer class for shared resource. ·        Create a semaphore for empty, full and mutex. ·        To produce the item,use the steps as follows. ·        If empty acquires it wait for not empty buffer. ·        If mutex acquires means it locks the critical section. ·        Assign the value and display it. ·        Finally, release the semaphores. ·        To consume the ite...

Synchronization in java

            Synchronization is a method which makes the one thread can access the shared resource at a time. The shared resource can be a variable, object or methods. The shared data can be used effectively. Features: The features are given below. ·        Data integrity. ·        Thread safety is achieved. ·        Race condition is effectively handled. ·        It prevents data inconsistency . Let us create a synchronized block in java . Program: To create synchronization in java This program creates a class for thread which is “ Th_Sample ”. It has two methods “incre()” and “getIt()”. “incre()” method is used to increment the value. “getIt()” method gets the input value. Next, a main class is created as “ S_Sample ”. The object for Th_sample is created. Two thread objects are created. Each one has separate for loop to ...