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

  Рет қаралды 29,520

Java Techie

Java Techie

Күн бұрын

🇮🇳🇮🇳 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...
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.co...
IOS 🌐 : apps.apple.com...
GitHub:
github.com/Jav...
Blogs:
/ javatechie
Facebook:
/ javatechie
Join this channel to get access to perks:
www.youtube.co...
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

Пікірлер: 81
@sayanbiswas8847
@sayanbiswas8847 Жыл бұрын
I also had faced this question around two years back in one of my interviews. Thanks for covering this
@gopisambasivarao5282
@gopisambasivarao5282 Жыл бұрын
Basant, awesome explanation, appreciate your efforts...God Bless you.
@itsmeibrahimm
@itsmeibrahimm 6 ай бұрын
I also faced this question recently thanks bro
@rahultyagi7490
@rahultyagi7490 22 күн бұрын
I have learned many things from you and your tutorials , God bless you always like that@javatechiee
@satyabratasahoo967
@satyabratasahoo967 6 ай бұрын
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 2 ай бұрын
Exactly bro if we are using threadname then there is no need for wait and notify
@yoshitamahajan353
@yoshitamahajan353 3 ай бұрын
Plz make video for aws interviews
@mrmohit1112
@mrmohit1112 Жыл бұрын
Sir,can you please create a full detailed video on 2-way email communication in microservices using rest apis and POP3 configurations
@rajenderprasad1193
@rajenderprasad1193 Жыл бұрын
Thank you Basant bhai
@GuruNanakTV
@GuruNanakTV 11 ай бұрын
Very complex solutions, it can be done very easily without code duplication
@amanabhishek8998
@amanabhishek8998 Жыл бұрын
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 Жыл бұрын
Please check my spring batch video i have explained this usecase
@amanabhishek8998
@amanabhishek8998 Жыл бұрын
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 😢
@singhkk138
@singhkk138 5 ай бұрын
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
@KuchNahiBasAiseHi
@KuchNahiBasAiseHi Жыл бұрын
very nice session. Thanks.
@ravi.m4954
@ravi.m4954 10 ай бұрын
@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
@yoshitamahajan353
@yoshitamahajan353 3 ай бұрын
Thank u even i faced this qt
@gopishettymahindra2713
@gopishettymahindra2713 Жыл бұрын
super explanation sir.
@kumarmahendra2611
@kumarmahendra2611 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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 😊
@Manoj-zx6ye
@Manoj-zx6ye Жыл бұрын
HI,could you make video on spring boot jwt with redies database .
@mrmohit1112
@mrmohit1112 Жыл бұрын
Great sir ❤
@vidhanchandra3997
@vidhanchandra3997 Жыл бұрын
Eagerly waiting for your next microservices design pattern....😊😊😊😊
@Javatechie
@Javatechie Жыл бұрын
Yes I will do soon
@kiranvish1
@kiranvish1 6 ай бұрын
Could u please explain what's the importance of Thread.sleep call in Main method.
@revanthchinnu8904
@revanthchinnu8904 11 ай бұрын
Hi basant , can you please make videos of multi threading like thread life cycle , volatile keyword, wait and sleep , synchronisation
@Javatechie
@Javatechie 11 ай бұрын
Okay i will do that
@neha6000
@neha6000 Жыл бұрын
Thank you for this much good content
@sameergurumukhi
@sameergurumukhi 10 ай бұрын
In the last approach, how is the excecution sequence of odd followed by even is guranteed ?
@dipujames5044
@dipujames5044 Жыл бұрын
EvenAndOddPrinterBy2Threads is printing till 11 for 10count or 101 for 100count in my system.
@Javatechie
@Javatechie Жыл бұрын
Make sure to define
@ilyasabbasi428
@ilyasabbasi428 10 ай бұрын
@@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 6 ай бұрын
f (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count
@MYSTERIOMAXXX
@MYSTERIOMAXXX Жыл бұрын
@java techie Hi bro your last methods is incorrect... Rerun it few times and worker-2 will get executed first..as operation is async..
@MuraliM.A.
@MuraliM.A. 2 ай бұрын
why the count should be in static?
@reenarodrigues8976
@reenarodrigues8976 10 ай бұрын
Hi sir can u create seperate series on multithreading playlist for java
@Javatechie
@Javatechie 10 ай бұрын
Yes I will
@Anushaaa_
@Anushaaa_ Жыл бұрын
👍
@akashpatil1676
@akashpatil1676 4 ай бұрын
I have set the limit to 10 but i am getting the 11th number as well ? what can be the issue
@LexterCarlAlmirol
@LexterCarlAlmirol Жыл бұрын
Hi can you make tutorial about JCR (Java content repository), it is hard to find tutorial online about that topic.
@Javatechie
@Javatechie Жыл бұрын
I am not aware of this concept. No worries i will check this out and update you
@sagararora5519
@sagararora5519 Жыл бұрын
Hi can u make tutorial on websocket implementation on spring boot microservice aritecture on api gateway example usi
@abhishekmishra4296
@abhishekmishra4296 11 ай бұрын
Was it asked in oracle OCI or normal oracle?
@Javatechie
@Javatechie 11 ай бұрын
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 11 ай бұрын
@@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 Жыл бұрын
When will you start the multithreading playlist?
@Javatechie
@Javatechie Жыл бұрын
Multi threading from scratch?
@varshneyshuchi
@varshneyshuchi Жыл бұрын
@@Javatechie Yes
@KuchNahiBasAiseHi
@KuchNahiBasAiseHi Жыл бұрын
@@Javatechie Please.. good multithreading questions and concepts is one part where many experienced Java guys fail badly. Please start such sessions.
@vishalpattnaik1996
@vishalpattnaik1996 Жыл бұрын
@@Javatechie yes please. multithreading and concurrency in JAVA
@amanabhishek8998
@amanabhishek8998 Жыл бұрын
@@Javatechie yes
@ilyasabbasi428
@ilyasabbasi428 10 ай бұрын
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 6 ай бұрын
if (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count
@testAutomation-z8d
@testAutomation-z8d Жыл бұрын
in case of completable future example how it make sure that odd thread only executes first in second and third example?
@abhinavpandey3356
@abhinavpandey3356 2 ай бұрын
Yes I am also having the same question
@harshsrivastav2197
@harshsrivastav2197 5 ай бұрын
I recently faced this question, and I was blank. I had watched this video even then I couldn't solve.
@ashishsengar87
@ashishsengar87 Жыл бұрын
3rd method will not work. It will depend which thread enters in synchronized block first.
@Javatechie
@Javatechie Жыл бұрын
Can you please run it multiple Times and see the results
@ashishsengar87
@ashishsengar87 Жыл бұрын
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 Жыл бұрын
Ohh is it ? Wondering i will check and update you
@piyushmishra889
@piyushmishra889 9 ай бұрын
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 8 ай бұрын
Delimeter ?
@piyushmishra889
@piyushmishra889 8 ай бұрын
I mean a , is printed after a even or odd print number
@piyushmishra889
@piyushmishra889 8 ай бұрын
@@Javatechie I have done that
@akshayyelpale2139
@akshayyelpale2139 6 ай бұрын
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
@piyushmishra889
@piyushmishra889 9 ай бұрын
above plain thread implentation printing one more iteration
@akshayyelpale2139
@akshayyelpale2139 6 ай бұрын
f (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count
@nayan.j
@nayan.j Жыл бұрын
caching in webflux
@hamednikbakht9708
@hamednikbakht9708 Жыл бұрын
how fetch 1000 to 1000 data from database and add to list using multithreading very fast
@courtneyadjoa246
@courtneyadjoa246 6 ай бұрын
1) It can be done using Spring batch. 2) It can also be done using CompletableFuture
@akshayyelpale2139
@akshayyelpale2139 6 ай бұрын
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 6 ай бұрын
No need to say sorry buddy. I might be wrong my approach might be showing different behaviour let me cross check and update
@GenieFolks
@GenieFolks 9 ай бұрын
++
@luisdanielmesa
@luisdanielmesa Жыл бұрын
This is awful...
@bhaskar287554
@bhaskar287554 Жыл бұрын
Do you have any better approach plz be post...?
@Javatechie
@Javatechie Жыл бұрын
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
@kbpmowrya
@kbpmowrya Жыл бұрын
I also faced this question recently thanks bro
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 12 МЛН
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 13 МЛН
Good teacher wows kids with practical examples #shorts
00:32
I migliori trucchetti di Fabiosa
Рет қаралды 6 МЛН
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 832 М.
Java 8 Streams | Optional Usage and Best Practices | JavaTechie
22:22
SOLID Design Principles in java with Example | JavaTechie
34:44
Java Techie
Рет қаралды 191 М.
How do Cats Eat Watermelon? 🍉
00:21
One More
Рет қаралды 12 МЛН