[Concurrent Programming in Java]CookieJar Problem

本文通过一个具体的并发编程案例——有限来源的饼干罐问题,详细介绍了如何使用Java语言实现一个多线程解决方案。该方案涉及三个独立运行的过程:姐姐Tina、妹妹Judy以及母亲,他们共同使用一个共享资源——饼干罐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

                }

            }

        }

    }

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

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值