🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Java���̱߳�̣�fwd��

#24091
From: ��
Date: Sat, 20 Jun 1998 00:00
115 lines
7536 bytes
������: pipishi.bbs@bbs.nju.edu.cn (���ܵ�Сʨ), ����: Programming
��  ��: Java���߳�
����վ: nju_bbs (Thu May  7 21:26:29 1998)
ת��վ: lifebbs!cstshnews!ustcnews!nju_bbs

  Java�Ķ��߳����������һ���ŵ㡣���߳�����Ե��̶߳��Ե�,
���߳���ָ�κ�ʱ��ֻ����һ�����г���,����������ȴ�;������
���߳��������,Java����֧�ֶ�����򲢷�ִ�С�Java�Ķ��̹߳�
�ܱ�������߳����С�

    �̵߳Ĵ����͵���
    �����ַ������Դ����̡߳���һ�ַ�����ͨ��̳���"Thread"��
�����߳��ࡣ��:
    class aaa extends Thread{   //aaa��������
      public void run(){
      //run������߳����������,��C�����е�main����
      //Դ����
      }
    }
    ��ʱ���ø�ʽ����:
    aaa test=new aaa();    //test����aaa��һ��ʵ��
    test.start();
    //start���߳���ij�Ա����,������һ���µ��߳�,����߳���
������Run
    �ڶ��ַ����ǽ���һ������Runnable�ӿڵ��ࡣ��:
    class aaa implements Runnable{
      public void run(){
      //Դ����
      }
    }
    ��ʱ���ø�ʽ����:
    aaa test=new aaa();
    new Thread(test).start();
                        //ͨ��Thread����һ���µ��̡߳��߳���
�ı�������Ա����
                        ����:
    MAX_PRIORITY        �涨���߳����ȼ��������õ����ֵ
    MIN_PRIORITY        �涨���߳����ȼ��������õ���Сֵ
    NORM_PRIORITY       �涨�����ȼ���ȱʡֵ
    ��Ա����:
    acitveCount()       ���ظ��߳����е�ǰ�����̵߳���Ŀ
    checkAccess()       ��⵱ǰ�߳��Ƿ���Ա��޸�
    countStackFrames()  ���ظ��̶߳�ջ�����Ŀ
    currentThread()     ���ص�ǰ����ִ�е��̶߳���
    destroy()           ��ֹһ���߳�,����������������
    dumpStack()         һ�����Թ��,�����ǰ�̵߳Ķ�ջʹ��
���
    enumerate(Thread[]) �����и��߳����м�����̸߳��Ƶ�һ��
�����������ȥ
    getName()           �����̵߳����
    getPriority()       �����̵߳����ȼ�
    getTheradGroup()    �����߳���
    interrupt()         ��һ���̷߳���һ���ж���Ϣ
    interrrupted()      �����߳��Ƿ��ж�
    isAlive()           ����߳��Ƿ��ڼ���״̬
    isDaemon()          �����߳��Ƿ�פ�ڴ�
    isInterrupted()     �����һ���߳��Ƿ��ж�
    join(long)          ��long�����еȴ��߳���ֹ
    join(long,int)      ��long����,int�����ڵȴ��߳���ֹ
    join()              ��Զ�ȴ��̵߳���ֹ
    resume()            ���¿�ʼִ�и��߳�
    run()               �����������,��C�����е�main����
    setDaemon()         �����߳�����ΪDaemon(��פ�ڴ�)
    setName(String)     �����̵߳����
    setPriority(int)    �����̵߳����ȼ�
    sleep(long)         ʹһ���߳�����long����
    sleep(long,int)     ʹһ���߳�����long����,int����
    start()             ����һ���߳�,����߳̽��Զ�����run��
�������߳�
                        ��ʼִ�е�ͬʱ,����start���Ǹ��߳̽�
��������ִ��
                        ������
    stop()              ��ֹһ���߳�
    stop(Throwable)     ��ֹһ���߳�,�����߳�����Throwable��
�̳й�����
    suspend()           ��ͣ�̵߳�ִ��
    toString()          ����һ���ַ�,�����̵߳���ơ�����
�����߳���
    yield()             ʹ�̷߳����CPU�Ŀ���,ʹ�����ڵȴ�
״̬���߳�����
    ͬ������ȫ�趨
    ������������Ҫʹ��ͬһ����Դʱ,����ͬʱִ�С�Java�ṩ��
ͬ���趨�Ĺ��ܡ�ÿ������Լ������в������������ͬʱ���еIJ�
�ֽ���ͬ���趨��ͬ���趨�Ĺ�����֪ͨJaa ��Щ������ѱ�����,
��Щ����Ĵ����ֻ�ܵ������С������¼��ַ�������ͬ���趨: 
    1.����������Ϊͬ����ʽ
    public synchronized void fun1(){
     //Դ����
    }
    public synchronized void fun2(){
    //Դ����
    }
    ��������������Ϊͬ����ʽ�ĺ���,ֻ���ڻ����Դ��ſ�����
������ÿһ������˵,ֻ��һ����Դ���κ�ʱ�̶�ֻ��һ�������Դ
�ij���������,�����ڻ����Դǰ���ڵȴ�״̬��

    2.�ڵ��ú�����˵���˺�����Ҫ��ͬ��
    synchronized(res){ res.fun1();}
    synchronized(res){ res.fun2();}
    ������������,fun1��fun2ֻ�ܻ��res����Դ��������С�����
���ý����,��Դ�ű��ͷ�,�Ӷ�ʹ����ȴ������Դ���̵߳�������
��
    �߳��еı�����������Ϊ��ȫ����,���߳�ʹ���������ʱ,����
�̲߳��ܸı����������ֵ���������ʹ�ñ�����������ʵ�ִ�����
�������ʹ����������,���������ѽ�һ���߳����ѱ��޸ĵı���
��ֿ��������ְ�ȫ�����Ķ��巽ʽ����:
    threadsafe ine ttt;
    //threadsafe���̰߳�ȫ����������,ttt�DZ�����
    ���dz��õ�static����,�����̰߳�ȫ�����ĵ�������

--
  ��Ī��������룬��Ī��������֪ ��

�� ��Դ: �й�ƴ�BBSվ [bbs.ustc.edu.cn]
Technical Details
Message-ID: <980620.ynOor$PBBS@bbs.ustc.edu.cn>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB
X-Google-Thread: f6cb0,d4c43af8376084cf,start
X-Google-Attributes: gidf6cb0,public
From: Qin_Qin@bbs.ustc.edu.cn (��)
Subject: Java���̱߳�̣�fwd��
Date: 1998/06/20
Message-ID: <980620.ynOor$PBBS@bbs.ustc.edu.cn>
X-Deja-AN: 364533498
Organization: �й�ƴ�BBSվ
Newsgroups: cn.bbs.comp.lang.java