标签:并发
class Car { private boolean waxOn = false; public synchronized void waxed() { waxOn = true; notifyAll( ); } public synchronized void buffed( ) { waxOn = false; notifyAll( ); } public synchronized void waitForWaxing( ) throws InterruptedException{ while(waxOn == false) wait( ); } public synchronized void waitForBuffing( ) throws InterruptedException { while(waxOn == true) wait( ); } } class WaxOn implements Runnable { private Car car; public WaxOn(Car c) { car = c;} public void run() { try { while(!Thread.interrupted()) { System.out.print(" Wax on!"); TimeUnit.MILLISECONDS.sleep(200); car.waxed(); car.waitForBuffing(); } } catch (InterruptedException e) { System.out.println("Exiting via interrupt"); } System.out.println("Ending Wax On task"); } } class WaxOff implements Runnable { private Car car; public WaxOff(Car c) {car = c;} public void run( ) { try { while(!Thread.interrupted()) { car.waitForWaxing(); System.out.print("Wax Off"); TimeUnit.MILLISECONDS.sleep(200); car.buffed(); } } catch(InterruptedException e) { System.out.println("Exiting via interrupt"); } System.out.println("Ending Wax Off task"); } } public class WaxOMatic { public static void main(String[] args) throws Exception{ Car car = new Car(); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(new WaxOff(car)); exec.execute(new WaxOn(car)); TimeUnit.SECONDS.sleep(5); exec.shutdownNow(); } }
class Meal { } class WaitPerson implements Runnable { private String name; private Restaurant restaurant; public WaitPerson(String name, Restaurant res) { this.name = name; this.restaurant = res; } @Override public void run() { try { while (!Thread.interrupted()) { synchronized (restaurant.waitPersons) { while (restaurant.meals.size() < 1) { restaurant.waitPersons.wait(); } } synchronized (restaurant.chefs) { if (restaurant.meals.size() >= 1) { restaurant.meals.poll(); restaurant.chefs.notifyAll(); System.out.println(name + " consumed a meal !"); } } } } catch (InterruptedException e) { System.out.println(name + " is ended via InterruptedException !"); return; } System.out.println(name + " is ended via InterruptedException !"); } } class Chef implements Runnable { private String name; private Restaurant restaurant; public Chef(String name, Restaurant res) { this.name = name; this.restaurant = res; } @Override public void run() { try { while (!Thread.interrupted()) { synchronized (restaurant.chefs) { while (restaurant.meals.size() > 10) { restaurant.chefs.wait(); } } synchronized (restaurant.waitPersons) { if (restaurant.meals.size() <= 10) { restaurant.meals.add(new Meal()); restaurant.waitPersons.notifyAll(); System.out.println(name + " produced a meal !"); } } } } catch (InterruptedException e) { System.out.println(name + " is ended via InterruptedException !"); return; } System.out.println(name + " is ended via InterruptedException !"); } } public class Restaurant { public Queue<Meal> meals = new ConcurrentLinkedQueue<Meal>(); public List<WaitPerson> waitPersons = new ArrayList<WaitPerson>(); public List<Chef> chefs = new ArrayList<Chef>(); public static void main(String[] args) throws InterruptedException { Restaurant res = new Restaurant(); ExecutorService exec = Executors.newCachedThreadPool(); Chef chef1 = new Chef("chef1", res); Chef chef2 = new Chef("chef2", res); res.chefs.add(chef1); res.chefs.add(chef2); exec.execute(chef1); exec.execute(chef2); WaitPerson waitPerson1 = new WaitPerson("waitPerson1", res); WaitPerson waitPerson2 = new WaitPerson("waitPerson2", res); res.waitPersons.add(waitPerson1); res.waitPersons.add(waitPerson2); exec.execute(waitPerson1); exec.execute(waitPerson2); // TimeUnit.MILLISECONDS.sleep(3000); // exec.shutdownNow(); } }
class Car { private boolean waxOn = false; private Lock lock = new ReentrantLock(); private Condition condition = lock.newCondition(); public void waxed() { lock.lock(); try { waxOn = true; condition.signalAll(); } finally { lock.unlock(); } } public void buffed( ) { lock.lock(); try { waxOn = false; condition.signalAll(); } finally { lock.unlock(); } } public void waitForWaxing( ) throws InterruptedException{ lock.lock(); try{ while(waxOn == false) condition.await(); } finally { lock.unlock(); } } public void waitForBuffing( ) throws InterruptedException { lock.lock(); try { while(waxOn == true) condition.await( ); } finally { lock.unlock(); } } }
class Meal { } class WaitPerson implements Runnable { private String name; private RestaurantBlookingQueue restaurant; public WaitPerson(String name, RestaurantBlookingQueue res) { this.name = name; this.restaurant = res; } @Override public void run() { try { while (!Thread.interrupted()) { restaurant.meals.take(); System.out.println(name + "taked a Meal"); Thread.sleep(100); } } catch (InterruptedException e) { System.out.println(name + " is ended via InterruptedException !"); return; } System.out.println(name + " is ended via InterruptedException !"); } } class Chef implements Runnable { private String name; private RestaurantBlookingQueue restaurant; public Chef(String name, RestaurantBlookingQueue res) { this.name = name; this.restaurant = res; } @Override public void run() { try { while (!Thread.interrupted()) { restaurant.meals.put(new Meal()); System.out.println(this.name + "made a meal"); Thread.sleep(100); } } catch (InterruptedException e) { System.out.println(name + " is ended via InterruptedException !"); return; } System.out.println(name + " is ended via InterruptedException !"); } } public class RestaurantBlookingQueue { public BlockingQueue<Meal> meals = new ArrayBlockingQueue<Meal>(10); public List<WaitPerson> waitPersons = new ArrayList<WaitPerson>(); public List<Chef> chefs = new ArrayList<Chef>(); public static void main(String[] args) throws InterruptedException { RestaurantBlookingQueue res = new RestaurantBlookingQueue(); ExecutorService exec = Executors.newCachedThreadPool(); Chef chef1 = new Chef("chef1", res); Chef chef2 = new Chef("chef2", res); res.chefs.add(chef1); res.chefs.add(chef2); exec.execute(chef1); exec.execute(chef2); WaitPerson waitPerson1 = new WaitPerson("waitPerson1", res); WaitPerson waitPerson2 = new WaitPerson("waitPerson2", res); res.waitPersons.add(waitPerson1); res.waitPersons.add(waitPerson2); exec.execute(waitPerson1); exec.execute(waitPerson2); // TimeUnit.MILLISECONDS.sleep(3000); // exec.shutdownNow(); } }
class Sender implements Runnable { private Random rand = new Random(47); private PipedWriter out = new PipedWriter(); public PipedWriter getPipedWriter( ) {return out;} public void run( ) { try { while(true) { for(char c = 'A' ; c <= 'z'; c++) { out.write(c); TimeUnit.MILLISECONDS.sleep( rand.nextInt(500)); } } } catch (IOException e) { System.out.println(e + " Sender write exception"); } catch (InterruptedException e) { System.out.println(e + " Sender sleep exception"); } } } class Receiver implements Runnable { private PipedReader in; public Receiver(Sender sender) throws IOException { in = new PipedReader(sender.getPipedWriter()); } public void run( ) { try { while(true) { System.out.print("Read: "+(char)in.read() + ", "); } } catch (IOException e) { System.out.println(e + " Receiver read exception"); } } } public class PipedIO { public static void main(String []args) throws Exception { Sender sender = new Sender( ); Receiver receiver = new Receiver( sender ); ExecutorService exec = Executors.newCachedThreadPool(); exec.execute(sender); exec.execute(receiver); TimeUnit.SECONDS.sleep( 4 ); exec.shutdownNow(); } }
标签:并发
原文地址:http://blog.csdn.net/troy__/article/details/40748003