Note that there are some explanatory texts on larger screens.

plurals
  1. POThread starts running and terminates itself
    text
    copied!<hr> <p><strong>Update</strong> : Thanks everyone! I've modified the program as per the suggestions and the code given below is the modified code.</p> <hr> <p><strong>Original Post :</strong> I've gone through some "Apply and Analyze" type of questions and in one question, the programmer has been asked to apply multithreading concept for three reservation counters of a cinema theater and calculate the total booking numbers and amount collected in a show.</p> <p>And I've written a program for the same which you can see below:</p> <pre><code>import java.io.*; import java.lang.*; class Cinema { int no=0,price=0; synchronized void reservation(int n,int p) { no=no+n; price=price+p; } } class Counter implements Runnable { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Cinema c; int not,cost; Counter(Cinema c) { this.c=c; } public void run() { try { System.out.print("\nCounter 1"); 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);} } } class CinemaMain { public static void main(String args[])throws IOException { Cinema c=new Cinema(); System.out.print("\nCounter 1"); Thread c1=new Thread(new Counter(c)); c1.start(); c1.join(); System.out.print("\nCounter 2"); Thread c2=new Thread(new Counter(c)); c2.start(); c2.join(); System.out.print("\nCounter 3"); Thread c3=new Thread(new Counter(c)); c3.start(); c3.join(); try { Thread.sleep(500); } 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); } } </code></pre> <p>I can compile it just fine, but when I run the program, this is what I get --><a href="http://puu.sh/5D2JN.png" rel="nofollow">LINK</a> (since I don't have 10 reputation, I couldn't post the image here, sorry!) I don't know why, it doesn't ask for input even though I've written the code to get input in the run method.</p>
 

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