user1947236
user1947236

Reputation: 673

Little confused about multi-threading concepts

I have a few questions about Java multi-threading. I am currently learning different methods of multi-threading. My first question is, what happens to the thread after the code in it is done running? Do I need to Stop/Kill the thread? I am currently making a class for each thread and implementing Runnable in each class. I then start the thread in the main class using new ThreadClass();. In the constructor of the Thread class, I have it set to make a Thread named "second." If I add new ThreadClass() twice in the main method, are both threads named "second"? Thanks.

Upvotes: 2

Views: 108

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533880

My first question is, what happens to the thread after the code in it is done running? Do I need to Stop/Kill the thread?

The thread stops when it has nothing to do. If you have an ExecutorService, you have to use shutdown when you have finished with it.

If I add new ThreadClass() twice in the main method, are both threads named "second"?

You are making the code the same. This doesn't mean the name of the thread has to be the same (and vice-versa)

Upvotes: 3

Steve
Steve

Reputation: 7271

I assume you mean Thread rather than ThreadClass.

When the run method of a thread returns then the thread will stop. If you only specify the name in the second thread then only that thread will have the name "second". The first thread is unaffected.

You should avoid calling stop if at all possible as it does not allow the thread to exit cleanly.

Upvotes: 0

Related Questions