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 - Наука та технологія
I also had faced this question around two years back in one of my interviews. Thanks for covering this
Basant, awesome explanation, appreciate your efforts...God Bless you.
I also faced this question recently thanks bro
very nice session. Thanks.
Thank you Basant bhai
super explanation sir.
Great sir ❤
Thank you for this much good content
Sir,can you please create a full detailed video on 2-way email communication in microservices using rest apis and POP3 configurations
Very complex solutions, it can be done very easily without code duplication
Thank u even i faced this qt
@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
Eagerly waiting for your next microservices design pattern....😊😊😊😊
Yes I will do soon
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 ?
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
@@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 😊
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
HI,could you make video on spring boot jwt with redies database .
Plz make video for aws interviews
👍
In the last approach, how is the excecution sequence of odd followed by even is guranteed ?
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
Please check my spring batch video i have explained this usecase
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 😢
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
Exactly bro if we are using threadname then there is no need for wait and notify
Hi can u make tutorial on websocket implementation on spring boot microservice aritecture on api gateway example usi
Hi basant , can you please make videos of multi threading like thread life cycle , volatile keyword, wait and sleep , synchronisation
Okay i will do that
Could u please explain what's the importance of Thread.sleep call in Main method.
@java techie Hi bro your last methods is incorrect...
Rerun it few times and worker-2 will get executed first..as operation is async..
Hi can you make tutorial about JCR (Java content repository), it is hard to find tutorial online about that topic.
I am not aware of this concept. No worries i will check this out and update you
in case of completable future example how it make sure that odd thread only executes first in second and third example?
Yes I am also having the same question
Hi sir can u create seperate series on multithreading playlist for java
Yes I will
why the count should be in static?
I have set the limit to 10 but i am getting the 11th number as well ? what can be the issue
I recently faced this question, and I was blank. I had watched this video even then I couldn't solve.
EvenAndOddPrinterBy2Threads is printing till 11 for 10count or 101 for 100count in my system.
Make sure to define
@@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.
f (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count
caching in webflux
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
if (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count
Was it asked in oracle OCI or normal oracle?
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
@@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.
When will you start the multithreading playlist?
Multi threading from scratch?
@@Javatechie Yes
@@Javatechie Please.. good multithreading questions and concepts is one part where many experienced Java guys fail badly. Please start such sessions.
@@Javatechie yes please. multithreading and concurrency in JAVA
@@Javatechie yes
above plain thread implentation printing one more iteration
f (count % 2 != 0 && Thread.currentThread().getName().equals("odd") && count
how fetch 1000 to 1000 data from database and add to list using multithreading very fast
1) It can be done using Spring batch.
2) It can also be done using CompletableFuture
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
Delimeter ?
I mean a , is printed after a even or odd print number
@@Javatechie I have done that
3rd method will not work. It will depend which thread enters in synchronized block first.
Can you please run it multiple Times and see the results
i rant it multiple times. sometimes numbers are printed in sequence and sometimes out of sequence like 2 1 4 3..... 100 99
Ohh is it ? Wondering i will check and update you
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
++
Sorry to say this but first and third Solutions are incorrect.
first solution prints till 11 and third solution execution is not guaranteed.
No need to say sorry buddy. I might be wrong my approach might be showing different behaviour let me cross check and update
This is awful...
Do you have any better approach plz be post...?
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
I also faced this question recently thanks bro