Java - Synchronized, Transient and Volatile Modifiers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the few other types of non access modifiers are synchronized transient and volatile modifiers let us understand in detail about each of them and how they are used mainly all these types of modifiers are used in the concept of threading threading is nothing but which allows you number of tasks to be performed together so when number of tasks are performed together what all resources are to be used or what all resources are to be free so let us understand in detail about each one of them in case of synchronized modifier as the word says synchronized what do you my synchronize synchronize is nothing but you're providing a law for an example you says that my home is there I'm trying to lock up that home and no other person apart from the family member can enter until unless I unlocked it right so here is the same case it says that a resources that has been called by a thread will be locked until unless it completes its purpose so in a case of programming we say that a method which is declared as synchronized how do you declare a method with synchronized with the help of a synchronized keyword so once you define a method as synchronized that method will be locked by one of your thread it means until unless that thread or task has not been completed it will not free up that particular method therefore this method cannot be called by any other thread until unless you free up your this particular method and this synchronized methods can be written with any type of the access modifiers you remember access modifiers are nothing but public private protected and no modifier so these synchronized keywords can be provided with all the four access levels of modifier and the syntax is public private and so on then you write the key word and this method returns nothing therefore it is void the method name normal brackets and in the you write the block of statement that you need to execute however normal methods and synchronized method it doesn't differ the way you declare the way you define the meaning everything remains the same only that this method cannot be called by any other method until the first method Freeza the next one is transient modifier what our transient modifies and with whom can be used these transient modifiers transient modifiers nothing but a transfer keyboard that you normally use to define the instance variable what our instance variable the variables that are declared outside the method but within the class our instance method and this instance variable can contain the transient keyword but what is the role of this transient keyboard the transient keyword will allow them to explode from serialization process so if you're defining any instance variable as transient they will be removed or excluded from serialization what do you understand by here serialization serialization is nothing but where you are running number of threads see what happens is that whenever you create a variable it's object state is stored in a string right that is nothing but serialization in case of serialization the state of your object will be stored in streams and when you want to return you deserialize them so now when you have created these variables and you do not want a particular variable to come under serialization or when you do not want a variable object state to be stored in a stream in those cases you use the transit keyword it says here that ass transient modifier indicates the JVM to stick a particular variable or object from serialization and once the purpose is finished it decent serialized those and one important thing is to remember a transient keyword cannot be used with static keyword because the static will face that the value is this and it cannot be changed and it becomes method and all right a static method is a static variable is nothing but your class variable whereas an instance variables are not your static variables and transient keyword can only be used by your instance variable the next type of non access modifier is your volatile what do you mean by volatile what happens in case of volatile let us understand it says that a wallet ID is related to the visibility of variable when number of threads are running concurrently it says that when number of tasks are running concurrently they will keep reading and writing the value of a variable which thread of which task has read the value which thread or task has written the value we need to store the value of this variable where will you store the value of this variable the value of the variable will be stored in your main memory and not in your CPU caches and the another important thing to remember about your wallet type is that it says that it cannot use the static keyword let us understand with the help of an example while we use wallah type in our real-time this is used mainly in case of threading concept so we have created here a class that extends you're threading concern and uses the threading methods you have a class called my runnable class that you have created you are trying to implement the runnable whatever are the methods available in runnable you're trying to call them you're trying to display them right so you have certain methods called run and stop before that you have created a variable of a boolean type called active which is said to call a time what will this wallet I do I shall explain you we have two methods created those are run and stop and these two methods are from runnable so in the first method run the active value that is the boolean variable value we are setting is true and if it is true please a block of statements it says that if the value is true and antonella's it is to keep executing and this loop will not stop then we have called another method stop and the value is false so we are providing a boolean value twice for the same variable once as true second is false in case of run once you provide true and the condition says that active it means it will not stop you will have to stop so well this method stopped this variable no it will not it will keep executing irrespective of each of run and stop method being called by different methods it will not stop run is called by a separate method stop is called by a separate method but it will not stop executing this block of statement until you make it as a wallet time therefore you need to make that variable as volatile so these three kind of modifiers can mainly use in the concept of threading to decide which task will hold which memory which kind of a variable are locked up or which resources are not accessible
Info
Channel: Tutorialspoint
Views: 47,190
Rating: undefined out of 5
Keywords: Java, Java Technology, Java Basics, Java - Synchronized, Transient and Volatile Modifiers
Id: MAfYOFs_DB4
Channel Id: undefined
Length: 7min 41sec (461 seconds)
Published: Thu Jan 18 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.