import threading class PrintThread(threading.Thread): def __init__(self, start_num, step, max_num): super(PrintThread, self).__init__() self.start_num = start_num self.step = step self.max_num = max_num def run(self): i = self.start_num while i <= self.max_num: print(i) i += self.step # 创建两个线程,分别打印奇数和偶数 t1 = PrintThread(1, 2, 100) t2 = PrintThread(0, 2, 100) # 启动线程 t1.start() t2.start() # 等待两个线程执行完毕 t1.join() t2.join()
using System; using System.Threading; class PrintThread { private int startNum; private int step; private int maxNum; public PrintThread(int startNum, int step, int maxNum) { this.startNum = startNum; this.step = step; this.maxNum = maxNum; } public void Run() { for (int i = startNum; i <= maxNum; i += step) { Console.WriteLine(i); } } } class Program { static void Main(string[] args) { // 创建两个线程,分别打印奇数和偶数 PrintThread oddThread = new PrintThread(1, 2, 100); PrintThread evenThread = new PrintThread(0, 2, 100); // 启动两个线程 Thread t1 = new Thread(oddThread.Run); Thread t2 = new Thread(evenThread.Run); t1.Start(); t2.Start(); // 等待两个线程执行完毕 t1.Join(); t2.Join(); } }
标签:奇偶,PrintThread,int,self,startNum,step,num,线程,100 From: https://www.cnblogs.com/zhoudelun/p/17270927.html