多线程 Cap 编程
主要技能和概念
• 了解创建多线程的基础知识
• 了解 Thread 类和 Runnable
接口
• 创建一个线程
• 创建多个线程
• 确定线程何时结束
• 使用线程优先级
• 了解线程同步
• 使用同步方法
• 使用同步块
• 促进线程之间的通信
• 暂停、恢复和停止线程
线程:这些是程序内独立的执行路径。
多任务:它可以基于进程(多个程序)或线程(同一程序中的多个任务)。
优点:
利用空闲时间提高效率。
更好地利用多核/多处理器系统。
线程创建和管理
类和接口:
Thread:封装线程的类。
Runnable:用于定义自定义线程的接口。
常用线程类方法:
- getName():返回线程的名称。
- getPriority():返回优先级。
- isAlive():检查线程是否仍在执行。
- join():等待线程完成。
- run():定义线程的入口点。
- sleep(long ms):暂停线程一段时间。
- start(): 开始线程执行。
创建主题:
- 实现可运行:
class MyThread implements Runnable { String threadName; MyThread(String name) { threadName = name; } public void run() { System.out.println(threadName + " starting."); try { for (int i = 0; i < 10; i++) { Thread.sleep(400); System.out.println("In " + threadName + ", count is " + i); } } catch (InterruptedException e) { System.out.println(threadName + " interrupted."); } System.out.println(threadName + " terminating."); } } public class UseThreads { public static void main(String[] args) { System.out.println("Main thread starting."); MyThread myThread = new MyThread("Child #1"); Thread thread = new Thread(myThread); thread.start(); for (int i = 0; i < 50; i++) { System.out.print("."); try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } } System.out.println("Main thread ending."); } }
预期输出:
Main thread starting. . Child #1 starting. .. In Child #1, count is 0 ... In Child #1, count is 1 ... Main thread ending.
线程类扩展:
class MyThread extends Thread { MyThread(String name) { super(name); } public void run() { System.out.println(getName() + " starting."); try { for (int i = 0; i < 10; i++) { Thread.sleep(400); System.out.println("In " + getName() + ", count is " + i); } } catch (InterruptedException e) { System.out.println(getName() + " interrupted."); } System.out.println(getName() + " terminating."); } } public class UseThreads { public static void main(String[] args) { System.out.println("Main thread starting."); MyThread thread = new MyThread("Child #1"); thread.start(); for (int i = 0; i < 50; i++) { System.out.print("."); try { Thread.sleep(100); } catch (InterruptedException e) { System.out.println("Main thread interrupted."); } } System.out.println("Main thread ending."); } }
注意:sleep()方法使得调用它的线程暂停执行
在指定的毫秒时间内。
书桌
以上是多线程 Cap 编程的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

在使用IntelliJIDEAUltimate版本启动Spring...

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...
