In the preceding code, what we essentially do is define a function called myThread. Within this function, we utilize the threading.currentThread().getName() getter in order to retrieve the current thread's moniker, and print this out both when we start our thread's execution, and when it ends.
We then go on to start a for loop, and create four thread objects that take in the name parameter, which we define as “Thread-” + str(i), as well as the myThread function as the target of that thread's execution.
We then, finally, go on to print out all the active threads currently running. This should print out something like the following:
$ python3.6 11_identifyingThreads.py
Thread Thread-0 starting
Thread Thread-1 starting
Thread Thread-2 starting
Thread Thread-3 starting
[<_MainThread(MainThread, started 140735793988544)>,
<Thread(Thread-0, started 123145368256512)>, <Thread(Thread-1,
started 123145373511680)>, <Thread(Thread-2, started
123145378766848)>, <Thread(Thread-3, started 123145384022016)>]
Thread Thread-0 ending
Thread Thread-2 ending
Thread Thread-3 ending
Thread Thread-1 ending