I am getting into multithreading in Java, and I ran into an odd problem.
When I try to start a new thread with my scheduler using Thread.start()
, I learned that we need a Thread.isAlive()
check because starting and resuming take different processes. however, with this code showing the check and start()
:
if ( current.isAlive( ) )
current.setPriority( 4 );
else {
// Spawn must be controlled by Scheduler
// Scheduler must start a new thread
current.start( );
current.setPriority( 4 );
}
where current is guaranteed to be a valid thread. I end up with this error
Exception in thread "Thread-0" java.lang.IllegalThreadStateException
at java.base/java.lang.Thread.start(Thread.java:790)
at Scheduler.run(Scheduler.java:152)
Which is why I think it's a problem of calling start()
when it's not supposed to . However, to all appearances, it shouldn't be calling start()
except when the thread is not alive.
Thus, my question is two-fold:
Is there something wrong with the above code that allows the second block to run if a thread is already started, and
What other things could be going wrong that would throw that exception? I see the same exception thrown in
setDaemon()
, however I'm not calling that, so that should be irrelevant.