A console/terminal demo of threading basics for Windows 11 and Ubuntu Linux 24.04. The program starts several "player" threads; each one sleeps for a random 1 to 5 seconds and then prints the time it arrived, while the main thread waits for all of them to finish. Because the threads run at the same time, the total wait is about as long as the slowest player, not the sum of them all. Standard library only — no third-party packages, no GPU.
[Folder Structure]
- CLAUDE.md / AGENT.md: program specification (identical content).
- CHANGELOG.md: change log.
- copyright.txt: copyright notice.
- src/: the example, the launcher scripts, and this document.
- venv/: Python virtual environment, created by step 1.
[Prerequisites]
- Operating system: Windows 11 or Ubuntu Linux 24.04.
- Python 3 and pip.
- No third-party packages — the example uses the standard library only.
- No internet connection and no GPU required.
[Instructions]
Step 1: Create the virtual environment
Create the virtual environment. Because the example uses only the standard library, this step installs no extra packages.
Windows 11:
src\01_venv_create.bat
Ubuntu Linux 24.04:
bash src/01_venv_create.sh
Step 2: Activate the virtual environment
Activate the environment in the current shell. On Linux you must source the script so the activation stays in effect after it finishes.
Windows 11:
src\02_venv_activate.bat
Ubuntu Linux 24.04:
source src/02_venv_activate.sh
Step 3: Run the threading demo
Run the demo. Every player thread starts immediately, sleeps for its own random time, and prints its arrival — so the order of the printed lines is decided by the random delays, not by the order the threads were started.
Windows 11:
src\03_thread.bat
Ubuntu Linux 24.04:
bash src/03_thread.sh
Step 4: Try your own settings
Extra arguments are passed straight through to the program: --players sets how many threads to start, and --seed fixes the random numbers so a run can be repeated exactly.
Windows 11:
src\03_thread.bat --players 8 --seed 42
Ubuntu Linux 24.04:
bash src/03_thread.sh --players 8 --seed 42
Step 5: Remove the virtual environment
When you are finished, delete the venv/ folder to free up disk space. Re-run step 1 whenever you need the environment again.
Windows 11:
src\99_venv_clear.bat
Ubuntu Linux 24.04:
bash src/99_venv_clear.sh
[Python Entry Point]
- --players {n}: how many player threads to start.
- --seed {n}: seed for the random sleep times; the same seed reproduces the same run.
[Usage Notes]
- Starting a thread returns straight away; join() is what makes the main thread wait for it, which is why the program only ends after every player has arrived.
- Run the demo twice without --seed and the arrival order will usually differ — that is the point: concurrent threads have no guaranteed order.
- Threads are ideal for work that spends its time waiting (sleeping, reading files, network requests). For CPU-heavy work, Python's global interpreter lock means multiple processes scale better than multiple threads.
- Each thread here only prints, so no locking is needed. Once threads share and modify the same data, a lock is required to keep it consistent.
This SDK is built in AppForAI - AI Dev Tools.
Purchase license separately: USD 600, permanent authorization, single APP authorization, single machine authorization, one-year activation, one-year download, one-year update, one-year email technical support.