I’ve been reading up on how to use Thread in java, and I’m hoping someone can help me verify I’m using it correctly. I’m concerned that I should be calling .interrupt() or destroying the thread in some way.
I have a simple script that just hits my server to verify some data. My code:
Thread checkregister = new Thread(){
@Override
public void run(){
checkSystem();
}
};
checkregister.start();
Where checkSystem() posts the device id to a php script and waits for the response via HttpClient & HttpResponse. There isn’t any looping so I don’t think blocking is called for, but please let me know if I’m wrong.
No need to destroy the
Thread. TheThreadis effectively taken out of the thread scheduler as soon asrun()returns.If for some reason you need a way to prematurely “end” the Thread, this is a bit more complicated and there’s been a lot of discussion about the proper way to do it. Simple way though is to just call
stop()on theThread.