Вот несколько методов параллельного или параллельного программирования, а также примеры кода:
-
Потоки в Python:
import threading def worker(): print("Hello from a worker thread!") # Create a new thread and start it thread = threading.Thread(target=worker) thread.start() # Main thread continues its execution print("Hello from the main thread!")
-
Процессы в Python:
import multiprocessing def worker(): print("Hello from a worker process!") # Create a new process and start it process = multiprocessing.Process(target=worker) process.start() # Main process continues its execution print("Hello from the main process!")
-
Темы Java:
class Worker implements Runnable { public void run() { System.out.println("Hello from a worker thread!"); } } public class Main { public static void main(String[] args) { // Create a new thread and start it Thread thread = new Thread(new Worker()); thread.start(); // Main thread continues its execution System.out.println("Hello from the main thread!"); } }
-
Задачи C#:
using System; using System.Threading.Tasks; public class Program { public static void Main() { // Create a new task and start it Task task = Task.Run(() => Worker()); // Main thread continues its execution Console.WriteLine("Hello from the main thread!"); // Wait for the task to complete task.Wait(); } public static void Worker() { Console.WriteLine("Hello from a worker task!"); } }