#87 Thread Priority and Sleep in Java

Поділитися
Вставка
  • Опубліковано 19 гру 2024

КОМЕНТАРІ • 18

  • @Charitsis
    @Charitsis 10 місяців тому +20

    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

  • @Sanai9
    @Sanai9 Рік тому

    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 Рік тому +4

      Depends on how fast your computer is. It would have finished the process pretty fast. Try increasing the sleep time, just play with it .

  • @saqibullah7286
    @saqibullah7286 Рік тому +2

    very nice explanation sir keep it up

  • @shashankdeep403
    @shashankdeep403 5 місяців тому

    We can use serializable for making it sequence?

  • @maleeshasandakalum6213
    @maleeshasandakalum6213 Рік тому +1

    Thank you sir❤❤

  • @justincarter360
    @justincarter360 Рік тому

    awesome!
    its hard to split de HI. and HELLO.. i couldnt but it cool!!

  • @codewithmaheshhbalwan
    @codewithmaheshhbalwan Рік тому +3

    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);
    }
    }
    }
    }

  • @ehteerk
    @ehteerk Рік тому

    Wait & Try Catch

  • @Aashutosh_Bajaj
    @Aashutosh_Bajaj Рік тому +1

    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 Рік тому

      I have 6-core processor and tried 7 threads but it worked, any clue why?

  • @NakabuyeLea
    @NakabuyeLea Рік тому

    We appreciate👍

  • @SidharthDev-nw2vc
    @SidharthDev-nw2vc Рік тому

    Brilliant.

  • @sakshikhaire396
    @sakshikhaire396 Рік тому +4

    Missing Hello Aliens!!

  • @TheGloriousOne0
    @TheGloriousOne0 6 місяців тому

  • @Zenetz-le4mq
    @Zenetz-le4mq 9 місяців тому +1

    my machine is printing HIHI ns hello hello even if we apply sleep for both the Threads.