Java Thread Problem

S

Sanny

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?
 
S

Sanny

Here is the code I use
 private Thread runner2;

public void start2(){
  runner2 = new Thread(this);
  runner2.setPriority(Thread.MAX_PRIORITY);
  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
 
L

Lew

Sanny said:
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"?

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.
 
L

Lew

Sanny said:
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.
private Thread runner2;

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

Do not muck with Thread priorities here.
runner2.start();
}

and in run of thread

public void run(){

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.
..
..

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.
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.
}
}

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.

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.
 
R

Roedy Green

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.
 
L

Logan Shaw

Lew said:
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.

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
 
L

Lew

Logan said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top