online Ticket Booking program without using Thread Synchronization

online Ticket Booking program without using Thread Synchronization

Program
class Reserve implements Runnable
{
    int available=1;
    int wanted;
    Reserve ()
    {
        wanted=1;
    }
    public void run()
    {
        System.out.println("avalable Tickets are "+available);
        if(available>=wanted)
        {
            String name=Thread.currentThread().getName();
            System.out.println(wanted+" Ticket reserved for person "+name);
            try
            {
                Thread.sleep(2000);
                available=available-wanted;
            }
            catch(Exception e)
            {
                System.out.println(e);
            }
        }
        else
        {
            System.out.println("sorry ! no Tickets Available");
        }
    }
}
public class Main
{
    public static void main(String args[])
    {
        Reserve r=new Reserve();
        Thread t1=new Thread(r);
        Thread t2=new Thread(r);
        Thread t3=new Thread(r);
        Thread t4=new Thread(r);
        t1.start();
        t2.start();
        t3.start();
        t4.start();
    }
}


Output:

avalable Tickets are 1
avalable Tickets are 1
avalable Tickets are 1
avalable Tickets are 1
1 Ticket reserved for person Thread-0
1 Ticket reserved for person Thread-1
1 Ticket reserved for person Thread-2
1 Ticket reserved for person Thread-3


Multi Threading In Java



More Questions


215 . Write a Java-program to Count the number of letters in a given string ?
216 . Write a Java-program to Count the number of Numbers in a given string ?
217 . Write a Java-program to Remove vowels in a given string ?
218 . Write a Java-program to Remove consonants in a given string ?
219 . Write a Java-program to Remove special characters in a given string ?
220 . Write a Java-program to Remove spaces in a given string ?
221 . Write a Java-program to Remove letters in a given string ?
222 . Write a Java-program to Remove numbers in a given string ?
223 . Write a Java-program to print duplicate characters from the string ?
224 . Write a Java-program to print unique(distinct (or non-repeating characters) ) characters from the string ?
225 . Write a java program How to check if two strings are anagrams of each other?