[Concurrent Programming in Java]CookieJar Problem
2011-03-31 16:23
323 查看
*Description:
* The Finite-Source Cookie Jar Problem. A cookie jar is being shared by two sisters,
* Tina and Judy, using the following rule: Judy can get a cookie from the jar only
* after Tina (being the older sister) gets a cookie in at least two separate
* occasions, whereas Tina gets a cookie from the jar whenever she wants to.
* The mother is alerted by Judy or Tina whenever they want to eat a cookie and
* the jar is empty, in which case the mother fills the jar up (from a hidden source),
* and the kids go on eating. Treating Tina, Judy and the mother as three independently
* executing processes and the Cookie Jar as a shared resource,
* develop a concurrent algorithm to solve the Finite-Source Cookie Jar Problem.
import java.util.Random;
/**
*-----------------------------------------------------------------------------
* @ Copyright(c) 2010~2011 All Rights Reserved.
*-----------------------------------------------------------------------------
* FILE NAME : CookieJar.java
* DESCRIPTION :
* SYSTEM NAME : cs490
* MODULE NAME : cs490
* LANGUAGE : Java
* DATE OF FIRST RELEASE :
*-----------------------------------------------------------------------------
* @ Created on 22 Marc, 2011
* @ Release 1.0.0.0
* @ Version 1.1
* -----------------------------------------------------------------------------------
* Date Author Version
* Description
* The Finite-Source Cookie Jar Problem. A cookie jar is being shared by two sisters,
* Tina and Judy, using the following rule: Judy can get a cookie from the jar only
* after Tina (being the older sister) gets a cookie in at least two separate
* occasions, whereas Tina gets a cookie from the jar whenever she wants to.
* The mother is alerted by Judy or Tina whenever they want to eat a cookie and
* the jar is empty, in which case the mother fills the jar up (from a hidden source),
* and the kids go on eating. Treating Tina, Judy and the mother as three independently
* executing processes and the Cookie Jar as a shared resource,
* develop a concurrent algorithm to solve the Finite-Source Cookie Jar Problem.
* -----------------------------------------------------------------------------------
* Mar 31, 2011 Haisheng Chen 1.1 Initial Create
* -----------------------------------------------------------------------------------
*/
//////////////////////////////////////////////////////////////////////////////////////
public class CookieJar
{
//fields
static int cookieCount = 25;
static Random random = new Random();
static int tinaCount = 0;//times for Tina's eating the cookie,communicate with Judy
// locks
static Object cookie = new Object();
static Object children = new Object();
static Object mother = new Object();
//methods
public static void randomWait()
{
int millisecs = random.nextInt(200);
try
{
Thread.sleep(millisecs);
}
catch (InterruptedException ex)
{
}
}
public static void main(String[] args)
{
new Thread(new Mother()).start();
new Thread(new Tina()).start();
new Thread(new Judy()).start();
}
//--inter classes definition
//////////////////////////////////////////////////////////////////////////////////
static class Tina implements Runnable
{
public void run()
{
while (true)
{
randomWait();
boolean empty = false;
synchronized (cookie)
{
if (cookieCount == 0)
{
empty = true;
synchronized (mother)
{
System.out
.println("Tina--:Oops! no cookie mother!!! Please help.");
mother.notify();
}
}
}
if (empty)
{
synchronized (children)
{
try
{
System.out
.println("Tina--:We are hungry mother. Where are other cookies? We are waiting.");
children.wait();//release the lock
}
catch (InterruptedException e)
{
}
}
}
synchronized (cookie)
{
cookieCount--;
tinaCount++;
cookie.notify();
System.out.println("Tina is eating cookie.");
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
static class Judy implements Runnable
{
public void run()
{
while (true)
{
randomWait();
boolean empty = false;
synchronized (cookie)
{
if (tinaCount < 2)
{
try
{
System.out
.println("Judy is now awake and again checking for tinaCount.");
cookie.wait();
}
catch (InterruptedException e)
{
}
}
if (cookieCount == 0)
{
empty = true;
synchronized (mother)
{
System.out
.println("Judy--:Oops! no cookie mother!!! Please help.");
mother.notify();
}
}
}
if (empty)
{
synchronized (children)
{
try
{
System.out
.println("Judy--:We are hungry mother. Where are other cookies? We are waiting.");
children.wait();//release the lock
}
catch (InterruptedException e)
{
}
}
}
synchronized (cookie)
{
cookieCount--;
tinaCount = 0;
//cookie.notify();
System.out.println("Judy has now reset the tinaCount.");
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
static class Mother implements Runnable
{
public void run()
{
while (true)
{
synchronized (mother)
{
System.out
.println("Mother is sleeping children. Wake me up when you finish your cookies.");
try
{
mother.wait();
}
catch (InterruptedException e)
{
}
}
synchronized (cookie)
{
cookieCount = 25;
System.out
.println("Mother has filled the cookie jar with good cookies.");
}
synchronized(children)
{
System.out
.println("Get the cookies dear! I've filled up the jar.");
children.notifyAll();
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
}
* The Finite-Source Cookie Jar Problem. A cookie jar is being shared by two sisters,
* Tina and Judy, using the following rule: Judy can get a cookie from the jar only
* after Tina (being the older sister) gets a cookie in at least two separate
* occasions, whereas Tina gets a cookie from the jar whenever she wants to.
* The mother is alerted by Judy or Tina whenever they want to eat a cookie and
* the jar is empty, in which case the mother fills the jar up (from a hidden source),
* and the kids go on eating. Treating Tina, Judy and the mother as three independently
* executing processes and the Cookie Jar as a shared resource,
* develop a concurrent algorithm to solve the Finite-Source Cookie Jar Problem.
import java.util.Random;
/**
*-----------------------------------------------------------------------------
* @ Copyright(c) 2010~2011 All Rights Reserved.
*-----------------------------------------------------------------------------
* FILE NAME : CookieJar.java
* DESCRIPTION :
* SYSTEM NAME : cs490
* MODULE NAME : cs490
* LANGUAGE : Java
* DATE OF FIRST RELEASE :
*-----------------------------------------------------------------------------
* @ Created on 22 Marc, 2011
* @ Release 1.0.0.0
* @ Version 1.1
* -----------------------------------------------------------------------------------
* Date Author Version
* Description
* The Finite-Source Cookie Jar Problem. A cookie jar is being shared by two sisters,
* Tina and Judy, using the following rule: Judy can get a cookie from the jar only
* after Tina (being the older sister) gets a cookie in at least two separate
* occasions, whereas Tina gets a cookie from the jar whenever she wants to.
* The mother is alerted by Judy or Tina whenever they want to eat a cookie and
* the jar is empty, in which case the mother fills the jar up (from a hidden source),
* and the kids go on eating. Treating Tina, Judy and the mother as three independently
* executing processes and the Cookie Jar as a shared resource,
* develop a concurrent algorithm to solve the Finite-Source Cookie Jar Problem.
* -----------------------------------------------------------------------------------
* Mar 31, 2011 Haisheng Chen 1.1 Initial Create
* -----------------------------------------------------------------------------------
*/
//////////////////////////////////////////////////////////////////////////////////////
public class CookieJar
{
//fields
static int cookieCount = 25;
static Random random = new Random();
static int tinaCount = 0;//times for Tina's eating the cookie,communicate with Judy
// locks
static Object cookie = new Object();
static Object children = new Object();
static Object mother = new Object();
//methods
public static void randomWait()
{
int millisecs = random.nextInt(200);
try
{
Thread.sleep(millisecs);
}
catch (InterruptedException ex)
{
}
}
public static void main(String[] args)
{
new Thread(new Mother()).start();
new Thread(new Tina()).start();
new Thread(new Judy()).start();
}
//--inter classes definition
//////////////////////////////////////////////////////////////////////////////////
static class Tina implements Runnable
{
public void run()
{
while (true)
{
randomWait();
boolean empty = false;
synchronized (cookie)
{
if (cookieCount == 0)
{
empty = true;
synchronized (mother)
{
System.out
.println("Tina--:Oops! no cookie mother!!! Please help.");
mother.notify();
}
}
}
if (empty)
{
synchronized (children)
{
try
{
System.out
.println("Tina--:We are hungry mother. Where are other cookies? We are waiting.");
children.wait();//release the lock
}
catch (InterruptedException e)
{
}
}
}
synchronized (cookie)
{
cookieCount--;
tinaCount++;
cookie.notify();
System.out.println("Tina is eating cookie.");
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
static class Judy implements Runnable
{
public void run()
{
while (true)
{
randomWait();
boolean empty = false;
synchronized (cookie)
{
if (tinaCount < 2)
{
try
{
System.out
.println("Judy is now awake and again checking for tinaCount.");
cookie.wait();
}
catch (InterruptedException e)
{
}
}
if (cookieCount == 0)
{
empty = true;
synchronized (mother)
{
System.out
.println("Judy--:Oops! no cookie mother!!! Please help.");
mother.notify();
}
}
}
if (empty)
{
synchronized (children)
{
try
{
System.out
.println("Judy--:We are hungry mother. Where are other cookies? We are waiting.");
children.wait();//release the lock
}
catch (InterruptedException e)
{
}
}
}
synchronized (cookie)
{
cookieCount--;
tinaCount = 0;
//cookie.notify();
System.out.println("Judy has now reset the tinaCount.");
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
static class Mother implements Runnable
{
public void run()
{
while (true)
{
synchronized (mother)
{
System.out
.println("Mother is sleeping children. Wake me up when you finish your cookies.");
try
{
mother.wait();
}
catch (InterruptedException e)
{
}
}
synchronized (cookie)
{
cookieCount = 25;
System.out
.println("Mother has filled the cookie jar with good cookies.");
}
synchronized(children)
{
System.out
.println("Get the cookies dear! I've filled up the jar.");
children.notifyAll();
}
}
}
}
//////////////////////////////////////////////////////////////////////////////////
}
相关文章推荐
- 《Concurrent Programming in Java: Design Principles and Patterns》Second Edition
- JAVA错误:AXIS Web Service Problem: No compiler found in your classpath! (you may need to add ‘tools.jar’)
- Concurrent Programming in Java(TM): Design Principles and Pattern
- Concurrent and Real-Time Programming in Java
- 说Hand-over-hand lock: 通读Concurrent Programming in Java: Design Principles and Patterns (1)
- 啃"创建线程"一章的疑难解析: 通读Concurrent Programming in Java: Design Principles and Patterns (2)
- Java programming problem in linux box: Exception in thread "main" java.lang.NoClassDefFoundError
- Concurrent Programming in Java - Single Threaded Execution
- [Java 多线程] Concurrent Programming in Java BOOK
- 译 Programming with typesafe enums and annotations in Java 5
- think in java笔记:java.util.concurrent.Semaphore
- java命令执行jar包(里面的main函数,无web.xml文件)的方式(包括依赖其它的jar包),使用Google-Guava Concurrent包里的Service框架,maven工程
- Generics in the Java Programming Language Java
- parallel programming in java introduce
- java.lang.IllegalArgumentException: Control character in cookie value or attribute.
- java.lang.IllegalArgumentException: Control character in cookie value or attribute
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
- The absolute uri: http://java.sun.com/jsp/jstl/fmt cannot be resolved in either web.xml or the jar
- Crafts Store ---- a concurrent simulation in Java
- maven 报Unable to locate the Javac Compiler in: D:\Program Files\Java\jdk1.6.0_20\..\lib\tools.jar