🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: cn.bbs.comp.program
1 messages
1 total messages Started by ���ܵ�Сʨ Thu, 07 May 1998 00:00
Java���߳�
#1087
Author: ���ܵ�Сʨ
Date: Thu, 07 May 1998 00:00
114 lines
7664 bytes
  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.nju.edu.cn��[FROM: hsia.nju.edu.cn]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads