您的位置:首页 > 编程语言 > Java开发

[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();

}

}

}

}

//////////////////////////////////////////////////////////////////////////////////

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