Why After a thread is started, it can never be started again in java

I know that "Once a thread is started, it can never be started again."
But I want to know why?
What is wrong if you are allowed to start again later at another time?
Why, the only time you can start a stream when it is in the NEW state? Why can't it also be after DEAD at least?

public class ThreadDemo { public static void main(String[] args) { Thread thread = new Thread(new MyRunnable()); thread.start(); thread.start(); // java.lang.IllegalThreadStateException } } class MyRunnable implements Runnable{ @Override public void run() { System.out.println("run().Thread.currentThread().getName() : " + Thread.currentThread().getName()); } } 

Note. I read these posts . But my question is much more specific and descriptive.

Note here that I want to know this mainly to understand the internal functions of threads and how related aspects such as GC work with thread states.

+5
source share
3 answers

Because the Thread implementation does not allow. You can always create another Thread instance using Runnable , for example

 new Thread(new MyRunnable()).start(); new Thread(new MyRunnable()).start(); 

Edit

JLS-17.4.3. Programs and Program Order (partially),

The set of actions is sequentially coordinated if all actions are performed in full order (execution order), which is consistent with the order of the program, and, in addition, each read r of the variable v sees the value written in the record w on v such that:

 w comes before r in the execution order, and there is no other write w' such that w comes before w' and w' comes before r in the execution order. 

Consistent consistency is a very strong guarantee that it will be displayed and streamlined during program execution. As part of a sequential sequential execution, there is a complete order over all individual actions (such as reading and writing) that are consistent with the order of the program, and each individual action is atomic and immediately visible to each stream.

If Thread instances can start again, implementing consistent consistency may not be possible.

+5
source

I can give you some points.

  • Garbage Collection: Active topics are considered the roots of the garbage collection. So, if any object is accessible from the active thread, then it cannot be collected in garbage.
    Now, if the stream is inactive (dead), any gc algorithm will determine that if a particular object, accessible only for this stream, now has the right to garbage collection, then it will collect this object. But now, if you make your thread active again, this is a problem because it will have a distorted view of its stack, because many objects can be garbage collected. Your whole program will work.

    To work around this problem, you save another state that is called by GC_COLLECTABLE. This means that the stream is active, means that its status! = GC_COLLECTABLE. Now the question arises as an announcement when the programmer sets this status. Its kind of the same problem where it is impossible to stop the flow or determine when certain objects should collect garbage. This in itself is a very difficult task to solve. The easiest way to solve this problem is to not activate the thread when it is dead, or cannot restart it.

  • thread.join (): As suggested by Eliott. Suppose there are three threads T1 T2 and a restartable thread RST. Now suppose that T1 and T2 are called RST.join (). The problem is when T1 and T2 exit the connection. If threads can be started again, then the join method is infinite. If you allow joining the return after the DEAD state, then there is a race and undefined behavior causing T1 to end join (), but T2 is still delayed because RST was already running.

  • API change:. As you suggest, this should be implemented.

    Thread.start (); // Will this method suffice. Probably not

    What happens if I call Thread.start () in a loop -> will it create new threads if previous threads are not dead or restart dead threads ( Sounds like Executors.newCachedThreadPools )

    -> either throw Exceptions if the current thread is not dead or restart the thread Not a good way: make your program more non-deterministic . Since the same thread can take variable time for the same task, therefore, you will have a problem with predicting the output of your program. And at least you need to change the signature for the checked exception from the start method or define a new restart of the method, but it will have the same problem as above.

From my point of view, the first point is enough to go from restarting the stream. You can also have similar functionality with ThreadPools, where the thread can be reused for different / similar tasks.

+3
source

If you look at the life cycle of a thread, it is created and then moved to the thread pool, where it awaits execution

Once his work has been completed, it is destroyed by the scheduler, and therefore the system no longer knows about it (zero link), so it cannot be started again, since Elliot assumes that it is perfectly normal to create a new theme that makes the MyRunnable function MyRunnable , but execution named threads unique to its scope

0
source

Source: https://habr.com/ru/post/1209971/


All Articles