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.
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?