Thursday, April 10, 2014

Q 1 CHAPTER 9 ACTIVITY BOOK THREADING TOLL BRIDGE

question 1:-

Write an application to simulate the vehicles crossing a toll bridge on a motorway. For the purpose of this exercise, simulate the environment for five vehicles that are approaching the bridge and the toll booth.
 The vehicles are numbered from one to five. The vehicles have to go through the following five stages: Start the journey Arrive at the toll Enter the toll booth Exit the toll booth Cross the bridge The vehicles can be in any of the stages in parallel,
 except the third stage as only one vehicle can enter the toll booth at a time. The simulation is performed by having one thread for each vehicle, and consists of the following classes: Vehicle: Simulates the behavior of a vehicle. TollBooth: Simulates the behavior of a toll booth. Simulate:
 Creates the vehicles and controls the simulation.

Ans:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class vehical
    {
        private int id;
        private static TollBooth = new TollBooth();
        public vehicle(int id)
        {
            this.id=id;
        }
        public void exec()
        {
            Console.WriteLine("vehicle {0} starts journey",id+1);
            Random RandGen = new Random();
            int Rnd=RandGen .Next(0,100);
            travel (Rnd);
            Console.WriteLine("vehicle {0} arrives at the toll ", id+1);
            toll.useToll(this);
            travel (Rnd);
            Console.WriteLine("vehicle {0} has crossed the bridge ",id+1);
        }
        public int getVehicleId()
        {
            return this.id;
        }
        public void travel (int time)
        {
            int limit =500000;
            for(int j=0; j<time;j++)
            {
                for(int k =0 ; k<limit;k++)
                    (// do nothing);
                }
            }
    }
    public class TollBooth
    {
        bool inUse;
       
        public TollBooth()
        {
            inUse=false;
        }
        public void useToll(Vehicle vehicle)
        {
            if(inUse==false)
            {
                inUse=true;
                Console.WriteLine("vehicle {0} enters tollbooth ", vehicle.getVehicleId()+1);
                vehicle.travel(50);
                Console.WriteLine("Vehicle {0} exits tollbooth ",vehicle.getVehicleId()+1);
                inUse=false;
                break;
            }
        }
    }
}
public class Simulate
{
    private static int noOfVehicles =5;
    private static Vehicles[] vehicles;
    public static void Main(string[]args)
    {
        try
        {
            Simulate sm=new Simulate();
            vehicles=new Vehicles[5];
            for(int i=0;i<noOfVehicles;i++)
            {
                vehicles[i]=new Vehicles(i);
                Thread t=new Thread(new ThreadStart(vehicles[i].exec);
                t.Start();
                Thread.Sleep(100);
            }
            Console.ReadLine();
        }
        catch (Exception ex)
        {
            System.Console.WriteLine(ex);
        }
    }
}

No comments:

Post a Comment