• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Thread Programming

 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,
I am trying my hands on basic thread programming.

My requirement is as follows.(It is just a trial in Multi threading not a real time scenario).

I need to print the numbers from 1 to 10 and 100 to 99 in the following way

1 99 2 98 3 97 4 96.....

I have done some homework by referring 'Complete Reference' and couple other books.

I have written a class with a method which takes an integer argument and prints it.
I have synchronized the method.
I have created two threads one, printing 1 to 10, and another 99 to 90.
I start both the threads and call the method to print the number passed in.


I am not able to effectively give turns between the two threads so that, I can alternately call the method from each thread and print the number as I want.
I guess wait() and notify can be used but I am not able to get it right.

Experts, would you please help?

This my program



public class ForwardReverse {

/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {

X m = new X();

Forward obj1 = new Forward(m);
Thread t1 = new Thread(obj1);




Reverse obj2 = new Reverse(m);
Thread t2 = new Thread(obj2);

t1.start();
t2.start();



}

}

class Forward implements Runnable
{
X x;
int n=1;

Forward(X x)
{
this.x=x;
}

public void run()
{
while(n<=10)
{
x.print(n);
n++;
}

}
}

class Reverse implements Runnable
{
X x;
int n=100;

Reverse(X x)
{
this.x=x;
}

public void run()
{
while(n>=90)
{
x.print(n);
n--;
}

}
}


class X
{

boolean setvalue = false;

public synchronized void print(int n)
{


try
{
wait();
}

catch(InterruptedException e)
{
e.printStackTrace();
}

System.out.println(n);
setvalue = true;
notify();

}

}


 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Rajaram,

Just adding your code in tags because the one you posted is very hard to read.

Its advisable to use the code tags for future
Cheers
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rajaraman navaneethan wrote:I am not able to effectively give turns between the two threads so that, I can alternately call the method from each thread and print the number as I want.

So TellTheDetails (<= that's a link), what does happen?

Walk through your print method. Pretend Forward gets to that method first, what does it do? Then assume Reverse gets to the method, what does it do? Use a piece of paper, and, using three columns, write down the steps that Forward does in sequence in column 1 and the sequence of what Reverse does in column 3. Then, anything inside a synchronized block, write down that sequence in column 2 (drawing a line from the Forward or Reverse column to indicate which thread is executing inside the synchronized block. For example, a quick example of how I might do it (please excuse my poor ascii art)


So what is wrong with this scenario? What do you actually see happening? How might you fix it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic