import java.util.*;

public class Hotel2
{
	static Random r;
	
	public static void main(String argv[])
	{
		int noThreads = Integer.parseInt(argv[0]);
		Hotel2 h = new Hotel2();
		
		r = new Random();
		Person[] threadObjects = new Person[noThreads];
		Thread[] threads = new Thread[noThreads];

		for (int i=0; i<noThreads; i++)
		{
			threadObjects[i] = new Person(h);
		} //for

		for (int i=0; i<noThreads; i++)
		{
			threads[i] = new Thread( threadObjects[i] );
			threads[i].start();
		} //for
	} //main()
	
	public synchronized static void visitRoom()
	{
		System.out.println("thread named "+Thread.currentThread().getName()+" entered room");
		try
		{
			Thread.sleep(r.nextInt(1000));
		} //try
		catch (InterruptedException ie)
		{
			System.out.println("An InterruptedException occured\n"+ie.getMessage());
			ie.printStackTrace();
			System.exit(1);
		} //catch()
		System.out.println("thread named "+Thread.currentThread().getName()+" left room");
	} //visitRoom()

	public synchronized void eat()
	{
		System.out.println("thread named "+Thread.currentThread().getName()+" is eating");
		try
		{
			Thread.sleep(r.nextInt(1000));
		} //try
		catch (InterruptedException ie)
		{
			System.out.println("An InterruptedException occured\n"+ie.getMessage());
			ie.printStackTrace();
			System.exit(1);
		} //catch()
		System.out.println("thread named "+Thread.currentThread().getName()+" meal ended");
	} //visit()
} //class Hotel2
// --------------------------------------------------------
class Person implements Runnable
{
	Hotel2 h;
	
	public Person(Hotel2 h)
	{
		this.h = h;
	} //constructor
	
	public void run()
	{
		for(int i=0; i<10; i++)
		{
			Hotel2.visitRoom();
			h.eat();
		} //for
	} //run()
} //class Person