#87 Thread Priority and Sleep in Java

  Рет қаралды 74,847

Telusko

Telusko

Жыл бұрын

Check out our courses:
Spring and Microservices Weekend Live Batch : bit.ly/spring-live-weekend
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Udemy Courses:
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
Java For Programmers:- bit.ly/javaProgrammers
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
In this lecture we will learn:
- Thread priority in Java
- How we can suggest a priority for a thread?
- Sleep() method in thread
- Waiting state in a thread
#1
- We cannot control the schedular, we can only give suggestions to it to give priority.
- getPriority() is a method that gives the current priority of a thread.
- The range of priority goes from 1 to 10. One is the least priority whereas ten is the maximum priority.
- 5 is the default priority or normal priority. By default, every thread has a normal priority which is 5.
- We can also change the priority by using the setPriority().
- Different schedulers have different algorithms to work upon so by giving priority, we can only give suggestions to it.
- It might be possible that the scheduler gives the highest priority to the process that will execute in less time at the running phase.
#2
- We can also make a thread to wait for some time and then execute the statement further.
- Thread will wait by using the sleep() method.
- In the sleep() method, we have the pass value for how much time we want a thread to wait. The time will be in milliseconds.
- Sleep() method will throw an interrupted exception. So, we can handle an exception by using the try-catch block.
- When we use sleep(), then the thread goes into the waiting state.
- As a programmer, we can not control a thread, we can only optimise it.
Github repo : github.com/navinreddy20/Javac...
Java:- bit.ly/JavaUdemyTelusko
Spring:- bit.ly/SpringUdemyTelusko
More Learning :
Java :- bit.ly/3x6rr0N
Python :- bit.ly/3GRc7JX
Django :- bit.ly/3MmoJK6
JavaScript :- bit.ly/3tiAlHo
Node JS :- bit.ly/3GT4liq
Rest Api :-bit.ly/3MjhZwt
Servlet :- bit.ly/3Q7eA7k
Spring Framework :- bit.ly/3xi7buh
Design Patterns in Java :- bit.ly/3MocXiq
Docker :- bit.ly/3xjWzLA
Blockchain Tutorial :- bit.ly/3NSbOkc
Corda Tutorial:- bit.ly/3thbUKa
Hyperledger Fabric :- bit.ly/38RZCRB
NoSQL Tutorial :- bit.ly/3aJpRuc
Mysql Tutorial :- bit.ly/3thpr4L
Data Structures using Java :- bit.ly/3MuJa7S
Git Tutorial :- bit.ly/3NXyCPu
Donation:
PayPal Id : navinreddy20
www.telusko.com

