Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic