-2

Edit: I want to make a user input stop a thread, then another user input will start the thread.

I have a noob java question, I'm new to Threads and i want to know if when i execute Thread.currentThread().join();, and rerun the same thread again, will it overwrite the Thread that i just joined or will it run a new thread? I hope my question is clear.

matt217
  • 15
  • 7
  • 3
    No. Not so clear at all. What do you expect when joining (=waiting the thread to die) the current thread? And how do you "rerun the same thread" again? You should probably post some code, try it out and ask a more specific question. – Frito Oct 23 '18 at 08:56
  • I just want to stop the thread manually and Thread.stop or Thread.destroy was said to be a bad idea. I want to know if Thread.join can be used as a thread stopper – matt217 Oct 23 '18 at 09:05
  • have a look here on how to terminate a thread: https://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java – kerner1000 Oct 23 '18 at 09:08
  • If you want to know, if Thread.join can be used as a thread stopper, why not asking? – Frito Oct 23 '18 at 09:08
  • 1
    [javadoc](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html#join()) states "Waits for this thread to die." - in your case the **current** thread will wait for the **current** thread to terminate, that is, for ever - do not join with the current Thread! You can't "rerun" a thread: "It is never legal to start a thread more than once.", you must create a new one – user85421 Oct 23 '18 at 09:37
  • @matt217 You should start with your requirements, not on detailed questions on not reasonable solution attempts. – Frito Oct 23 '18 at 09:59
  • @CarlosHeuberger Ok so my i joined the current thread and ran a new one, but i want to stop the current thread instead of having it on wait. Doing this will only create a lot of threads on hold. – matt217 Oct 23 '18 at 10:08
  • @Frito I just want a user input to make a thread stop. And it was my idea to do the thread.join then run another thread again on another user input. – matt217 Oct 23 '18 at 10:09
  • @matt217 That's it: googling for "java make a thread stop" shows the question just answered on stackoverflow on top. – Frito Oct 23 '18 at 10:59
  • I think you must be a bit more specific. You can't join the actual running Thread, that makes no sense: pause the actual thread until it is finished - if it is waiting it will not finish normally. You can use a flag (or `interrupt()`) to signal a running (different) Thread to stop and, to wait it stop, call **its** `join()` method. – user85421 Oct 23 '18 at 11:44
  • The javadoc for `Thread.stop()` states why not use it and how to stop a thread: [Why are Thread.stop, Thread.suspend and Thread.resume Deprecated?](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/doc-files/threadPrimitiveDeprecation.html) – user85421 Oct 23 '18 at 11:49

2 Answers2

0

From the JavDoc:

Waits for this thread to die. This implementation uses a loop of this.wait calls conditioned on this.isAlive. As a thread terminates the this.notifyAll method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.

Thread t1 = new Thread(new MyRunnable(), "t1");
t1.start();
// wait here for t1 to terminate.
t1.join();
// alternatively, you can provide a timeout in ms.
t1.join(1000);

The calling thread (assume it's the main thread) will wait for another thread, t1, to terminate.

The term joining might be misleading, there is no joining in that sense of two threads, also no overriding.

If you are trying to terminate a thread, have a look here (in a nutshell, don't try to kill a thread but make it finish it's job, it will terminate naturally after that)

kerner1000
  • 3,382
  • 1
  • 37
  • 57
  • Thanks for the code, what if i after t1.join(); another execution of t1.start() will be executed? Im not sure if starting that thread again will add new threads – matt217 Oct 23 '18 at 09:09
  • Create another thread using the same runnable if you want to execute the code again. – kerner1000 Oct 23 '18 at 09:10
  • @matt217 you may never start a thread twice. The JavaDoc for Thread.start (https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#start()) explicitly states: "It is never legal to start a thread more than once" – Thomas Kläger Oct 23 '18 at 09:33
  • Is there a way to make a thread stop manually? Not making it wait but to literally destroy it? I want to stop that thread on user input – matt217 Oct 23 '18 at 10:10
0

Just to be really clear: t.join() does not do anything to thread t.

The only thing it does is wait for thread t to terminate.


when i execute Thread.currentThread().join();, and rerun the same thread again, will it overwrite the Thread that i just joined or will it run a new thread?

A Thread instance may only be start()ed one time. You must create a new Thread instance each time you want to start a new thread...


...But if you think you want your program to continually create new threads, then you probably should read about thread pools.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57