Berre vising
0:03
S… Speaker 1 (4- Pausing a Thread)
The third class in Java has a sleep method which we can use for pausing the execution
0:07
S… Speaker 1 (4- Pausing a Thread)
of a thread.
0:08
S… Speaker 1 (4- Pausing a Thread)
With this we can simulate a long -running operation,
0:11
S… Speaker 1 (4- Pausing a Thread)
like downloading a file that takes a few seconds.
0:13
S… Speaker 1 (4- Pausing a Thread)
So in our task class,
0:15
S… Speaker 1 (4- Pausing a Thread)
after printing this message,
0:17
S… Speaker 1 (4- Pausing a Thread)
let's call thread dot sleep.
0:20
S… Speaker 1 (4- Pausing a Thread)
Here we can pass the delay in milliseconds,
0:22
S… Speaker 1 (4- Pausing a Thread)
let's say 5000.
0:25
S… Speaker 1 (4- Pausing a Thread)
So this will suspend the current thread for roughly 5000 milliseconds or
0:29
S… Speaker 1 (4- Pausing a Thread)
five seconds.
0:30
S… Speaker 1 (4- Pausing a Thread)
It's not going to be exactly five seconds because this is dependent on the underlying
0:34
S… Speaker 1 (4- Pausing a Thread)
operating system.
0:35
S… Speaker 1 (4- Pausing a Thread)
But we can assume that this thread will be sleeping for roughly five seconds and
0:39
S… Speaker 1 (4- Pausing a Thread)
during this time other threads can get processor time.
0:43
S… Speaker 1 (4- Pausing a Thread)
Now, the Java compiler is complaining that we have an unhandled exception here.
0:47
S… Speaker 1 (4- Pausing a Thread)
That is interrupted exception.
0:49
S… Speaker 1 (4- Pausing a Thread)
This is an exception that gets thrown if you try to interrupt the thread that is
0:53
S… Speaker 1 (4- Pausing a Thread)
sleeping. We'll talk about interrupting a thread later.
0:55
S… Speaker 1 (4- Pausing a Thread)
But for now,
0:56
S… Speaker 1 (4- Pausing a Thread)
let's handle this exception.
0:57
S… Speaker 1 (4- Pausing a Thread)
So we put the caret on this sleep method,
0:59
S… Speaker 1 (4- Pausing a Thread)
press alt and enter and wrap this inside a try
1:03
S… Speaker 1 (4- Pausing a Thread)
catch block.
1:04
S… Speaker 2 (4- Pausing a Thread)
Pretty easy.
1:06
S… Speaker 2 (4- Pausing a Thread)
Now,
1:07
S… Speaker 1 (4- Pausing a Thread)
after five seconds of delay,
1:09
S… Speaker 1 (4- Pausing a Thread)
let's print another message like download complete.
1:14
S… Speaker 1 (4- Pausing a Thread)
We can also add the thread name for clarity,
1:16
S… Speaker 1 (4- Pausing a Thread)
so plus thread .currentThread .getName.
1:21
S… Speaker 2 (4- Pausing a Thread)
Now,
1:22
S… Speaker 1 (4- Pausing a Thread)
back to our demo class,
1:23
S… Speaker 1 (4- Pausing a Thread)
here we are starting 10 threads for downloading 10 files.
1:27
S… Speaker 1 (4- Pausing a Thread)
When I run this program,
1:28
S… Speaker 1 (4- Pausing a Thread)
you will see that these 10 threads will start at the same time,
1:31
S… Speaker 1 (4- Pausing a Thread)
and after 5 seconds,
1:33
S… Speaker 1 (4- Pausing a Thread)
they all complete.
1:34
S… Speaker 2 (4- Pausing a Thread)
Take a look.
1:35
S… Speaker 2 (4- Pausing a Thread)
So,
1:35
S… Speaker 1 (4- Pausing a Thread)
run,
1:36
S… Speaker 1 (4- Pausing a Thread)
they all started,
1:38
S… Speaker 1 (4- Pausing a Thread)
now 1,
1:39
S… Speaker 1 (4- Pausing a Thread)
2,
1:40
S… Speaker 1 (4- Pausing a Thread)
3,
1:40
S… Speaker 1 (4- Pausing a Thread)
4,
1:41
S… Speaker 1 (4- Pausing a Thread)
and 5,
1:42
S… Speaker 1 (4- Pausing a Thread)
they all complete.
1:44
S… Speaker 1 (4- Pausing a Thread)
Now if you had a single threaded application,
1:46
S… Speaker 1 (4- Pausing a Thread)
downloading these 10 files would take 50 seconds instead of 5 seconds,
1:50
S… Speaker 1 (4- Pausing a Thread)
because each download would start once another download finished.
1:54
S… Speaker 1 (4- Pausing a Thread)
So multi -threading makes our applications faster and more responsive.
1:58
S… Speaker 2 (4- Pausing a Thread)
Now,
1:59
S… Speaker 1 (4- Pausing a Thread)
what if we try to download,
2:00
S… Speaker 1 (4- Pausing a Thread)
let's say,
2:01
S… Speaker 1 (4- Pausing a Thread)
100 or 1000 files concurrently?
2:04
S… Speaker 1 (4- Pausing a Thread)
My machine has 4 cores and 8 threads available.
2:07
S… Speaker 1 (4- Pausing a Thread)
So how is that going to work?
2:08
S… Speaker 2 (4- Pausing a Thread)
Well,
2:09
S… Speaker 1 (4- Pausing a Thread)
the Java virtual machine has what we call a thread scheduler.
2:13
S… Speaker 1 (4- Pausing a Thread)
The job of this scheduler is to decide what threads to run for how long.
2:17
S… Speaker 1 (4- Pausing a Thread)
So if you have more tasks than the available threads,
2:20
S… Speaker 1 (4- Pausing a Thread)
the scheduler switches between these tasks,
2:22
S… Speaker 1 (4- Pausing a Thread)
giving each a slice of the CPU time.
2:25
S… Speaker 1 (4- Pausing a Thread)
This happens so fast that gives us the illusion that these tasks are being
2:29
S… Speaker 1 (4- Pausing a Thread)
executed in parallel,
2:30
S… Speaker 1 (4- Pausing a Thread)
but that's parallelism at software level.

Denne transkripsjonen er laga av AI (automatisk talegjenkjenning). Kan innehalda feil – sjekk mot originallyden for kritisk bruk. AI-regel

Elskar du STT.ai? Fortel det til vennene dine!
Samandrag
Trykk på Oppsummer for å laga eit AI- samandrag av denne transkripsjonen.
Samanfattar & # 160; …
Spør AI om denne transkripsjonen
Spør noko om denne transkripsjonen — AI vil finne relevante delar og svare.