Java8 | Multithreading | 🔥 Asked in Oracle | Print Even & Odd Numbers Using 2 Thread | JavaTechie

Поділитися
Вставка
  • Опубліковано 14 сер 2023
  • 🇮🇳🇮🇳 Happy Independence Day 🇮🇳🇮🇳
    #JavaTechie #Java8 #Multithreading #InterviewQ&A
    In this tutorial, you will learn how to print even and odd numbers in synchronize order using the two threads in java . We will discuss below 3 approach to solve this question
    👉 Solution using Java 7
    👉 Solution using Executor Service & ComplatableFuture
    👉 Solution using ComplatableFuture & Java 8 stream
    Spring boot microservice Premium course lunched with 70% off 🚀 🚀
    Hurry-up & Register today itself!
    COURSE LINK : javatechie5246.ongraphy.com/
    PROMO CODE : JAVATECHIE50
    OR use Javatechie APP
    Download the JavaTechie app on your iOS or Android device from the App Store or Google Play Store.
    Find the links below :
    Android 🌐 : play.google.com/store/apps/de...
    IOS 🌐 : apps.apple.com/in/app/javatec...
    GitHub:
    github.com/Java-Techie-jt/java8
    Blogs:
    / javatechie
    Facebook:
    / javatechie
    Join this channel to get access to perks:
    ua-cam.com/users/javatechiejoin
    guys if you like this video please do subscribe now and press the bell icon to not miss any update from Java Techie
    Disclaimer/Policy:
    --------------------------------
    Note : All uploaded content in this channel is mine and its not copied from any community ,
    you are free to use source code from above mentioned GitHub account
  • Наука та технологія

