Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this Approach ; </p> <pre><code>import java.io.*; import java.lang.*; public class Cinema { int no=0,price=0; synchronized void reservation(int n,int p) { no=no+n; price=price+p; } public static void main(String args[])throws IOException, InterruptedException { Cinema c=new Cinema(); Thread t1 = new Thread(new Counter(c,"Counter 1")); t1.start(); Thread t2 = new Thread(new Counter(c,"Counter 2")); t2.start(); Thread t3 = new Thread(new Counter(c,"Counter 3")); t3.start(); t1.join(); t2.join(); t3.join(); try { Thread.sleep(100); } catch(InterruptedException ie) { System.out.print("\n"+ie); } System.out.print("\nTotal no. of tickets :"+c.no); System.out.print("\nTotal Money collected:"+c.price); } } class Counter implements Runnable { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Cinema c; int not,cost; String counterName; Counter(Cinema c,String counterName) { this.c=c; this.counterName=counterName; } public void run() { try { synchronized(c) { System.out.print("\n" + counterName); System.out.print("\nEnter the no. of tickets :"); not=Integer.parseInt(br.readLine()); cost=not*150; c.reservation(not,cost); } } catch(IOException e){System.out.print("\n"+e);} } } </code></pre> <ol> <li><p>I have made a single Counter class instead of 3 classes you are using.</p></li> <li><p>I made the reservation method to be synchronized.</p></li> <li><p>I called join method on all the three threads. So the program will not terminate abruptly.TGhe last thread that would teminate would be main.</p></li> <li><p>In run() method , I locked the Cinema object c. This will resolve your issue of Buffered Reader at this moment of time. But in real world scenario different threads will be run by different people. So no need to lock the Cinema object there.it is just for your usage.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload