Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Odd-Even Number print program

 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I don't know threads much....
During an interview I was asked to write a program so that THread1 always prints ODD number and Thread2 always prints EVEN number.
Can anybody please help me with the program.

Thanks,
Purvesh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make a runnable that increments by 2, start thread 1 at 1 and start thread 2 at 2?

Now if they really meant to make two threads that access a shared resource in turn, that would be more work. Do you think the runnable says this:

System.out.println( sharedResource.getNextInt() );

and we are supposed to get T1 and T2 to alternate?
 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Stan.

As u suggested I tried the foll. prog.

public class Test {

public static void main(String[] args) {
OddEven o1 = new OddEven("Odd Number Thread" , 1);
OddEven o2 = new OddEven("Even Number Thread" , 2);
o1.start();
o2.start();
}
}

====
public class OddEven extends Thread {
/**
* @param string
*/

private int number;

public OddEven(String name, int p_number){
super(name);
this.number = p_number;
}

public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(getName()+ this.number );
this.number +=2;
}
}
}

====
The above program first of all prints all odd numbers and then even number.
But What if I want to print the odd number followed by even number and then odd number then....

Need to use notifyall, wait and Synchronize...

Pls help.

Thanks & Regards,
Purvesh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you could pass your two threads a common object and they could lock and unlock on that object. I'd have to stare at the ceiling a while to think of how to do it. Any ideas?
 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan,

I tried foll. after studying sun's thread tutorial.

some hardcoding in the code but it works.....

=====
public class Test {

public static void main(String[] args) {
System.out.println("Odd Even test");
NumHolder objNumHolder = new NumHolder();
Odd o1 = new Odd(objNumHolder, "Odd Number Thread");
Even e1 = new Even(objNumHolder, "Even Number Thread");
o1.start();
e1.start();
}
}

=============
public class NumHolder {
private int intCurrNum;
private boolean isEven = false;

public synchronized void printOddNumber(String tname) {
while (isEven == true){
try {
wait();
}catch (InterruptedException e) {
}
}
isEven = true;
System.out.println("Thread Name="+tname + "===Number="+intCurrNum);
intCurrNum += 1;
notifyAll();
}

public synchronized void printEvenNumber(String tname) {
while (isEven == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
isEven = false;
System.out.println("Thread Name="+tname + "===Number="+intCurrNum);
intCurrNum += 1;
notifyAll();
}
}
===================

public class Even extends Thread {
private NumHolder objNumHolder;

public Even(NumHolder p_objNumHolder, String name) {
super(name);
objNumHolder=p_objNumHolder;
}

public void run() {
for (int i = 0; i < 10; i++) {
objNumHolder.printEvenNumber(getName());
}
}
}

======================
public class Odd extends Thread {
private NumHolder objNumHolder;

public Odd(NumHolder p_objNumHolder,String name) {
super(name);
objNumHolder = p_objNumHolder;
}

public void run() {
for (int i = 0; i < 10; i++) {
objNumHolder.printOddNumber(getName());
}
}
}
======

Regards,
Purvesh
 
vrayudu kumar
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rupesh,

am new to threads.Can u explain this program..
 
Purvesh Vora
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a minor change in the program in the NumHolder class...
change
private int intCurrNum;
to
private int intCurrNum = 1;
============

Here is a shared object NumHolder which has method to print odd and even number and that methods are synchronized.

Odd and Even classes extend thread and internally call printOddNumber or printEvenNumber methods in their run() method....

In the Test.java created one NumHolder objNumHolder and instance of the Odd and Even class(o1, e1).
Start both the treads by calling o1.start() and e1.start()...
In the o1.run() its calling printOddNumber(String threadName)
And in the e1.run()its calling printEvenNumber(String threadName)

Initially isEven variables value is false. So even if the printEvenNumber method from e1 is executed before printOddNumber from o1 it will wait. So e1 will wait till the isEven is changed to true. So the o1's printOddNumber will get executed and it will print the statement...
after that we are changing boolean status of the isEven to true. And calling notifyAll.
So waiting thread of e1 will go ahead and print its value.

Hope this clarified.

Regards,
Purvesh
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool! Let's make it harder. Can you do it with only one runnable class and only one print method? That's where I was trying to go ... and not having any great ideas yet.
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe a static counter, two parameters (just in case we need it for threads that should only print when the num is (1 % 3) .....

call them i and j. the thread should only print (and notifyAll()) when
(counter == (i % j))

otherwise it calls wait().

just a couple of thoughts...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic