public class Buffer { int value; boolean flag = false; public synchronized int getValue() { if(!flag) { try{ wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } } System.out.println(" get_value "+value); flag = false; notify(); return value; } public synchronized void setValue(int value) { if(flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } } System.out.println("set_value "+value); this.value = value; flag = true; notify(); } }
Thread control
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325543968&siteId=291194637
Recommended
Ranking