核心内容摘要
7大突破!OpenSpeedy开源游戏加速工具如何彻底改变你的游戏体验
基本概念开发程序是为了解决问题
程序一个存在磁盘中的程序一份文件 代码文件数据文件不能解决问题
进程正在运行中的程序 代码和数据 都在内存中可以解决问题通过代码-计算机指令调度计算机资源CPU缓存、RAM磁盘、IOSystem IO操作系统来解决问题当一个计算机需要同时运行多个进程时需要操作系统来帮忙当一个程序需要同时进行多个任务时需要使用线程操作系统分配资源的最小单元
线程一个进程的组成部分任何一个进程都至少有一个线程进程也需要操作系统帮忙管理线程CPU分配执行任务的最小单元 就是线程进程使用多线程的优势是可以共享数据
线程
Java中实现线程的方式1继承父类Thread继承public class MyThread extends Thread{ }重写方法run public void run( ){ }将需要独立放在线程中执行的代码写在run方法中启动线程使用MyThread类 创建对象并且调用start方法不可调用runeg.创建继承 Thread 的类/** * 继承 Thread 类的自定义线程类 */ public class MyThread extends Thread { private String threadName; // 构造方法可以传入线程名称 public MyThread(String name) { this.threadName name; } /** * 重写 run 方法包含线程要执行的代码 * 注意run() 方法是线程的入口点 */ Override public void run() { System.out.println(threadName 线程开始执行...); try { // 模拟耗时操作 for (int i 1; i 5; i) { System.out.println(threadName - 执行第 i 次任务); // 休眠1秒模拟任务执行时间 Thread.sleep(
; // 可以调用其他方法 doSomething(i); } } catch (InterruptedException e) { System.out.println(threadName 被中断); } System.out.println(threadName 线程执行完毕); } /** * 线程中可以执行的其他方法 */ private void doSomething(int count) { System.out.println(threadName - 正在处理第 count 个数据); } }使用 MyThread 创建线程并启动/** * 主类演示如何使用 MyThread 创建和启动线程 */ public class ThreadExample { public static void main(String[] args) { System.out.println(主线程开始执行...); // 示例1: 创建单个线程 System.out.println( 示例1: 创建单个线程 ); // 第一步: 创建 MyThread 对象 MyThread thread1 new MyThread(线程A); // 第二步: 调用 start() 方法启动线程 thread
start(); // 注意不要调用 run() 方法 // 验证直接调用 run() 和调用 start() 的区别 System.out.println(\n 测试直接调用 run() 方法 ); MyThread testThread new MyThread(测试线程); // 直接调用 run() 方法错误示例 System.out.println(直接调用 run() 方法:); testThread.run(); // 这会在当前线程主线程中执行不会创建新线程 System.out.println(\n 测试调用 start() 方法 ); MyThread testThread2 new MyThread(正确线程); testThread
start(); // 正确会在新线程中执行 // 主线程继续执行其他任务 for (int i 0; i 3; i) { System.out.println(主线程执行任务: i); try { Thread.sleep(
; // 主线程休眠 } catch (InterruptedException e) { e.printStackTrace(); } } // 示例2: 创建多个线程 System.out.println(\n 示例2: 创建多个线程 ); MyThread thread2 new MyThread(线程B); MyThread thread3 new MyThread(线程C); // 启动多个线程 thread
start(); thread
start(); // 等待所有线程执行完毕 try { thread
join(); // 等待线程1结束 thread
join(); // 等待线程2结束 thread
join(); // 等待线程3结束 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(所有线程执行完毕主线程结束); } }2实现接口Runnable实现接口public class MyRun implements Runnable{ }重写run方法实现线程中的代码使用MyRun创建一个对象将这个对象放在一个Thread类对象的构造方法中使用Thread对象start方法eg.创建实现 Runnable 接口的类/** * 实现 Runnable 接口的类 */ public class MyRun implements Runnable { private String threadName; // 构造方法可以传入线程名称 public MyRun(String name) { this.threadName name; } /** * 重写 run 方法包含线程要执行的代码 */ Override public void run() { try { for (int i 1; i 5; i) { // 模拟耗时操作 Thread.sleep(
; // 休眠1秒 // 打印当前线程信息 System.out.println(threadName - 正在执行: i); // 可以在run方法中调用其他方法 doSomething(i); } } catch (InterruptedException e) { System.out.println(threadName 被中断); } System.out.println(threadName 执行完毕); } /** * 线程中可以执行的其他方法 */ private void doSomething(int count) { System.out.println(threadName - 执行第 count 次操作); } }使用 MyRun 创建线程并启动/** * 主类演示如何使用 MyRun 创建和启动线程 */ public class ThreadExample { public static void main(String[] args) { System.out.println(主线程开始执行...); // 示例1: 创建单个线程 System.out.println( 示例1: 创建单个线程 ); // 第一步: 创建 MyRun 对象实现了Runnable接口 MyRun myRun1 new MyRun(线程A); // 第二步: 将 MyRun 对象作为参数创建 Thread 对象 Thread thread1 new Thread(myRun
; // 第三步: 调用 Thread 对象的 start() 方法启动线程 thread
start(); // 主线程继续执行其他任务 for (int i 0; i 3; i) { System.out.println(主线程执行任务: i); try { Thread.sleep(
; // 主线程休眠 } catch (InterruptedException e) { e.printStackTrace(); } } // 示例2: 创建多个线程 System.out.println(\n 示例2: 创建多个线程 ); MyRun myRun2 new MyRun(线程B); MyRun myRun3 new MyRun(线程C); Thread thread2 new Thread(myRun
; Thread thread3 new Thread(myRun
; // 启动多个线程 thread
start(); thread
start(); // 等待所有线程执行完毕 try { thread
join(); // 等待线程1结束 thread
join(); // 等待线程2结束 thread
join(); // 等待线程3结束 } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(所有线程执行完毕主线程结束); } }3实现Callable接口最新复杂目前所学知识无法实现后续学到会补充tips.同一个thread对象不可调用多次start函数同一个线程在一个生命周期内创建-运行-销毁只能启动一次但同一个runnable接口类可以同时被多个thread运行
继承父类和实现接口两种创建线程的方式区别1继承机制继承 Thread 类// 继承 Thread 类 public class MyThread extends Thread { Override public void run() { // 线程代码 } } // 使用 MyThread thread new MyThread(); thread.start();实现 Runnable 接口// 实现 Runnable 接口 public class MyRunnable implements Runnable { Override public void run() { // 线程代码 } } // 使用 MyRunnable runnable new MyRunnable(); Thread thread new Thread(runnable); thread.start();2核心区别对比表对比点继承 Thread 类实现 Runnable 接口继承限制占用继承位置Java单继承不占用继承位置可以继承其他类代码结构线程和任务耦合在一起线程和任务分离更清晰资源共享相对复杂方便共享资源同一个Runnable对象扩展性较差更好更灵活面向对象不符合组合优于继承原则符合面向对象设计原则3实际开发中的选择标准选择继承 Thread 类的情况需要重写 Thread 类的其他方法如 interrupt()简单的单线程任务不需要共享资源学习或演示目的选择实现 Runnable 接口的情况需要继承其他类Java单继承限制需要多个线程共享资源使用线程池管理线程代码需要更好的可扩展性大多数实际项目场景在绝大多数情况下应该优先使用实现 Runnable 接口的方式创建线程。
只有在需要重写 Thread 类的特定方法时才考虑继承 Thread 类。