Пікірлер: 18
@Charitsis
@Charitsis 6 ай бұрын
Obviously, the goal of this short tutorial is to teach the concept of thread priority and show how Thread.sleep() works. Navin did a great job explaining those. However, if your goal is to enforce execution order between threads, you can use a binary semaphore like the code below and do not rely on elements of randomness like sleep or thread-scheduling algorithms: ------------------------------------------------------------ import java.util.concurrent.Semaphore; public class Demo { Semaphore goA = new Semaphore(1); Semaphore goB = new Semaphore(0); public static void main(String[] args) { Demo demo = new Demo(); A obj1 = demo.new A(); B obj2 = demo.new B(); obj1.start(); obj2.start(); } class A extends Thread { public void run() { for (int i = 1; i
@The_MinecraftDude
@The_MinecraftDude 3 ай бұрын
Oo😮
@saqibullah7286
@saqibullah7286 Жыл бұрын
very nice explanation sir keep it up
@maleeshasandakalum6213
@maleeshasandakalum6213 Жыл бұрын
Thank you sir❤❤
@SidharthDev-nw2vc
@SidharthDev-nw2vc Жыл бұрын
Brilliant.
@user-sg4yd5in7p
@user-sg4yd5in7p 10 ай бұрын
We appreciate👍
@shashankdeep403
@shashankdeep403 Ай бұрын
We can use serializable for making it sequence?
@justincarter360
@justincarter360 9 ай бұрын
awesome! its hard to split de HI. and HELLO.. i couldnt but it cool!!
@user-dv7nx6yn2k
@user-dv7nx6yn2k 8 ай бұрын
by ur saying at 6:50 .Sir, i've kept a time gap of 10 milli secs but still no use, hello was printed twice , many times
@Spam-xi2nv
@Spam-xi2nv 8 ай бұрын
Depends on how fast your computer is. It would have finished the process pretty fast. Try increasing the sleep time, just play with it .
@TheGloriousOne0
@TheGloriousOne0 Ай бұрын
@ehteerk
@ehteerk 11 ай бұрын
Wait & Try Catch
@sakshikhaire396
@sakshikhaire396 10 ай бұрын
Missing Hello Aliens!!
@Aashutosh_Bajaj
@Aashutosh_Bajaj Жыл бұрын
Will this work ??? class A extends Thread{ // after extending it with thread then now it is a thread // start is a method that belongs to thread class public void run(){ // every thread will have a run method int count1 = 0; for(int i = 0; iHIGHEST PRIORITY // System.out.println("Priority (before) is: " + obj1.getPriority()); // obj1.setPriority(Thread.MAX_PRIORITY); // System.out.println("Priority (after) is: " + obj1.getPriority()); } } // If you have 4 cores then you can execute 4 threads // If you have an octaCore processor then you can execute 8 threads // Number of cores are propotional to number of threads
@HeadSHOOTdewunzz
@HeadSHOOTdewunzz 8 ай бұрын
I have 6-core processor and tried 7 threads but it worked, any clue why?
@codewithmaheshhbalwan
@codewithmaheshhbalwan Жыл бұрын
public class MultipleThreads { public static void main(String[] args) { printHello o = new printHello(); printHi i = new printHi(); o.start(); try { Thread.sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } i.start(); } } class printHello extends Thread { public void printHello() { for (int i = 0; i < 10; i++) { System.out.println(i + " : " + "hello"); try { Thread.sleep(10); } catch (InterruptedException e) { throw new RuntimeException(e); } } } } class printHi extends Thread { public void printHi() { for (int i = 1; i < 10; i++) { System.out.println(i + " : " + "hi"); try { Thread.sleep(10); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }
@Zenetz-le4mq
@Zenetz-le4mq 4 ай бұрын
my machine is printing HIHI ns hello hello even if we apply sleep for both the Threads.
#88 Runnable vs Thread in Java
8:27
Telusko
Рет қаралды 70 М.
路飞太过分了,自己游泳。#海贼王#路飞
00:28
路飞与唐舞桐
Рет қаралды 39 МЛН
Son ❤️ #shorts by Leisi Show
00:41
Leisi Show
Рет қаралды 8 МЛН
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 74 МЛН
File Handling in Java Complete Course
1:01:04
Kunal Kushwaha
Рет қаралды 79 М.
#86 Multiple Threads in Java
11:30
Telusko
Рет қаралды 112 М.
#83 User Input using BufferedReader and Scanner in Java
11:48
#90 Thread States in Java
3:31
Telusko
Рет қаралды 43 М.
#89 Race Condition in Java
12:30
Telusko
Рет қаралды 59 М.
#65 What is Interface in Java
8:03
Telusko
Рет қаралды 184 М.
#92 ArrayList in Java
11:54
Telusko
Рет қаралды 97 М.
#95 Comparator vs Comparable in Java
15:43
Telusko
Рет қаралды 164 М.
01. Internal Working of HashMap & Java-8 Enhancement
19:11
WebEncyclop Tutorials
Рет қаралды 108 М.
КРУТОЙ ТЕЛЕФОН
0:16
KINO KAIF
Рет қаралды 7 МЛН
low battery 🪫
0:10
dednahype
Рет қаралды 1,8 МЛН
Klavye İle Trafik Işığını Yönetmek #shorts
0:18
Osman Kabadayı
Рет қаралды 9 МЛН