Package Penalty for com.ring2.test; / ** * definition of a gun can only be filled with 20 bullets * When the number is greater than 20 bullets, bullets producers to stop production, waiting to be awakened * When a bullet is equal to 0, consumption to stop shooting bullets, waiting to be awakened * * / public class Gun { Private static int bullet = 0 ; Private static int maxBullet = 20 ; Private static String = Lock "at the Same, Object" ; static class Provider the implements Runnable { @Override public void RUN () { for ( int0 = I; I <20 is; I ++ ) { the try { the Thread.sleep ( 1000 ); } the catch (InterruptedException E) { e.printStackTrace (); } the synchronized (Lock) { IF (bullet == maxBullet) { the try { Lock .wait (); System.out.println ( "full bore, loading is stopped, waiting for a notification." ); } the catch (InterruptedException E) { e.printStackTrace (); } } Bullet ++ ; System.out.println (Thread.currentThread () getName (). + "A loading completed, the current number of cartridges is:" + bullet); Lock.notifyAll (); } } } } static class Consumer the implements the Runnable { @Override public void RUN () { for ( int I = 0; I <20 is; I ++ ) { the try { the Thread.sleep ( 1000); } The catch (InterruptedException E) { e.printStackTrace (); } the synchronized (Lock) { IF (bullet == 0 ) { the try { Lock.wait (); System.out.println ( "number of bullets is empty and ready . bullet loaded " ); } the catch (InterruptedException E) { e.printStackTrace (); } } bullet - ; System.out.println (Thread.currentThread () getName (). + "One shot is completed, the current number of cartridges is:" + bullet); Lock.notifyAll (); } } } } public static void main (String [] args ) { System.out.println ( "start thread" ); new new the thread ( new new Provider ()) start. (); new new the thread ( new new Consumer ()) start ();. } }
Results of the:
Start threads the Thread -0 once loading is completed, the current number of bullets is: 1 the Thread -1 complete a shot, the current number of bullets is: 0 the Thread -0 once loading is completed, the current number of bullets is: 1 the Thread -1 complete a shot, The current number of cartridges is: 0 the Thread -0 complete a loading, the current number of cartridges is: 1 number of cartridges is empty, waiting for loading bullets. The Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading current bullets an amount of:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a package shells, the current number of cartridges: 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is: 1 number of cartridges is empty, waiting for loading bullets. Thread-1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading The current number of bullets: 1 number of cartridges is empty, waiting for loading bullets. The Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading current bullets an amount of:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a package shells, as the current number of cartridges:. 1 the Thread-1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is: 1 number of cartridges is empty, waiting for loading bullets. The Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading current bullets an amount of:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0 the Thread -0 complete a loading, the current number of cartridges is:. 1 the Thread -1 completion of one shot, to the current number of cartridges: 0