This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Doubt in Thread Programming Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Doubt in Thread Programming" Watch "Doubt in Thread Programming" New topic

Doubt in Thread Programming

rajaraman navaneethan
Ranch Hand

Joined: Feb 26, 2005
Posts: 86
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);




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

Forward(X x)

public void run()


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

Reverse(X x)

public void run()


class X

boolean setvalue = false;

public synchronized void print(int n)


catch(InterruptedException e)

setvalue = true;



Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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

Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Steve Luke

Joined: Jan 28, 2003
Posts: 4164

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?

I agree. Here's the link:
subject: Doubt in Thread Programming
Similar Threads
threading single instance vs multiple instances
Is it possible to acceessdifferent parts of class by different threads?
Thread with synchronized method
Thread synchronization
Basic thread doubt