Article View: cn.bbs.comp.lang.java
Article #24594Re: chat room
From: ����˭
Date: Wed, 07 Jan 1998 00:00
Date: Wed, 07 Jan 1998 00:00
108 lines
6704 bytes
6704 bytes
�������˵�ú��������run�����е�ѭ�����е�������:
clockthread==mythread(Ҳ���ǵ�ǰ���߳�),����stop()������
ʹclockthread=null��Ҳ����˵����ˢ�»��뿪���ҳ��ʱ��
Browser����stop()������ʹclockthread=null,�Ӷ�run�е�ѭ��
����Ӷ�run()������߳̽���
�������û�лش�ղŵ����⣬Ҳ���ǣ�stop()������
�߳�ֹͣ��û�и����ã���ijЩ�ڴ�û�����?�߳�������Ķ�
����������ͷţ�destroy()����������ʲô�á��̵߳Ľ���
һ����ζ�������õ��ڴ��ͷų����ˡ�
�ȷ�˵��
class SubThread extends Thread{
Object object1;
...
public void Run(){
...
}
...
}
����һ��Thread �����࣬�������н��������ռ���ڴ�
�ͷų����������е�Object1��������̵߳�ֹͣ���ڴ��ͷ�
Ӧ���������°�?�Ҿ���java���ڵ�û�б���ָ��һ������ʱ,����
���ͷ����������ռ���ڴ档
�� �� dayou (SqCW97) �Ĵ������ᵽ: ��
: �� �� 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>
--
_________________________
���˲�����ͻ,���Ʋ�����̣
[m[1;35m�� ��Դ:���������� BBS bbs.nease.net��[FROM: 202.96.161.212][m
Technical Details
Message-ID:
<3MQVZV$7Wa@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: jwolf.bbs@bbs.nease.net (����˭) Subject: Re: chat room Date: 1998/01/07 Message-ID: <3MQVZV$7Wa@bbs.nease.net> X-Deja-AN: 313513893 X-Filename: Java/M.884145583.A Organization: Net Ease Newsgroups: cn.bbs.comp.lang.java,