Article View: cn.bbs.comp.lang.java
Article #24592Re: chat room
From: SqCW97
Date: Wed, 07 Jan 1998 00:00
Date: Wed, 07 Jan 1998 00:00
94 lines
4919 bytes
4919 bytes
�� �� jwolf (����˭) �Ĵ������ᵽ: ��
: ����Ĺؼ����ڣ����������ɱ����һ���̣߳�������stop()
: ����ɱ������?����Ҫ���������?��������Ķ�����ʲôʱ����
: �ŵģ��Ҳ���һ�£�Thread.destroy()ֻ���׳�һ��
: NOSUCHMETHOD�쳣����û�����κδ��?
: ���ڵij����У�����ÿһ��Ҫɱ�����߳��ǵ�����stop()����
: �ģ������ڸ��߳��ڲ����õģ�Ҳ����˵�Լ����Լ�ɱ��(���stop()
: ����ɱ���Ļ���������wait(),notify()���ƿ���ʵ���ڸ��߳��ⲿɱ��
: �̣߳�����һ��ר��ɱ���̵߳��̣߳�ƽ������wait(),һ�����߳���Ҫ
: ��ɱ����notify()��
: ����������û�������ֻ��ƣ���Ϊ���������߳��Լ����Լ�ɱ
: ���Dz��������⡣
:
Java Tutorial����˵�����̵߳�����������run()����ֹ�����Ա���
��run()�����ú�run()������ֹ��������Tutorial�и����������:
<pre>
public void run() {
Thread myThread = Thread.currentThread();
while (clockThread == myThread) {
do-some-thing();
try {
Thread.sleep(1000);
} catch (InterruptedException e){}
}
}
</pre>
���жϵ�ǰ�߳��Ƿ�������ִ�е��̣߳���û�и㶮����λ��
����һ�£����ɴ��Ұ�ԭ��COPY�����
<pre>
Stopping a Thread
A program doesn't stop a thread like it stops an applet (by
calling a method). Rather, a thread arranges for its own death by
having a run method that terminates naturally. For example, the
while loop in this run method is a finite loop-- it will iterate
100 times and then exit:
public void run() {
int i = 0;
while (i < 100) {
i++;
System.out.println("i = " + i);
}
}
A thread with this run method dies naturally when the loop
completes and the run method exits.
Let's look at how the Clock applet thread arranges for its own
death. You might want to use this technique with your applets.
Recall Clock's run method:
public void run() {
Thead myThread = Thread.currentThread();
while (clockThread == myThread) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e){
// the VM doesn't want us to sleep anymore,
// so get back to work
}
}
}
The exit condition for this run method is the exit condition for
the while loop because there is no code after the while loop:
while (clockThread == myThread) {
This condition indicates that the loop will exit when the
currently exiting thread is not equal to clockThread. When would
this ever be the case?
When you leave the page, the application in which the applet is
running calls the applet's stop method. This method then sets the
clockThread to null, thereby telling the main loop in the run method
to terminate:
public void stop() { // applets' stop method
clockThread = null;
}
If you revisit the page, the start method is called again and the
clock starts up again with a new thread. Even if you stop and
start the applet faster than one iteration of the loop, clockThread
will be a different thread than myThread and the loop will still
terminate.
</pre>
--
[1;36m ��Դ: ����������BBSվ��bbs.nease.net�E[FROM: www-post@bbs.nease.n]
Technical Details
Message-ID:
<3MQTkJ$78Y@bbs.nease.net>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB X-Google-Thread: f6cb0,46b4f2a2f397d037 X-Google-Attributes: gidf6cb0,public From: dayou.bbs@bbs.nease.net (SqCW97) Subject: Re: chat room Date: 1998/01/07 Message-ID: <3MQTkJ$78Y@bbs.nease.net> X-Deja-AN: 313490212 X-Filename: Java/M.884141491.A Organization: Net Ease Newsgroups: cn.bbs.comp.lang.java,