This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Doubt in Thread Programming

 
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();

}

}


 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • 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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic