Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2008

Tip: Looking for answers? Try searching our database.

Java Thread Problem

Thread view: 
Sanny - 21 Mar 2008 06:41 GMT
I use a thread to call a function in a new thread.

Here is the code I use

private Thread runner2;

public void start2(){
 runner2 = new Thread(this);
 runner2.setPriority(Thread.MAX_PRIORITY);
 runner2.start();
}

and in run of thread

public void run(){

Thread thisThread = Thread.currentThread();

if (runner2==thisThread){

 runner2.setPriority(Thread.MAX_PRIORITY);

callfunction1();
.
.
.
.

start2();to start a new thread

// END THIS THREAD

runner2=null;
 try{
 runner2.destroy();
  }catch (NullPointerException e){
   //System.out.println("Bad URL: "+page);
 }
 }

I use start2() to start a new runner2 thread.

But my Question is When I call the function to start a new thread Will
the Original thread runner2 stopped or a new thread runner2 will be
created?

I want that runner2 thread first stop and then start a new thread
runner2. How to destroy a thread and start it again.

Once I call runner2.destroy, will that thread stops? And in case I
start a new thread runner2 will the old thread destroy both old & new
threads as both use same variable runner2?
Sanny - 21 Mar 2008 06:49 GMT
> Here is the code I use
>
[quoted text clipped - 5 lines]
>   runner2.start();
> }

When I call the function start2() a new thread "runner2" is started
will that end the previously running thread or there will be two
threads running at the same time.

If I call the function start2() 10  times will 10 threads will run
simultaniously with name "runner2"?

Bye
Sanny
Lew - 21 Mar 2008 12:30 GMT
>> Here is the code I use
>>
[quoted text clipped - 12 lines]
> If I call the function start2() 10  times will 10 threads will run
> simultaniously with name "runner2"?

I don't know about your code, but generally when you start /n/ threads and
they take long enough to run, /n/ threads will run simultaneously.

To answer the question specifically for your code would require knowledge of
the parts of your code that you've not deigned to share with us.

Signature

Lew

Logan Shaw - 22 Mar 2008 18:05 GMT
>>> Here is the code I use
>>>
[quoted text clipped - 19 lines]
> knowledge of the parts of your code that you've not deigned to share
> with us.

Why?  Isn't the question whether overwriting the reference in
the member variable will cause the other thread to stop?  I don't
see why we need any further information about the code to give
the general answer to this question.

The question can be reformulated:  if you overwrite a reference
to a Thread object, will that have any effect on the object or
stop the thread from running?  The answer is that it will not
do anything at all to the object, with the one exception that
it *may* make the object eligible for garbage collection (if it
was the last reference).

So now the question can be reformulated again:  what happens
when a Thread object is no longer reachable from the root set
and is eligible for garbage collection?  Does it keep running?

I honestly have never answered this question for myself since
I guess I've never (intentionally) written code where I don't
keep track of the threads in some way.  I'm fairly sure the GC
doesn't destroy a thread just because there are no references
to it.  In fact, the answer may be that the JVM doesn't allow
a Thread object to become unreachable in the first place.  I'm
starting to suspect that is the case, since every live thread
can always call Thread.currentThread() to get its own object,
which would mean the object must be considered reachable even
if no code has ever bothered to store a reference to it.

So I believe the answer is that, no, overwriting the reference
to the Thread object doesn't affect the running thread.  The
thread does not care that is running "with name 'runner2'".
The thread does not regard a member variable as its name; the
member variable has no special association with any of the
objects it may point to at various times.

  - Logan
Lew - 22 Mar 2008 18:52 GMT
> So now the question can be reformulated again:  what happens
> when a Thread object is no longer reachable from the root set
> and is eligible for garbage collection?  Does it keep running?

Yes, unless it's a daemon thread and the program has terminated.

Once a thread has terminated then its owning Thread instance can be collected.

