Article View: borland.public.jbuilder.java.language
Article #19034Re: thread and variables
From: Greg Dickson
Date: Tue, 17 Mar 1998 00:00
Date: Tue, 17 Mar 1998 00:00
114 lines
3351 bytes
3351 bytes
Thanks Conrad
the extends is definitely cleaner I'll swap mine over
I supose the reason for the implements is if you have to extend
something else.
I guess it, is, fairly obvious that as the scope of the tc variable
in my example is local then a new instance is made on each call to the
method
Greg
example is only local that a new instance of the variable
Conrad Hoesle-Kienzlen wrote in message <350D3CDC.8680E7D0@csi.com>...
>Greg,
>I have been playing around with threads as well and I did an applet
that
>starts several dialog boxes, each in it's own thread, that count up
to a
>certain number independantly. This works fine, each thread is
completely
>independant of the other, each get's it's own name, each has a
separate
>set of objects and so on. I stopped after 15 threads because it was
too
>much typing :-)
>You may consider not to implement runnable but to extend Thread. If
you
>implement runnable you have to create a thread object inside the
class and
>converse with that object from outside the class. That needs
knowledge of
>how you called the thread within that class. Whereas if you extend
Thread,
>you create the new thread object directly and therefore can converse
with
>that object directly. To make things clear, consider these code
snippets:
>
>The thread class:
>
>class MyThread implements runnable {
> Thread th;
>
> public MyThread() {
> th = new Thread(this, "SomeThread");
> th.start();
> }
> public void run() {
> ....
> }
>}
>
>You would then use this in the calling class like this:
>
> MyThread thread1 = new MyThread();
> ...
> thread1.th.sleep(1000); // you have to know that thread1 contains
>object <th>!!!
>
>Whereas if you extend Thread, your thread class looks like this:
>
>class MyThread extends Thread {
>
> public MyThread() {
> super("SomeThread");
> start();
> }
> public void run() {
> ....
> }
>}
>
>And you can use it like this:
>
> MyThread thread1 = new MyThread();
> ...
> thread.sleep(1000) // you don't have to know anything about the
>implementation
>
>...Conrad
>
>
>Greg Dickson wrote:
>
>> Hi all
>> I am playing withthreads and I was wondering if anyone knows what
>> happens here
>>
>> if I start a thread from a method with say
>>
>> private void doCommand (String doThis) {
>>
>> RunCommand tc = new RunCommand(doThis);
>>
>> }
>>
>> the RunCommand implements runnable and the constructor also starts
the
>> thread
>>
>> What happens if the method is called several times
>>
>> is tc given a new handle each time the method is called or is this
>> going to go nuts .
>>
>> Thanks
>> Greg Dickson
>
>
>
>
>
Technical Details
Message-ID:
<6ek98v$omq6@forums.borland.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<6ej7rq$nmb1@forums.borland.com> <350D3CDC.8680E7D0@csi.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,ddaf1f759f6f1855 X-Google-Attributes: gidf74b3,public From: "Greg Dickson" <witchy@netserv.net.au> Subject: Re: thread and variables Date: 1998/03/17 Message-ID: <6ek98v$omq6@forums.borland.com> X-Deja-AN: 334689815 Content-Transfer-Encoding: 7bit References: <6ej7rq$nmb1@forums.borland.com> <350D3CDC.8680E7D0@csi.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 Organization: Another Netscape Collabra Server User Newsgroups: borland.public.jbuilder.java.language