Back to project page androidcodes.
The source code is released under:
GNU General Public License
If you think the Android project androidcodes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.java.thread; //from w w w .ja v a 2 s. co m import java.util.Collection; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ConditionExample { private int count; private final Lock lock = new ReentrantLock(); private Condition isEmpty = lock.newCondition(); private Condition isFull = lock.newCondition(); public void isFull() throws InterruptedException { lock.lock(); try { while (count == 10) isFull.await(); count++; isFull.signalAll(); } finally { lock.unlock(); } } public void isEmpty() throws InterruptedException { System.out.println("inside is empty"); lock.lock(); try { while (count == 0) isEmpty.await(); count--; System.out.println("is empty " + count); isEmpty.signalAll(); } finally { lock.unlock(); } } public static void main(String[] args) { final ConditionExample ce = new ConditionExample(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { while (true) ce.isEmpty(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); Thread t2 = new Thread(new Runnable() { @Override public void run() { try { while (true) ce.isFull(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); t1.start(); t2.start(); } }