Print odd and even numbers with thread T1 and T2 respectively, If prime number comes, print in reverse order

Print odd and even numbers with thread T1 and T2 respectively, If prime number comes, print in reverse order



I recently had an interview with Morgan Stanley and was asked this question. I had to print all odd numbers when thread T1 is running and even numbers when T2 is running, but hey! there is more, If any prime number comes up then I had to reverse the order meaning now odd numbers with T2 and even numbers with T1.



Below is what I had tried but that guy looked at me like he wants to put me in jail for this solution. I'm new to multi-threading. There might be a way to establish the communication between multiple threads to achieve this using notify() and wait() methods. Could someone pull me out of that jail?


notify()


wait()


public class MainClass extends Thread
@Override
public void run()
for(int i=0;i<20;i++)
String tname = Thread.currentThread().getName();
if(isPrime(i))
switch(tname)
case "T2":
if(i%2 != 0)
System.out.println(tname + " - "+i);
break;
case "T1":
if(i%2 == 0)
System.out.println(tname + " - "+i);
break;
default:break;

else
switch(tname)
case "T1":
if(i%2 != 0)
System.out.println(tname + " - "+i);
break;
case "T2":
if(i%2 == 0)
System.out.println(tname + " - "+i);
break;
default:break;




public boolean isPrime(int num)
if(num <= 1) return false;
if(num == 2) return true;
for(int i=2;i<=num/2;i++)
if(num%i == 0)
return false;

return true;

public MainClass(String name)
super(name);

public static void main(String args)
MainClass t1 = new MainClass("T1");
MainClass t2 = new MainClass("T2");
t1.start();
try
t1.join();//always keep it inside the try otherwise compile time error
catch(InterruptedException ie)
ie.printStackTrace();

t2.start();




And here is the output.


T1 - 1
T1 - 2
T1 - 9
T1 - 15
T2 - 0
T2 - 3
T2 - 4
T2 - 5
T2 - 6
T2 - 7
T2 - 8
T2 - 10
T2 - 11
T2 - 12
T2 - 13
T2 - 14
T2 - 16
T2 - 17
T2 - 18
T2 - 19





I'd ask the interviewer why you are being asked to use two threads to process this few things. And why run the two threads one after the other?
– Andy Turner
Aug 28 at 20:08






You could cache the biggest prime already calculated. Then in your isPrime method instead of starting i = 2 you start i = biggestPrime.
– fpezzini
Aug 28 at 23:16






I think you could also use extra Thread reference variables called evenThread and oddThread. You start them with oddThread = T1 and evenThread = T2. When it's a prime number you only invert the reference. Then you can pull the switch block outside of the if (isPrime()).
– fpezzini
Aug 28 at 23:20





@Andreas Could you please provide an answer with better version of my code?
– MAC
Aug 29 at 11:34









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế