📄 bettersharedobject.java
字号:
public class BetterSharedObject extends SharedObject{ // The private shared data variable private Integer sharedData = null; private boolean dataAvailable = false; // Default Constructor public BetterSharedObject( int initialValue ) { super( initialValue ); // Initialize the shared object value to something sharedData = new Integer( initialValue ); } // The produced will call this method to update the data public synchronized void setSharedData( int newData ) { if ( dataAvailable ) { try { wait(); } catch( InterruptedException ex ) {} } System.out.println( "Shared Object - New Value Set: " + newData ); sharedData = new Integer( newData ); System.out.println( "Send notification that its ok to read new data" ); dataAvailable = true; notifyAll(); } // The consumer will call this method to get the new data public synchronized Integer getSharedData() { if ( !dataAvailable ) { try { wait(); } catch( InterruptedException ex ) { System.out.println( "Got notification that it was ok to read new data" ); } } dataAvailable = false; notifyAll(); return sharedData; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -