Thread starts running and terminates itself
java, multithreading
Solution
I can compile it just fine, but when I run the program, this is what I get ...
There are a couple of things wrong with your program:
The main thread is not waiting for the `Counter` threads to finish before it prints out the totals. If you need to wait for a thread to complete then you call `thread.join()` on it.
Thread counter1 = new Thread(new Counter1(c));
counter1.start();
// start other threads here...
// now wait for the counter1 to finish
counter1.join();
In your case, the 3 `Counter`s are forked but the main only sleeps a bit and then quits. The `Counter` threads are still running.
Each of the `Counter` threads is adding values to fields inside the `Cinema` but there is no synchronization in `Cinema`. Anytime two threads are modifying the same field, there must be some mutex protection and memory synchronization.
The easy thing to do here is to make the `Cinema.reservation(...)` method be `synchronized`. Then each of the `Counter` objects will get a lock on the `Cinema` instance which will ensure only one `Counter` updates the `Cinema` at one time. The `synchronized` keyword also ensures that the fields in the `Cinema` object are memory synchronized as well.
synchronized void reservation(int n,int p) { ...
As always, you should consider using the `ExecutorService` classes instead of forking threads yourself. See the Java tutorial.
Problem
Update : Thanks everyone! I've modified the program as per the suggestions and the code given below is the modified code. Original Post : 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. And I've written a program for the same which you can see below: ``` 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); } } ``` I can compile it just fine, but when I run the program, this is what I get -->LINK (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.