• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

thread doubt

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers ,
this question id from k&b from threads self test
can any one explain me this programm i am not able to understand

public class Messager implements Runnable
{
public static void main(String[] args)
{
new Thread(new Messager("wallace")).start();
new Thread(new Messager("gromit")).start();
}
private String name;
public Messager(String name)
{
this.name=name;
}
public void run()
{
message(1);
message(2);
}
private synchronized void message(int n)
{
System.out.print(name + "-" + n + " ");
}
}
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The program seems clearcut.

public class Messager implements Runnable
{
public static void main(String[] args)
{
new Thread(new Messager("wallace")).start(); //new thread object
//created and started
new Thread(new Messager("gromit")).start(); //same here
}
private String name;
public Messager(String name) //object Messager constructor
{
this.name=name; // setts arg name to instance variable
}
public void run() // the run method
{
message(1); //run the message(int n) method
message(2); //again by passing another parameter
}
private synchronized void message(int n) //thread lock
{
System.out.print(name + "-" + n + " "); //prints the thread name and the
//passed parameter
}
}

RJ
 
ranjitsingh thakurratan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ranjitsingh thakurratan:
The program seems clearcut.

RJ

reply
    Bookmark Topic Watch Topic
  • New Topic