Advanced Java: Multi-threading Part 2 -- Basic Thread Synchronization

preview_player
Показать описание
-------------------------------------------------------------------------------------------------------------------------------------------
This video looks at basic thread communication in Java using boolean flags with the volatile keyword. I also take a look at why volatile isn't good enough for most situations.
--------------------------------------------------------------------------------------------------------------------------------------------
Рекомендации по теме
Комментарии
Автор

For those wondering: when a variable is declared volatile, the value of the variable is read and written directly from the memory.
Each thread caches the values of variables from the memory. Thus the thread does not have to always refer the memory, and it can simply read the values from the cache. This is gives rise to the problem of visibility.
This cache may not be consistent with what other variables see. With volatile, we skip the cache and read/write directly in the memory. Thus, changes are visible to all threads.

anamigator
Автор

I went though your videos when I had just 3 years of experience, and now I have 12 years of experience still going through your videos.

ndgprasad
Автор

I know :) I played around with different ways of speaking in my early videos, and now when I listen back to some of them, I really don't know how I ended up speaking like that. Sometimes I was really tired, but I sound like I've been woken up in the middle of a throat operation or something.

caveofprogramming
Автор

Dammit! Many times when I'm reviewing coding concepts I stumble across your videos. The moment you start speaking I realize that I've seen your videos before. You have earned my subscription!

Poser_Metal_Records
Автор

2019 - Still an awesome tutorial! Thanks

cuongv
Автор

If you have many of them, maybe check out the video on thread pools. Or use thread interrupts. You probably need something more advanced.

caveofprogramming
Автор

You could use one "running" if you want. Or use one per thread if you want to control them individually. It's just normal Java -- only the "volatile" is different.

caveofprogramming
Автор

Wonderfully simple explanation of this concept.. I'll be watching the rest of this series. Thanks!

paulhayden
Автор

Haha explaining volatile has to be one of the hardest things in Java :P You can't show it and you can't prove it

YDwelve
Автор

You are saying that volatile is a reminder to the thread not to be greedy and that other threads are also in need of the "shared data" and to be graceful enough to keep an eye on the changes affected on the said resource. I guess you achieved what you intended at 05:05 when the thread, proc1 ignores the main thread request to wind up and goes ahead and says "Hello". If that is what you meant, be it said - you have nailed it.

mistery
Автор

Nice explanation, i have a small doubt .. while you explaining you said there are 2 threads, but here as per my knowledge we have only one thread that is processor... can you please explain... advance thanks ..very helpful :)

ShravankumarAkirala
Автор

I think, if you uses running inside the run method, as a local variable then the threads will keep a copy of it . For instance variables threads does not keep a copy

arpanbanerjee
Автор

Cheers Amu, try copying the code from my website (in the YouTube videos section on the homepage). It works. Also you could try putting extra System.out.println statement in, to see what is actually getting executed. But I'm guessing you have mis-typed.

caveofprogramming
Автор

If I understood right, the main method of program acts like an another thread ? So we have 2 thread in this example, one that you created with Proccessor instance and other one is main method ?

rasimguler
Автор

I've been watching these series and they are awesome, I'm just a bit worried since they are 9 years old, is everything explained here still up to date?

julian.castro
Автор

So basically, if he didn't use volatile then some systems (OS) would not see the change in the "running" variable and stay in infinite loop? Is that correct?

christorok
Автор

i am shocked how good the explanation for volatile is. my professor was not able to make that clear.

IMakeMyDayI
Автор

yes; caching is the only potential problem.

caveofprogramming
Автор

One thread may cache some variables at the beginning and then never access the memory to see if the variables have changed. With "volatile", we can prevent a thread from caching its own copy of some variables. Then all threads will access the only one memory location.

tienanzhang
Автор

Really clear explanation that made it easy to understand, thanks!

BluePhoenixGames
welcome to shbcf.ru