Article View: cn.bbs.comp.program
Article #1087Java���߳�
From: ���ܵ�Сʨ
Date: Thu, 07 May 1998 00:00
Date: Thu, 07 May 1998 00:00
114 lines
7664 bytes
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����,�����̰߳�ȫ�����ĵ�������
--
________ _____ _____ ______________ _____
___ __ \___(_)________ ___(_) __ ___/___ /_ ___(_)
__ /_/ /__ / ___ __ \__ / _____ \ __ __ \__ /
_ ____/ _ / __ /_/ /_ / ____/ / _ / / /_ /
/_/ /_/ _ .___/ /_/ /____/ /_/ /_/ /_/
/_/
[m[1;36m�� ��Դ:���ϴ�С�ٺ���Ϣ����վ bbs.nju.edu.cn��[FROM: hsia.nju.edu.cn][m
Technical Details
Message-ID:
<3OOYNa$tv8@bbs.nju.edu.cn>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB X-Google-Thread: fc86e,76f7f925fa538cce,start X-Google-Attributes: gidfc86e,public From: pipishi.bbs@bbs.nju.edu.cn (���ܵ�Сʨ) Subject: Java���߳� Date: 1998/05/07 Message-ID: <3OOYNa$tv8@bbs.nju.edu.cn> X-Deja-AN: 351560161 X-Filename: Programming/M.894547572.A Organization: nju_bbs Newsgroups: cn.bbs.comp.program