import java.util.*;

public class Hotel
{
	static Random r;
	
	public static void main(String argv[])
	{
		int noThreads = Integer.parseInt(argv[0]);
		
		r = new Random();
		Person[] threadObjects = new Person[noThreads];
		Thread[] threads = new Thread[noThreads];

		for (int i=0; i<noThreads; i++)
		{
			threadObjects[i] = new Person();
		} //for

		for (int i=0; i<noThreads; i++)
		{
			threads[i] = new Thread( threadObjects[i] );
			threads[i].start();
		} //for
	} //main()
	
	public static synchronized 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 static 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");
	} //eat()
} //class Hotel
// --------------------------------------------------------
class Person implements Runnable
{
	public void run()
	{
		for(int i=0; i<10; i++)
		{
			Hotel.visitRoom();
			Hotel.eat();
		} //for
	} //run()
} //class Person	