КОМЕНТАРІ • 79

  • @sayanbiswas8847
    @sayanbiswas8847 11 місяців тому +6

    I also had faced this question around two years back in one of my interviews. Thanks for covering this

  • @gopisambasivarao5282
    @gopisambasivarao5282 11 місяців тому +2

    Basant, awesome explanation, appreciate your efforts...God Bless you.

  • @kbpmowrya
    @kbpmowrya 11 місяців тому +2

    I also faced this question recently thanks bro

  • @KuchNahiBasAiseHi
    @KuchNahiBasAiseHi 11 місяців тому +1

    very nice session. Thanks.

  • @rajenderprasad1193
    @rajenderprasad1193 11 місяців тому +2

    Thank you Basant bhai

  • @gopishettymahindra2713
    @gopishettymahindra2713 11 місяців тому +1

    super explanation sir.

  • @mrmohit1112
    @mrmohit1112 11 місяців тому +1

    Great sir ❤

  • @neha6000
    @neha6000 11 місяців тому

    Thank you for this much good content

  • @mrmohit1112
    @mrmohit1112 11 місяців тому +2

    Sir,can you please create a full detailed video on 2-way email communication in microservices using rest apis and POP3 configurations

  • @GuruNanakTV
    @GuruNanakTV 9 місяців тому

    Very complex solutions, it can be done very easily without code duplication

  • @yoshitamahajan353
    @yoshitamahajan353 Місяць тому

    Thank u even i faced this qt

  • @ravi.m4954
    @ravi.m4954 8 місяців тому

    @JavaTechie thanks for knowledge sharing. If possible can you make one video on below or share me if you have any reference links
    1)How can we estimate min and max number of threads to create.
    2)on which parameters number of threads creation depends
    3)Threads vs Core CPU

  • @vidhanchandra3997
    @vidhanchandra3997 11 місяців тому +1

    Eagerly waiting for your next microservices design pattern....😊😊😊😊

  • @kumarmahendra2611
    @kumarmahendra2611 11 місяців тому +2

    Hi Javateachie ,
    I have been following your videos from long it helped me alot in my professional journey. Thanks a lot 🙌🏻
    One request from my side , can you please created few videos on EJB and how to implement on higher level ?

    • @Javatechie
      @Javatechie  11 місяців тому

      Thank you buddy but so far my knowledge ejb is not used in the market and i believe very rarely it's being used in some legacy projects

    • @kumarmahendra2611
      @kumarmahendra2611 11 місяців тому +1

      @@Javatechie yes but i have one project to work on in which ejb is used and i am finding difficulty to understand the use of it . If you can just make small demo video understand the concept and pros/cons of it then it will be big help 🙌🏻.
      Thank you for replying 😊

  • @singhkk138
    @singhkk138 3 місяці тому

    Very nice explanation, can you please make a video to print number 1,2,3 from thread 1, thread 2 and thread 3 then number 4,5,6 from thread 1, t2, t3 and so on

  • @Manoj-zx6ye
    @Manoj-zx6ye 11 місяців тому +1

    HI,could you make video on spring boot jwt with redies database .

  • @yoshitamahajan353
    @yoshitamahajan353 Місяць тому +1

    Plz make video for aws interviews

  • @Anushaaa_
    @Anushaaa_ 11 місяців тому +2

    👍

  • @sameergurumukhi
    @sameergurumukhi 8 місяців тому

    In the last approach, how is the excecution sequence of odd followed by even is guranteed ?

  • @amanabhishek8998
    @amanabhishek8998 10 місяців тому +2

    could you please do one video where all the multithread program questions are there, in addition to that in the interview some people asked about lots of files and how you read and process( make some change) and write in document as a output using multiple threads/Executor framework

    • @Javatechie
      @Javatechie  10 місяців тому

      Please check my spring batch video i have explained this usecase

    • @amanabhishek8998
      @amanabhishek8998 10 місяців тому

      Thanks for your immediate response, I have seen and accordingly I have given the answer about the first part but for making the changes after reading and then write in new file on those side I am not giving satisfactory answer to them 😢

  • @satyabratasahoo967
    @satyabratasahoo967 4 місяці тому +1

    Here we are checking the thread name in if condition, which violates the multithreading concept. If the odd thread is also trying to access the object still it will be thrown out because of failing the if condition. Also if we are using this condition then the code will work without synchronized block and lock.wait() and notify(). But if we remove the thread name check in the if condition, it will not work in the desired way which means odd thread is able to access the even condition also. Please suggest further

    • @abhinavpandey3356
      @abhinavpandey3356 6 днів тому

      Exactly bro if we are using threadname then there is no need for wait and notify

  • @sagararora5519
    @sagararora5519 11 місяців тому

    Hi can u make tutorial on websocket implementation on spring boot microservice aritecture on api gateway example usi

  • @revanthchinnu8904
    @revanthchinnu8904 9 місяців тому +1

    Hi basant , can you please make videos of multi threading like thread life cycle , volatile keyword, wait and sleep , synchronisation

  • @kiranvish1
    @kiranvish1 4 місяці тому

    Could u please explain what's the importance of Thread.sleep call in Main method.

  • @MYSTERIOMAXXX
    @MYSTERIOMAXXX 10 місяців тому +1

    @java techie Hi bro your last methods is incorrect...
    Rerun it few times and worker-2 will get executed first..as operation is async..

  • @user-ni4ln8ps2n
    @user-ni4ln8ps2n 11 місяців тому +1

    Hi can you make tutorial about JCR (Java content repository), it is hard to find tutorial online about that topic.

    • @Javatechie
      @Javatechie  11 місяців тому +1

      I am not aware of this concept. No worries i will check this out and update you

  • @user-ss6jx4nh9t
    @user-ss6jx4nh9t 11 місяців тому

    in case of completable future example how it make sure that odd thread only executes first in second and third example?

  • @reenarodrigues8976
    @reenarodrigues8976 8 місяців тому +1

    Hi sir can u create seperate series on multithreading playlist for java

  • @MuraliM.A.
    @MuraliM.A. 21 день тому

    why the count should be in static?

  • @akashpatil1676
    @akashpatil1676 2 місяці тому

    I have set the limit to 10 but i am getting the 11th number as well ? what can be the issue

  • @harshsrivastav2197
    @harshsrivastav2197 3 місяці тому

    I recently faced this question, and I was blank. I had watched this video even then I couldn't solve.

  • @dipujames5044
    @dipujames5044 11 місяців тому +1

    EvenAndOddPrinterBy2Threads is printing till 11 for 10count or 101 for 100count in my system.

    • @Javatechie
      @Javatechie  11 місяців тому

      Make sure to define

    • @ilyasabbasi428
      @ilyasabbasi428 8 місяців тому

      @@Javatechie Please re run in your machine multiple time. its printing till 11. I checked the condition also. and I am using apple M2 Pro.

    • @akshayyelpale2139
      @akshayyelpale2139 4 місяці тому

      f (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count

  • @nayan.j
    @nayan.j 11 місяців тому +1

    caching in webflux

  • @ilyasabbasi428
    @ilyasabbasi428 8 місяців тому

    Hi @JavaTechie,
    Your first approach is printing 1 to 11. Its strange. condition is till 10 only.
    public class EvenAndOddPrinterBy2Threads implements Runnable {
    static int count = 1;
    Object object;
    public EvenAndOddPrinterBy2Threads(Object object) {
    this.object = object;
    }
    @Override
    public void run() {
    while (count

    • @akshayyelpale2139
      @akshayyelpale2139 4 місяці тому

      if (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count

  • @abhishekmishra4296
    @abhishekmishra4296 9 місяців тому +1

    Was it asked in oracle OCI or normal oracle?

    • @Javatechie
      @Javatechie  9 місяців тому

      I believe in OCI , That's what my friend shares but how does it matter what the company is. This question is really important for any other companies as well

    • @abhishekmishra4296
      @abhishekmishra4296 9 місяців тому

      @@Javatechie sure, it’s important, I just asked as I heard OCI interview is at par with Amazon, so was just curious to know. Thanks for replying.

  • @shuchivarshney3191
    @shuchivarshney3191 11 місяців тому +1

    When will you start the multithreading playlist?

    • @Javatechie
      @Javatechie  11 місяців тому +1

      Multi threading from scratch?

    • @varshneyshuchi
      @varshneyshuchi 11 місяців тому +1

      @@Javatechie Yes

    • @KuchNahiBasAiseHi
      @KuchNahiBasAiseHi 11 місяців тому +1

      @@Javatechie Please.. good multithreading questions and concepts is one part where many experienced Java guys fail badly. Please start such sessions.

    • @vishalpattnaik1996
      @vishalpattnaik1996 11 місяців тому

      @@Javatechie yes please. multithreading and concurrency in JAVA

    • @amanabhishek8998
      @amanabhishek8998 10 місяців тому +1

      @@Javatechie yes

  • @piyushmishra889
    @piyushmishra889 7 місяців тому

    above plain thread implentation printing one more iteration

    • @akshayyelpale2139
      @akshayyelpale2139 4 місяці тому

      f (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count

  • @hamednikbakht9708
    @hamednikbakht9708 11 місяців тому

    how fetch 1000 to 1000 data from database and add to list using multithreading very fast

    • @courtneyadjoa246
      @courtneyadjoa246 4 місяці тому

      1) It can be done using Spring batch.
      2) It can also be done using CompletableFuture

  • @piyushmishra889
    @piyushmishra889 7 місяців тому

    I am trying a question with 3 threads, like one thread print even,second is odd, and third is delimter, can you help with that

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

      Delimeter ?

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

      I mean a , is printed after a even or odd print number

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

      @@Javatechie I have done that

  • @ashishsengar87
    @ashishsengar87 11 місяців тому +1

    3rd method will not work. It will depend which thread enters in synchronized block first.

    • @Javatechie
      @Javatechie  11 місяців тому

      Can you please run it multiple Times and see the results

    • @ashishsengar87
      @ashishsengar87 11 місяців тому +1

      i rant it multiple times. sometimes numbers are printed in sequence and sometimes out of sequence like 2 1 4 3..... 100 99

    • @Javatechie
      @Javatechie  11 місяців тому

      Ohh is it ? Wondering i will check and update you

  • @akshayyelpale2139
    @akshayyelpale2139 4 місяці тому

    Correct Solution for 1st method
    public class EvenAndOddPrinterBy2Threads implements Runnable {
    static int count = 1;
    Object object;
    public EvenAndOddPrinterBy2Threads(Object object) {
    this.object = object;
    }
    public static void main(String[] args) {
    Object lock = new Object();
    Runnable r1 = new EvenAndOddPrinterBy2Threads(lock);
    Runnable r2 = new EvenAndOddPrinterBy2Threads(lock);
    new Thread(r1, "even").start();
    new Thread(r2, "odd").start();
    }
    @Override
    public void run() {
    while (count

  • @GenieFolks
    @GenieFolks 7 місяців тому

    ++

  • @akshayyelpale2139
    @akshayyelpale2139 4 місяці тому

    Sorry to say this but first and third Solutions are incorrect.
    first solution prints till 11 and third solution execution is not guaranteed.

    • @Javatechie
      @Javatechie  4 місяці тому

      No need to say sorry buddy. I might be wrong my approach might be showing different behaviour let me cross check and update

  • @luisdanielmesa
    @luisdanielmesa 11 місяців тому +1

    This is awful...

    • @bhaskar287554
      @bhaskar287554 11 місяців тому

      Do you have any better approach plz be post...?

    • @Javatechie
      @Javatechie  11 місяців тому +1

      Yes please share if you can come up with any other approach? Rather than commenting it would be good for you to share some knowledge to the community if you have

  • @itsmeibrahimm
    @itsmeibrahimm 4 місяці тому +1

    I also faced this question recently thanks bro