> So I believe the answer is that, no, overwriting the reference
> to the Thread object doesn't affect the running thread.  The
> thread does not care that is running "with name 'runner2'".
> The thread does not regard a member variable as its name; the
> member variable has no special association with any of the
> objects it may point to at various times.

Correct.  For that matter, holding on to a Thread's name or any object to
which its member variables point does nothing to keep the Thread instance
itself from being collected.

Signature

Lew

Lew - 21 Mar 2008 12:43 GMT
> I use a thread to call a function in a new thread.
>
> Here is the code I use

But only fragments of it.  You need to provide an SSCCE.
<http://mindprod.com/jgloss/sscce.html>

>  private Thread runner2;
>
> public void start2(){
>   runner2 = new Thread(this);
>   runner2.setPriority(Thread.MAX_PRIORITY);

Do not muck with Thread priorities here.

>   runner2.start();
> }
[quoted text clipped - 4 lines]
>
> Thread thisThread = Thread.currentThread();

If you only use 'thisThread' once, for the comparison, you have no need for
the variable.  Just use the currentThread() expression directly.

> if (runner2==thisThread){

This check-and-set idiom will fail, absent some form of synchronization.

>   runner2.setPriority(Thread.MAX_PRIORITY);

Do not set Thread priorities until you know more about how threads work.

> callfunction1();

Since you're already seen to be calling a function, this isn't a great name
for a method.  Also, use camel case in the name.

> ..
<http://mindprod.com/jgloss/sscce.html>
> ..
<http://mindprod.com/jgloss/sscce.html>
> ..
> ..
>
> start2();to start a new thread

Is this supposed to be part of the code snippet, or part of the narrative?

> // END THIS THREAD
>
>  runner2=null;
>   try{
>   runner2.destroy();

Do you read the API docs?  You should.
<http://java.sun.com/javase/6/docs/api/java/lang/Thread.html#destroy()>
>> **Deprecated.** ... the method was never implemented.
>> If if were to be implemented, it would be deadlock-prone

Whoops.

>    }catch (NullPointerException e){
>     //System.out.println("Bad URL: "+page);

As shown, the code is guaranteed to throw this exception.  Well, nearly
guaranteed; concurrency anomalies might actually prevent the exception.

>   }
>   }
[quoted text clipped - 7 lines]
> I want that runner2 thread first stop and then start a new thread
> runner2. How to destroy a thread and start it again.

Why would you want to destroy the thread?  Why start it at all if you're
planning to destroy it?  I don't understand.

> Once I call runner2.destroy, will that thread stops? And in case I

What thread?  You have runner2 set to null, unless a concurrent action somehow
breaks through the memory barrier and changes it.

Even if runner2 referenced a Thread, the destroy() wouldn't stop it, because
"the method was never implemented"!

> start a new thread runner2 will the old thread destroy both old & new
> threads as both use same variable runner2?

There is a difference between the variable and the object to which it points.
 Actions come from objects, not variables.

Signature

Lew

Roedy Green - 21 Mar 2008 13:10 GMT
On Thu, 20 Mar 2008 22:41:37 -0700 (PDT), Sanny
<softtanks@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

>But my Question is When I call the function to start a new thread Will
>the Original thread runner2 stopped or a new thread runner2 will be
>created?

The whole point of threads is to get multiple threads going at once.
"start" spins off a separate thread that executes in parallel.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 21 Mar 2008 13:12 GMT
On Thu, 20 Mar 2008 22:41:37 -0700 (PDT), Sanny
<softtanks@hotmail.com> wrote, quoted or indirectly quoted someone who
said :

>I want that runner2 thread first stop and then start a new thread
>runner2.

That would be pointless. You might as well just use ordinary methods
if you don't want parallelism.

>How to destroy a thread and start it again.
You must start a new thread.  You can, however, put a thread to sleep
temporarily, or make it wait on some object.

see http://mindprod.com/jgloss/thread.html

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.