Thread Safety with Phaser, StampedLock, and VarHandle

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
my first time was it it's my first time to Oracle code one obviously but haven't been here for a while last time was 2012 before that was 2010 so yeah it's a long way from from Crete so we get to look at time 3/8 safety we look at 3 different constructs from Java 7 8 & 9 well 9 10 11 12 13 and the first one is phaser and the way that I structure this I thought well I can make like a hundred slides and just flip through the slides but then you can't see me fall down right or maybe in my laptop fall down we've stuck it down with gaffer tape so hopefully it won't fall down but we'll see what happens maybe my my my my exercises won't work so the idea the phaser class is a class which has been around since Java 7 so it's not really very modern but not very many people now hard works it's a replacement for cyclic Barre and count on match it's it's similar but it's more flexible and the whole way that they do their interruptions is also a bit more sophisticated than in the past also you can you can change the number of parties over time so it's not fixed like you have a cyclic barrier and with countdown edge it's also one of very few classes that cooperates with the fork/join pool and it does this through something called a managed blocker and what I'm what I'm going to do is the the daemons me to do now I'm going to put on their they're gonna be on github there's like currently the current set is on day and I'll upload once I'm done the latest classes on that so let's do the demo of cyclic Barre versus phaser and we all are in teams we all have to worry about all this cool stuff we have to do and we're going to start by looking at the team now the team gets together regularly for scrum right we have to do scrum very important so the team is going to get together for scrum at a regular basis we're going to have here our scrum master and we're going to do it first of all with a cyclic bear now we can use Java leaven because we are modern and cool almost as cool as Python but not quite there yet so you can save our whistle equals new cyclic barriers that's going to be the first type of thing with full-sized 5 so the 5 people who are part of this team and we're gonna say for the team we can say executors dot new cash 2 3 4 so that's going to be the team of threads who going to be doing the work but you know you know how life is you have to work then you've got to meet together for the scrum and then you can work again and wait again for the scrum until until you're done and then we blow the bus and everything gets shut down you're welcome to take photographs but this will be on the github as well so we're going to do from naught to 5 and we're going to make a bunch of runner balls these are going to be the job the the workers we're curls these lambdas of course because we're cool and we're gonna say while true and we're going to work now the work comes from I need to extend that the team glorious to get all the cool little helper methods that I need so work is going to do some work and the work cake takes between half a second and three six so we all take different times to finish our work and then we ought to get together at the same time to do the meeting so this is a bit inefficient because some guys will be sitting there twiddling their thumbs for for one or two seconds whilst the rest are also trying to join for for the meeting so it's between half a second and three seconds and then the scrum Murs one second all together alright so we then meet so we're going to say whistle we whistle the whistle and say we're meeting now and then afters we're going to do the actual meeting so the sort of the code we're going to do for the worker and then this is the work of we're going to submit them to the team so submit to the to the the worker to the team and then after that we can this after the full loop we're going to say see for a short while that's ten ten seconds is enough and then we're going to say team dot shutdown no no shutdown no does it actually interrupts all the three it's currently busy so inside the the work inside the meet you're going to see this going to be an interrupted exception and that's going to throw it a cancellation exception so that's what we're going to do if you're interrupted we're just going to cancel with the cancellation exception it all right now the couple of exceptions we need to worry about here over here we need to worry about the interrupted exception and again we can just say throw a new cancellation exception interrupted if I could type this to go a lot faster and broken barrier exception is going to just print out a message we've got a special one for that saying broken barrier yeah and what we can also do we can we can put a try catcher on this whole thing so that we get cancelled we can actually say we've been cancelled so we fear the cancellation exception we're going to say message we're done let's make a lower case and the gate message will give the reason why we're done right so this is our little demo and let's run this and see whether it actually does what we would expect it to do and that's what should go pass the five people five different names each name each citizen for name and they're all working and then they finish a different times but once they're all together they do this scrum it takes a second and then they go off and work again and then they all get interrupted and we are happy everything is fine now sometimes things get slightly wrong with this mechanism so for example if I set off for ten seconds wait for two seconds then it's likely to not work as we want it to work and if you look here you see that they you only send over beer they are ready for scrum and then we say shut down now and Kabir gets a broken berry exception and so she she actually carries on hits on forum and and and a new analyst is interrupted so yep it is broken barriers and and things can go wrong with us no let's see how this the same code would look with the phaser so we take this cyclic barrier copy-paste this is a way you can write a lot of lines of code very quickly ctrl-c ctrl-v get bonus and this is negated with a phase instead of a cyclic barrier I take the phaser and this code here with the the wait am I going to replace by saying simply whistle dot arrive and await advance so this way it's for everybody to arrive at the same time and then we're gonna do the meeting together do the meeting together so this is great and and we're going to again to do it for ten seconds like we did before and let's run this so runs for a while and just looking good alright so so far so good so they're all meeting together and scrubbing together and working together and got a slight problem not a problem here is that some of the the people Dimitri RB and Mark were finished before the rest and so they are ready for scrum so they are the waiting there and the other two are not not ready it so the the phaser if you use this particular method if you interrupt the thread it says a cow saw the interruption I will deal with it once I'm done here but it's basically uninterruptible now there are interruptible versions as well but this is the one you'd normally use so now we say well what we're gonna do about it well what we can do is whenever one worker gets cancelled we can say whistle dot arrive and D register which decreases the number of parties of the phaser so if we have the same situation happening and run this again hopefully it's going to be similar that somebody dear edits before the rest then the others cancelled with their scrum and move on Roth subscribe for ten seconds well that's to happen hopefully it's going to work and the app doesn't okay so we have to bump it down to two so it's going to be definitely going to have that problems always do filled with random times that it's hey you don't always get the effect you desire but I can see that three of them Yanis Demeter are beer they were ready for scrum they're ready and but then they all got interrupted and actually the two of them were will also be registered from that so then they could still do they they scrum the three of them could do this from one to three and then they were dropped it and everything shut down alright so this the the actual code if you compare it is a bit simpler for the phaser than for the other one here's just a single method identify what interrupted exceptions kids managed eternally whereas with the other one it's more complicated a lot of complicated all right let's try something else there's a possibility that someone might get axed at some point so let's make this with Xing all right so that means after we've met somebody might get in an envelope that says really sorry but we don't need you anymore okay terribly sorry unfortunately we don't need your services so I'm gonna bump this up to a bit more 20 seconds and you'll see what happens that so what happens is when you run this psychic barrier version with with with some people getting axed and you'll see okay I'd be our sorry you you've been fired now the rest of the guys they say well we got a meet so let's get together so mark you only steve and de metrio they were saying where's Robbie oh we can't start and who the base here so they're waiting now and they're waiting and waiting and waiting and she doesn't come until of course eventually the 20 seconds up and we kill everybody but they wait they'll wait indefinitely until eventually well nothing happens now you could if you wanted to add a timeout you could say I want to wait maximum of 5 seconds over here person doesn't show up we're going to just carry on now that's all fine and well about it doesn't work as you'll see now so the time art will print out two message timed out saying to time that but it completely destroys the whole system as you'll see so now we're doing it again everything's fine now she's really and lucky it's like she didn't want to come to drive one that's why that's why she give you extra and now you can see that they're timed out and now as everything's upside-down the people are just meeting by themselves and there's no more scrum okay it's a complete mess right and so this doesn't work properly you can't change number of parties as part of that let's try the same thing with the phaser then you'll see it's much easier to 2x people when you've got a phaser so you get a phaser or don't if you don't want to get axed so after the after the meet we say X maybe right and we run the code now it's not with a different mechanism and you'll see let's see this around a bit longer so it actually gives a chance for a beer to get killed again it's random so it shouldn't be her again but it could of course it's rather and no one's being fired yeah you are nice to spin ax good now I can see you always been axe and other guys say we don't care we just care with our meetings right that just cares only markets axed and they in the next the race three there does care up so it's quite quite easy and Dmitry is the last one so he he and he's actually talking in other room sure they've gone there haha but you see it just it just carries on without without the people that we next so the phase allows it to introduce and deregister people are dynamically alright um any questions about phase before we go on there's one more thing which I forgot to show but I think that's enough let's look at something Oh Who am I I'm Heinz I'm one of the jaikrit and founders I write the dogfish Deniz later I'm also Oracle groundbreaker ambassador it's like really long and involved it used to be develop a champion in our Oracle drama can developers and ambassadors and and draw a champion it's funny because last week and this is a true story I know all bad stories start with this is a true story but I was speaking to my wife I had a friend and I said you know what I'm thinking of like starting a of making my house like a general consul to South Africa's because I've got a South African passport as well and it's really cool because then the Greeks can't come into my house like the it's an official residence of a you know my great-grandfather did this and so they forget I said yeah then I'll be like an ambassador but I'm not sure how many millions I've got to give them for that but so I was talking about this and then I become a groundbreaking ambassadors there we go I don't know what that all means let's go to the next one stamp lock right so this is a Java 8 construct which was added to give you a beta function better ability for for communication between between threads better than readwrite locks and better than synchronized right but it's a little bit tricky because what what did what these guys do grow an Oracle and so on is that then they make the synchronize always better better better better so every version that synchronize gets a little better so you've got it is called constructs I'm gonna use reentrant lock and I'm going to use stamped lock and in the meantime they're optimizing synchronized so it keeps on catching up a little bit so what I'm gonna do is make it run some some benchmarks on this on this this machine and you bet you'll see that it's the quite a bit of cost to do with using synchronized so what is this this is a synchronizer since Java 8 it allows you to do is thread safe optimistic reads you can also do pessimistic reads and I'll define the reference in a moment you can of course to do pessimistic writes it's not reentrant some algorithms become more tricky this way it's used to enforce invariance if you don't have invariance across your fields there's no point using this just use Atomics just use volatile you don't need this if you know if there are no invariance but if you have invariance this is quite a nice one to have and something else which you can do with this thing is you can split the locking and unlocking between threads so one guy that I met he was using this for for an effort for some asynchronous framework where he had some event he he wanted to lock something it wasn't a thread it was it wasn't a memory section but he wanted some locking and he would lock with one thread and in a two an unlock with the second thread because to lock and unlock all you need is a step right so the stem clock has got a method right lock which returns the number and then you unlock it again with the same number so it's like an ID number you get a special pass and they give that back to unlock it and but it doesn't work reentrant li this hard works this is a pessimistic exclusive lock that's the right we also have a pessimistic non exclusive lock that's a read so it's just like the right but just with read and there's unlock read also an unlock which can take either read alright um then you've got um optimistic things like try optimistic read now trapped mystic read will give back a number that is nonzero if no one currently has a right lock so if some one other thread currently has a write lock the interrupt miss agreed is going to give back 0 but otherwise it get back a number and then validate you pass in that number that you got if that number was zero it comes back as false automatically there's no further discussion otherwise if someone if no one else has in the meantime asked for right lock you get back true so very ret says there's no way that anybody could have corrupted this data in the mean time now of course if you get back false it doesn't mean it was corrupted it just means that there is a chance it was corrupted right so this is a idiom for how to do optimistic read and it's quite involved with government one ladder the time you first of all get the number you then read the state into your into local variables and with this in me to be a little bit careful we don't want to accidentally cause exceptions all right so be careful of exceptions we don't wanna get exceptions so and we will I'll show you what I mean by this in one of the X one of the demos after we read the values in two local variables with invalidate that no one has in the meantime written and if no one has if someone has maybe written in the meantime then we use a pessimistic read lock so we get the read lock we try read the the local the the values back into local variables again and then we unlock read and once we've done that we can then use the that local variables to to to do one of our calculation is to do not so it's really important that we have a shortest possible time from a minute trapped miss agreed until we divided it the shorter the better the shorter the better because the phase if even like checking is stamped zero or not you wasting CPU cycles and and and you increase your chance of losing the heavy somebody else tried to write in the meter so the the longer the current the short of the current path the better then that's really important all right so I'm one example of where is a position now I live on the island of Crete and a few years ago we had a crocodile a live crocodile I feel from before that's like yeah we don't care about that but on Crete this was quite spectacular and they're trying to catch it but there were so many little sheep and chickens and children walking around that he never got hungry so they couldn't couldn't bait him and catch him and he eventually died because it actually it's quite cold and Crete if you can believe that on an island it snows um sometimes I mean it's always snows on the mountains but it does get quite cold so we've got a position were to move from one place to another from north nor two nor coma five and we want to go in a diagonal slide avoiding the crocodile right and but if we in see intermediate spaces that's bad we don't want to see that want to make sure that if we go from one place to another we go in the direct line we don't want to go accidentally and end up as crocodile food so let's have a look at how that could be done now we're going to take first of all the position position over here and it's this close close so this is original that's a two method synchronized void move by and another one is the calculation distance from origin is just hypotenuse of X and one so it's very simple very basic code now and my test here what it does is it's got one thread which is doing the move and one more thread doing the distance now of course you're going to have different results when you have when you run with more cause and also if you're running with with yeah with with nothing else I've got also keynote and things on this machine now don't worry too much about the actual numbers the thing which is interesting here is the very last number of each line this is a system time over elapsed time elapsed time as your wall clock time how long it actually took and you can see that there is a fairly large amount of system time being burnt up managing the unlocking and locking and unlocking and locking and locking and unlocking and and this is actually the base the base was the value here so it's about 25 percent between 17 and 25 percent you're wasting and system time with it with a synchronized and this is the sub performance here so what we'll do is we'll just so we don't forget what that was we will copy this here then I'll show you how to how to code this this with with the stem clock but the performance might not be better so I'm just warning of that and all right so the move by we're going to do this we're gonna say long stemp equals SL dot read lock of course that a write lock arose after school define the ISO so the SL is going to be a field stamped lock equals new stem plonkers make final as well um and then we're gonna wrap a try finally around that the try finally is going to say SL that unlock a right stamp now you could say unlock but the unlock right is a double check just to make sure that it actually is was it was the right step so this is this is quite easy now the second one is a bit more tricky because we first want to read these values into local variables so we're going to say long stamp equals SL dot try optimistic read we then say because we can't save our if you want current x equals x comma current y equals like any two that they caught you need to save are again yeah I wasn't sure about that let's get rid of that not happy right don't know about that and then we can say if not SL dot validate of the stamp has been somebody maths maths or else might have written then we can say over here current x equals what we need to not do pessimistic read stamps we're gonna say stamp equals SL dot read lock and current x equals x current y equals y and of course we won rattler to try finally and then in that one we're gonna say it's all that unlock read of the step and then here for that Potter knees we're going to say current X comma current what so this is a very very basic very simple very simple plus and I think that all of us will will get this one right if we try to use it and I'm not sure the performance will be that much greater on this but we will see the system time going down so that's the thing we're looking at here at this moment you can see the system sound down to almost zero there the last the last number here almost down to zero and we might be a better performance but it's really dependent on the test it depends how much we move how much we do that whilst we're doing positioning and so on so the the thing we're looking at here is now at the moment just the system time that's important pot so all right so this is one if one example how you could use stamp look now I've got another example what I want which want to show you um which is a little bit more complicated and it took about five guys to get this right might have been more we do were a part of that I've McCourt remember there was a lot of people to get this right and so the it started with with an email that my friend Jack shirazi sent me way he tried to have just a just a thought experiment let's make an int list right list of hints ArrayList of hints which um which which uses one one lakh for writing and one lakh for reading two different synchronized and and his code had race condition side curse didn't he's coded and actually work and then I said well the sexy because he wanted read and write is it's actually a good place to try and use read write lock so this is now using synchronize at the moment alright so if you if you run this you'll see that there's again a lot of system time every elapsed time and I'll just show you one one or two steps see it's it's um and I've got one thread calling size one thread calling gate another thread calling add and remove because I don't want to have an infinite size of elements so you can see this it's a feeling fading out if you look at the numbers gate is about 13 12 13 million times size is about 14 so they're all about the same it's all cost about the same so if you stop this run actually got to go out the number so let's write these done whilst we're here now let's try and and redo this with with the lock so we could use for example a private final read/write lock which which is it's a its failure for the philly old class being around for a while and it does have some issues that's just warning it you probably shouldn't be using this but let's just use it to try it on and let me just Rianne cart or the synchronized and then once I've done that it'll be quite easy to put in the read/write because I've got all these IntelliJ shortcuts suited for me okay so it's all art now and now we can simply say read lock see how quickly this get there's a read so we make a read lock and as a right we make a write lock trim to size doesn't get called often but it's there so read/write lock remove there's a write lock so it's we all done for the reentrant read/write lock and it's it's basically like a synchronized but I've got I've got two different things and the reason I need this is because I've got a size and a force got an array but these are two separate fields because I've got two separate fields I need to I need some way of locking that something so let's run the code again and see what happens when we do so the readwrite lock the now's the read to happen whilst the writes happening but there was a problem with another Fitz barging I think barging is the right word for it I don't know if you notice if you walk to to Oracle due to the two code one in the mornings there's one road without a traffic light and there's poor suckers coming with arms with cars and they just have to wait for like the whole day to get across the road because it's nothing saying now you can drive and like before one person's across the road the next person comes across the road right this is a problem so he's like why don't take this road and and because it's there's no there's no traffic light to stop from walking we keep on we like the readers and he's like the writer we keep on taking turns and so you actually find that the readers here are being or the writers almost being preferred getting preference you'll see that here the writers like he's going he's great because it's fantastic but the readers are terrible look at it it's really bad with the read write look now um this is copy and paste there's the system time it shouldn't be very different actually it's not about the same doesn't they're not much different with the system time um so let's let's let's do this with the with a stamped look and what we'll do is we're at the same time also doing it will also go straight to using the optimistic approaches so for size we just want to make sure we see a correct size as you know as most recent as possible so to do that you can actually say that were this must be commissary name is to it so I souls are trapped a mystic read that does a volatile read and then we can actually just say return size we're going to see the correct value right the very recent value gate is more complicated I will get back to gate in a second whoo that's is actually quite tricky it inside here correct inside here yes exactly exactly exactly right so I'm I'm so this will work I can also put a try try and vetted at off hoods but I don't really care I mean I think if somebody else writes in the meantime I mean the size you get back from concurrent structures anywhere not very reliable I know I've got something which at some point was correct which is better than concurrent link you because you get something back would close maybe never correct so it's so we better than that so gate will get back to in a moment the add I'm going to say lamp long stamp equals a so dot right look and then we're gonna say again it's all an unlock right on the step so this is again very similar to to the readwrite lock same thing with the trim to size we're going to be going to start with this and I'm gonna optimize that just to show you but it's it's not going to pick the performance because I didn't call trim to size very frequently the other ones are called often all right then the remove again we can do the same as we had before and as a first step just do it this way okay so this is our initial initial values and let's do the add as well the gate because the gate is actually quite quite tricky we want to do it optimistically and we want to give it a few tries to get the value optimistically because um if you have some 3/8 writing and you want to get it if you fail the first time it's sometimes worthwhile to try three or four times not too often but you know let's just try and let's try up to five times so we're gonna have a full loop naught up to optimist stick retries which is going to be a constant of let's just make it five and you don't want to fail optimistically more than more than ten more more than 10% so it must be should be less than 10% times that you have to go to something else so so now we're going to say long stamp equals s Oh thought try optimistic read so we get the optimistic read value and now we want to read the values until our variables so gonna save our current size equal size of our current r equals r okay and now the thing is we want to be very careful because if we now call range check for example with these values or if we try and get the value for example if we say here VAR r equals current or index we can get an error in excel found exception so we don't to do that want to be very careful because this can cause an exception we don't get an exception so we need to check if index there's less than it will but but as I said I want to have a code path as short as possible bouncer yes okay so the range check will throw an exception but then it's like an index out of bounds exception not an Arabic sort of bounds exception and and and and I want to put on - to prevent even getting to that exception so now what you can do which is that you could cheat a little bit okay because if you cause an error in Excel of bounds exception it's actually free it doesn't cost anything so you could actually just try catch and then degrade to pessimistic read look but that's an optimization has been around since draw of 1.3 and the server VN but that is kind of cheating and people might look at and go what the heck are you doing you know what about the cost of the exception well it's zero doesn't cause anything because it's a special time yeah exactly exactly yeah it should never happen but this this really can happen it really can because if I call trim to size while somebody's calling gate it can happen so we need to be careful this is one case where you really want to rather prevent it so if index is less than R dot length then we can continue now we can say if SL dot validate of the stamp and now we do the range check so now we're sure that we're not going to get in normal areas of bounds exception but we can out of the range tricks of index comma the current size and then we can say return or oh no sorry we need to say after this we need to save or R equals current array index right that's what you need to do so you so basically once you've double-checked that sorry yes that the array me finish this once you double check that it fits within the index you then go and and read it until our it yes thank you very much awesome current are dot length thank you very much so once you validate to then you can return it and then what you can do is you can say if you don't return then we're just going to do it the normal way pessimistic read lock read lock and say it's all dot unlock read and lock read over here of the step alright so this is this is how you'd sort of the thought proceeded to go through to to get something like this let me run the code is just to see if there's any better the system time and some of them should be better because whilst some writing there's a smaller probability that somebody else is going to stop me with the the gate or the size well the size won't block any writers the gate might block a writer but the chance is much smaller look at the system times down to zero and I mean the size is like billions of times right it's really really fast and the gate is also pretty good but as I said they they do improve the synchronize with every Java version so the difference is no longer as stark as it as it could have been at some point in the past all right there's one more thing one can do but I'm not going to do it because I want to talk about var handle you can you can also upgrade a read lock too right look with with with stamp lock which you can't do with readwrite locks but it's not very useful so I'd rather go to to var handle so the Vandal added in Java 9 is they try to to sort of give us cold turkey or unsafe and you know there's so many libraries easier and serve some people won't use that service everybody's on set if it's so much cooler than anything else um and with with with for handle they you you able to to read and write existing fields using volatile or get acquire set release and there's a new one called get opaque and set opaque which and give up a consider papers useful if you want to do some logging for example and you don't care when the data that you're writing will become available all you care about is that you don't have splitting off of 64-bit values that's guaranteed and and that that you that the value that you see as at some point being valid so but you don't care when it happens so you can't use get a pair consider peg to do a cue for example into thread communication doesn't work if it works in your machine it still broke doesn't actually work they've also got a new one for comparing exchange volatile this allows you to do a real compare and swap we get back the value that was actually in memory rather than a true or false and get competing exchange volatile is can be faster if you don't have a lot of contention but if it off contention sometimes the performance can be worse than this to normal compared set counter-intuitively so let's have a look at how one would take this position and convert it to var handles rather than the stamped look so let's go over to that um the VAR handle class is here position back at synchronized and and what we're going to do is instead of having instead of using a stamp lock we're going to wrap the the values into an array so equals North comma okay and and then we're going to simply in fact you can be can be but better than that we can say X y equals x comma Y not I I really because I'm because I'm going to see this from multiple threads I actually want to make sure that this is that these are sorry double I want to make sure that this is always going to be the correct values so I'm gonna make it follow top now we all know that array elements are not thread safe right there also are mutable but no one's going to see these these arrays there's only internally so from outside in and see it so they move by becomes a simple atomic update right comp and sit and sort of the the way that we normally code this as we make a vow handle with the same name put at the bottom of the class typically so it's going to be private final static var handle XY it's got the same name as a field that you are using but just capital letters and then static XY equals method handles dot lookup dot find var handle with the class would be our position class the name is XY and that I don't know what's worth is H and then the the type is a double array dot class it's the type over here now of course this could throw a reflective operation exception and if it does we gain to this throw an error that's a typical thing what we'll do what one does over here so reflective operation exception and throw new error of e right and now that I've got the XY we can actually do like a loop and try and set it with campaign set so we're gonna say double array really struggling with typing today current common next and I'm going to initialize the next immediately with naught comma naught that's just an empty array and then I'm gonna say do the reason I use do instead of while it's because people are programmers just can't manage to understand do for some reason they like while I don't know why so do current equals XY so read the value into into current I then say next naught equals current naught plus Delta X and doctor why next one equals compass Delta Y then I say do while not XY dot compare and set and I am expecting it to be still equal to current so campaign set from on the current position current object I'm expecting it to solve ax equal to current and if it is I want to set it to next and if it's not I'm going to simply try again so for what multiple threads who are moving at the same time then they're all going to create the array try and set the values try and set it if they succeed great otherwise they just undo and retry it again second time so this is moved by the distance from becomes incredibly simple here you can simply say double current equals XY now it's important to read it in toluca variable first and then you can say because otherwise what can happen is that you you might end up with with with too hot with reading it in the middle so you have to put in two local variables so it could actually be be reassigned while Syrian whilst he doing the calculation but it won't affect you because you're pointing to the original array okay it's the current nought and current one and now if you if you run this this code you should again see that the system time over elapsed timers is really small and the the reading should be incredibly fast the the the gating of the values so the distance from origin it's like was it so slow 36 R&I 36 million times it always tricky to do these performance tests especially on a machine which is doing other stuff as well but I wanted to show you sort of the the the coding approach you would use or use for this and of course you can optimize that if you if you know for example that there's anyone through writing and several threads reading it optimize it in other ways as well as I said I had to there are some other things you can do but I think we want to have just a few minutes for questions we got another two minutes yes sir right okay so the question is what's the difference between Atomics and just using VAR handles and the EAJA of course of course force so therefore position you could also do it with atomic reference absolutely absolutely but then you've got another level of indirection you have to deal with Sonia for high performance code you want to have as little indirection as possible so so this this is the benefit you get from it because because all of the methods like find out pay or get Ipek all of that is also available in the in in Atomics as well on job since Java 9 so you don't get any extra functionality with VAR handles but you can get slightly better performance because you tell you what you know if you look at the way the memory is allocated and you know you'll have you know atomic being there and the object is here less directness in direction is always better yes exactly i Wasi yeah that's what it's mainly for to avoid that and safe so if you can it's basically to to do the same thing I'm used to do with unsafe but with four handles we another one that used to be used with atomic references atomic field references but but but but they suffer from an amnesia that was forget I actually have to do this to keep on checking so there were not as fast as unsafe there are none safe the vore handle gives you the same performance is unsafe so if you using unsafe try move over to four handles you should not lose any performance that's what I'm saying here there's not that much more to say yeah oh yes yes absolutely absolutely yeah thanks very much don't forget to vote as a leave [Applause]
Info
Channel: Oracle Developers
Views: 2,040
Rating: 5 out of 5
Keywords: oracle, oracle cloud, oracle developers, cloud, cloud computing, platform, infrastructure
Id: Xro4KwoMNJ8
Channel Id: undefined
Length: 47min 20sec (2840 seconds)
Published: Wed Oct 24 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.