🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #19024

Re: thread and variables

#19024
From: Conrad Hoesle-Kienzlen
Date: Mon, 16 Mar 1998 00:00
83 lines
2690 bytes
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: <350D3CDC.8680E7D0@csi.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <6ej7rq$nmb1@forums.borland.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: Conrad Hoesle-Kienzlen <Conrad_Hoesle@csi.com>
Subject: Re: thread and variables
Date: 1998/03/16
Message-ID: <350D3CDC.8680E7D0@csi.com>
X-Deja-AN: 334574649
Content-Transfer-Encoding: 7bit
References: <6ej7rq$nmb1@forums.borland.com>
Content-Type: text/plain; charset=us-ascii
Organization: CHK IT-Consulting
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language