🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.lang.java
Article #277

�����̵߳�����

#277
From: ��ɵ���
Date: Tue, 24 Feb 2009 06:26
82 lines
2802 bytes
class ham                                       //��װ������ĺ�����Ϊ��������
{
    static Object box=new Object();
    static int totalmaterial=10;
    static int sales=0;
    static int production=5;
}
class hmaker extends Thread                      //��ʦ�߳���
{
    public void make()
    {
        synchronized(ham.box)
        {
            (ham.production)++;
            try
            {
                ham.box.notify();
            }
            catch(Exception e){}
        } 
    }
    public void run()
    {
        while(ham.production<ham.totalmaterial)
        {      
            if(ham.production>0) 
            {
                System.out.println("��ʦ"+getName()+":"+"��������(�ܹ�"+(ham.production-ham.sales)+"��)");
            } 
            try 
            { 
                sleep(3000);
            } 
            catch(Exception e){}
            make(); 
        } 
     } 
}
class hassistant extends Thread 
{
    public void sell()
    {
        if(ham.production==0)
        {
            System.out.println("ӪҵԱ���˿������ǣ�����΢��һ��,����û��!!");
        }
        try 
        { 
            ham.box.wait();
        } 
        catch(Exception e){} 
        ham.sales++; 
        System.out.println("ӪҵԱ���˿ͺã����������ˣ����ܹ�����"+ham.sales+"����"); 
    }
    public void run()
    { 
        while(ham.sales<ham.production)
        { 
            try 
            { 
                sleep(1000); 
            } 
            catch(Exception e){} 
            sell(); 
        } 
    } 
} 
public class thread13 
{ 
    public static void main(String[] args) 
    { 
        hmaker maker=new hmaker(); 
        hassistant assistant=new hassistant(); 
        maker.setName("��"); 
        maker.start(); 
        assistant.start(); 
    }
}


--
�� ��Դ:�������ʵBBS http://bbs.tju.edu.cn [FROM: 211.81.57.181]
Technical Details
Message-ID: <4eZER0$ASN@bbs.tju.edu.cn>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!newshub.sdsu.edu!newsgate.cuhk.edu.hk!news.neu.edu.cn!news.byr.edu.cn!TJU

Show Full Headers
Complete Article Headers
X-Google-Thread: f6cb0,c464406d397dfa4a,start
X-Google-Attributes: gidf6cb0,domainid0,public,usenet
X-Google-Language: ENGLISH,GB
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!newshub.sdsu.edu!newsgate.cuhk.edu.hk!news.neu.edu.cn!news.byr.edu.cn!TJU
From: lishoukai.bbs@bbs.tju.edu.cn.no.spam (��ɵ���)
Newsgroups: cn.bbs.comp.lang.java
Subject: �����̵߳�����
Date: 24 Feb 2009 06:26:24 GMT
Organization: �����ʵBBSվ
Message-ID: <4eZER0$ASN@bbs.tju.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: Java/M.1235456784.A