13.7 Multithreading Synchronized Keyword

preview_player
Показать описание
In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system.
Multithreading in java is a process of executing multiple activities can proceed concurrently in the same program.
Thread is basically a lightweight sub-process, a smallest unit of processing.
In multithreading threads share a common memory area.
They don't allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.

Multiple threads can exist within the same process and share resources such as memory, while different processes do not share these resources.
Using two different task at the same time means multi-tasking. Thread is unit of a process.

Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

Udemy Courses:

For More Queries WhatsApp or Call on : +919008963671

Follow on Facebook:

Subscribe to our other channel:
Telusko Hindi :

Subscribe to the channel and learn Programming in easy way.

Рекомендации по теме
Комментарии
Автор

those trying to test this programs on your high end laptops, might see no matter u use a counter of 1000 or 5000 or even 10, 000 to increment, you might still get the correct total of incremented value without even using SYNCHRONIZE keyword to sync the increment process between 2 threads. It just means your thread scheduler has a lot of resources available to be very efficient. In such cases, just increase the counter loop by a million (1, 00, 000) and then you will see the need of using the SYNCHRONIZE keyword as you will see an incorrect total. Hope it helps.

kartzoftvasu
Автор

Notes: not synchronized method is not thread safe, which means multiple thread can access the same method at the same time. Making the thread synchronized means only one thread can use the method at a time.

avahome
Автор

Very clear and simple example, thank you!

FedoraTime
Автор

you cleared my doubt I did not skip any ad! Modern Guru Dakshina (and I learned If we don't skip first ad YT shows a second one)

uditswaroopa
Автор

champav basu... intha simple ga telchesav, thread safe synchronization yappati nundo doubt. simply superb. hyd lo unte kaludam we can have good biryani..

rajag
Автор

That is a gem of an explanation, thank you Sir!

mohammedhammad
Автор

your videos are very good. perfect point-to-point explanation such as mechanical students are also getting interested.

shreyash
Автор

Synchronized block is used to lock an object for any shared resource.
Scope of synchronized block is smaller than the method.
A Java synchronized block doesn't allow more than one JVM, to provide access control to a shared resource.

lifeofme
Автор

This *synchronized* is a very big performance hit":

*** mainThreadCall...
Count: Done in: 125 milliseconds
***
Count: Done in: 129 milliseconds
*** callingWithTwoThreads... (not synchronized method)
Count: 1013362697 Done in: 67 milliseconds
***
Count: Done in: 67621 milliseconds

Also on some conference I've seen an example where the "synchronized" did not help. I don't remember what was the case, but they had to do smething else. I think it was some I/O issue or with databases. Just a guess.

Two synchronized threads are 540 times slower than one thread in my example (2, 000, 000, 000 calls). But when there's less calls, it works fine (but still slower than in one thread).

damianwojakowski
Автор

t1.start();
t1.join();
t2.start();
t2.join();

will fetch the same result without using the synchronized key word.

BUT...WHAT YOU HAVE DONE FOR THE PURPOSE OF EXPLAINING ITS A PERFECT EXAMPLE.

rahulgopalakrishnan
Автор

Godlike explanation, CLEAR AND SIMPLE !

thlegioner
Автор

Awesome and very clear explanation and easy to understand

vengateshan
Автор

YOU are most talented navin.. best and simple way to understand that how u elaborate the concepts..

suchitrajadhav
Автор

Hi navin, I have one question in this concept of threading. My question is like let us have three threads t1, t2, t3. t1 shud print 1, t2 shud print 2, t3 shud print 3, again t1 shud print 4, t2 shud 5, t3 shud print 6 and so on like this .

class Hi extends Thread
{
public synchronized void run(){
for(int i=1;i<=30;i=i+3)
{
System.out.println("from t1 " +i);
try{Thread.sleep(500);}
catch(Exception e){
System.out.println(e);
}
}
}
}

class Hi2 extends Thread
{
public synchronized void run(){
for(int i=2;i<=30;i=i+3)
{
System.out.println( "from t2 " +i);
try{Thread.sleep(500);}
catch(Exception e){
System.out.println(e);
}
}
}
}

class Hi3 extends Thread
{
public synchronized void run(){
for(int i=3;i<=30;i=i+3)
{
System.out.println("from t3 " +i);
try{Thread.sleep(500);}
catch(Exception e){
System.out.println(e);
}
}
}
}

class Hello1
{
public static void main(String[] args)
{
Hi obj=new Hi();
obj.start();
Hi2 obj1= new Hi2();
obj1.start();
Hi3 obj2=new Hi3();
obj2.start();
}
}


output is from t1 1
from t2 2
from t3 3
from t1 4
from t2 5
from t3 6
from t1 7
from t2 8
from t3 9
from t1 10
from t2 11
from t3 12
from t1 13
from t2 14
from t3 15
from t1 16
from t2 17
from t3 18
from t3 21
from t2 20
from t1 19
from t2 23
from t3 24
from t1 22
from t2 26
from t3 27
from t1 25
from t2 29
from t3 30
from t1 28


If you observe above some t3 and t2 getting executed before t1 can you suggest me about this. Thanks in advance

bharathibavana
Автор

thank u so much sir . such a beautiful explanation

saisrisai
Автор

Great video, thank you! One of the best Java instructors

gfunkonthecheesetree
Автор

Bucky Roberts from India .
Amazing tutorials sir!

dharmikbatra
Автор

@Telusko
We need teachers like you in college : )

cookwithme
Автор

Excellent video, thanks so much, you made it easy and simple to understand :)

ferfykins
Автор

Excellent video, makes understanding very simple and easy!!

ankitgoyal