Here's a suggestion:
Mark the time when starting the thread.
Then during the script run, add a time check action and compare the time difference with your desired value.
-> Take appropriate action.
There are many solutions to do this, but which solution helps to optimize the source code is important. Please clarify the purpose, explain more clearly