Why do all the producers are not Shared a num, every producer output the output of the 1, 2... , I according to the example of the book can be Shared, if there is a problem of the inner class, but if there is no inner class I and how to realize the communication between producers and consumers, Ask a question, by the way, that is I according to the example of the book to knock, but every time the result of the execution is almost completed by a window all ticketing, wasn't up to the window of the upper and lower two tickets must be different, the code is as follows: Morally sleep (100) is not the same thread execute continuous
package ThreadTest;
The class SaleThread2 implements Runnable { Int tickets=10; Object lock=new Object(); @ Override Public void the run () { //TODO Auto - generated method stub While (true) { Synchronized (lock) { If (tickets> 0 { Try { Thread.sleep(100); {} catch InterruptedException (e) //TODO Auto - generated the catch block, e.printStackTrace(); } System. The out. Println (Thread. CurrentThread (). The getName () + "is selling the first" + tickets - + "ticket"); } } } }
}
Public class Example12 { Public static void main (String [] args) { SaleThread2 st=new SaleThread2 (); New Thread (st, "window 1"). The start (); New Thread (st, "window 2"). The start (); New Thread (st, "window 3"). The start (); New Thread (st, 4 "window"). The start (); } }