Posts

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 ...

Standard Template Library – Iterators

                You have a container with elements. How will you access it?? You need a hand to pick it up… Here, iterator is the thing which access the elements of a container. Features: It traverses the container and hides the internal structure. It supports find(),sort() and count() algorithms . It access the data as input,output. It can traverse in both forward and bidirectional. To access it, use container_type::iterator name. Let us create a c++ program to illustrate the iterators. #include <iostream> #include < vector > using namespace std; int main() {     vector<int> v1 = {100,120,330,440,550};     // let us traverse the vector using iterator     for (vector<int>::iterator i1 = v1.begin(); i1 != v1.end(); ++i1)         cout << *i1 << " ";     return 0; } Output: 100 12...

Standard Template Library- Containers

                 This is the built-in implementation of data structures named as containers. It is an object which holds the collection of other elements. Generally, it is implemented as template defined as class. Main advantage is flexibility. Types of containers can be classified as follows. 1.       Associative containers 2.       Unordered associative containers 3.       Container adapter 4.       Sequence containers The built-in functions connected with each container are explained below. 1.Associative containers:               From the name itself, we can know. Association means the data storage have some connectivity. It makes the access faster. So, the time is efficient here. It has the following containers. Map -    Here, the value and key...