恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

Java 多线程(抢CPU)

  • 首页
  • 资讯中心
  • /
  • Java 多线程(抢CPU)

相关资讯

浏览器扩展开发者必知:ExtensionPay.js错误处理与调试方法 2026/8/5 21:04:23
OBS多平台直播终极指南:5分钟实现一键多路推流 2026/8/5 20:59:22
Docker Minecraft服务器性能调优实战指南:7个企业级配置策略深度解析 2026/8/5 20:59:22

最新资讯

AI公式粘贴后出现星号?AI导出鸭一键解决乱码难题
技术项目评估四步法:从价值预判到生产集成的系统化拆解
Python列表排序全解析:sort()与sorted()的升序降序实战
FIR滤波器窗函数设计法:从矩形窗到吉布斯现象
深度学习入门幻觉正在毁掉你的职业发展,资深AI研究员紧急发布5条不可逆的学习红线
12个实用技巧:高效节省AI大模型Token,降低使用成本

今日推荐

AI小程序创业陷阱大起底(92%新手踩坑的3个致命错误)
为什么92.7%的AI 3D生成项目卡在UV重拓扑?资深TD曝光内部验证过的5步自动化修复协议
三升四,比成绩下滑更可怕的,是孩子开始「认命」

本周热门

ncmdumpGUI:一键解锁网易云音乐ncm文件的终极解决方案
分布式配置中心选型实战:Nacos与Consul在创业场景下的对比
MoneyPrinterPlus实战指南:AI视频批量生成与自动化发布完整解决方案

本月精选

如何用DamaiHelper实现演唱会门票的智能自动化抢购:完整技术解决方案指南
第4篇:59 倍性能差距的索引瓶颈定位——一次教科书级的全表扫描调优
终极歌词批量下载神器:5分钟解决离线音乐库歌词同步难题

Java 多线程(抢CPU)

发布时间:2026/8/5 21:04:23
Java 多线程(抢CPU) 哈哈哈什么是多线程可以让程序同时做多件事情。多线程的作用提高效率。多线程的应用场景想让多个事情同时运行。并发多个指令在单个CPU交替执行和并行多个指令在多个CPU交替执行多线程的实现方式1.继承Thread类的方式实现简单扩展性差public class ss { public static void main(String[] args) { //1.自己定义一个类继承Thread //2.重写run方法 //3.创建子类的对象并启动线程 MyThread t1new MyThread(); MyThread t2new MyThread(); t1.setName(1); t2.setName(2); t1.start(); t2.start(); } }public class MyThread extends Thread{ Override public void run(){ for (int i 0; i 100; i) { System.out.println(getName()hello); } } }2.实现Runnable接口的方式进行实现复杂扩展性强public class ss { public static void main(String[] args) { //1.自己定义一个类实现Runnable接口 //2.重写里面的run方法 //3.创建自己的类的对象 //4.创建一个Thread类的对象并开启线程 MyRun mrnew MyRun(); Thread tnew Thread(mr); Thread t2new Thread(mr); t.setName(1); t2.setName(2); t.start(); t2.start(); } }public class MyRun implements Runnable{ Override public void run() { for (int i 0; i 100; i) { Thread tThread.currentThread(); System.out.println(t.getName()hello); } } }3.利用Callable接口和Future接口方式实现可以获取结果import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.FutureTask; public class ss { public static void main(String[] args) throws ExecutionException, InterruptedException { //1.创建一个类MyCallable实现Callable接口 //2.重写call //3.创建MyCallable对象 //4.创建FutureTask的对象 //5.创建Thread类对象并启动 MyCallable mcnew MyCallable(); FutureTaskInteger ftnew FutureTask(mc); Thread t1new Thread(ft); t1.start(); Integer resultft.get(); System.out.println(result); } }import java.util.concurrent.Callable; public class MyCallable implements CallableInteger{ Override public Integer call() throws Exception{ int sum0; for (int i 0; i 100; i) { sumsumi; } return sum; } }多线程的常用成员方法public class ss { public static void main(String[] args) throws InterruptedException { //getName MyThread mtnew MyThread(111); MyThread t2new MyThread(); mt.start(); t2.start(); //setName //.... //currentThread Thread tThread.currentThread(); System.out.println(t.getName()); //sleep(long time)毫秒 睡眠时间 System.out.println(11111); Thread.sleep(5000); System.out.println(22222); } }public class MyThread extends Thread{ public MyThread() { } public MyThread(String name) { super(name); } Override public void run() { for (int i 0; i 100; i) { try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } System.out.println(getName()i); } } }线程的优先级public class ss { public static void main(String[] args) throws InterruptedException { MyRunnable mrnew MyRunnable(); Thread t1new Thread(mr,1); Thread t2new Thread(mr,2); System.out.println(t1.getPriority());//默认优先级5 System.out.println(t2.getPriority());//默认优先级5//最小1//最大10 t1.setPriority(1); t2.setPriority(10); t1.start(); t2.start(); } }public class MyRunnable implements Runnable{ Override public void run() { for (int i 0; i 100; i) { System.out.println(Thread.currentThread().getName()i); } } }守护线程起码有2个线程public class ss { public static void main(String[] args) throws InterruptedException { MyThread t1new MyThread(); MyThread2 t2new MyThread2(); t1.setName(1); t2.setName(2); t2.setDaemon(true); t1.start(); t2.start(); } }public class MyThread extends Thread{ Override public void run() { for (int i 0; i 10; i) { System.out.println(getName()i); } } }public class MyThread2 extends Thread{ Override public void run() { for (int i 0; i 10; i) { System.out.println(getName()i); } } }礼让线程public class ss { public static void main(String[] args) throws InterruptedException { MyThread2 t1new MyThread2(); MyThread2 t2new MyThread2(); t1.setName(1); t2.setName(2); t1.start(); t2.start(); } }public class MyThread2 extends Thread{ Override public void run() { for (int i 0; i 100; i) { System.out.println(getName()i); Thread.yield();//礼让一下再重新抢夺CPU的执行权 } } }插入线程/插队线程public class ss { public static void main(String[] args) throws InterruptedException { MyThread t1new MyThread(); t1.setName(1); t1.start(); t1.join();//把t线程插入到当前线程main之前 for (int i 0; i 10; i) { System.out.println(maini); } } }public class MyThread extends Thread{ Override public void run() { for (int i 0; i 100; i) { System.out.println(getName()i); } } }线程的生命周期一个线程从创建到结束。线程的安全问题下面这个代码存在问题public class ss { public static void main(String[] args) throws InterruptedException { MyThread t1new MyThread(); MyThread t2new MyThread(); MyThread t3new MyThread(); t1.setName(窗口1); t2.setName(窗口2); t3.setName(窗口3); t1.start(); t2.start(); t3.start(); } }public class MyThread extends Thread{ static int ticket0;//这个类所有对象共享ticket数据加了static Override public void run() { while (true){ if(ticket100){ try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } ticket; System.out.println(getName()正在卖第ticket张票); }else break; } } }同步代码块可以解决问题public class ss { public static void main(String[] args) throws InterruptedException { MyThread t1new MyThread(); MyThread t2new MyThread(); MyThread t3new MyThread(); t1.setName(窗口1); t2.setName(窗口2); t3.setName(窗口3); t1.start(); t2.start(); t3.start(); } }public class MyThread extends Thread{ static int ticket0;//这个类所有对象共享ticket数据加了static static Object objnew Object(); Override public void run() { while (true){ synchronized (obj){ if(ticket100){ try { Thread.sleep(100); } catch (InterruptedException e) { throw new RuntimeException(e); } ticket; System.out.println(getName()正在卖第ticket张票); }else break; } } } }同步方法就是把synchronized关键字加到方法上public class ss { public static void main(String[] args) throws InterruptedException { MyRunnable mrnew MyRunnable(); Thread t1new Thread(mr); Thread t2new Thread(mr); Thread t3new Thread(mr); t1.setName(窗口1); t2.setName(窗口2); t3.setName(窗口3); t1.start(); t2.start(); t3.start(); } }public class MyRunnable implements Runnable{ int ticket0; Override public void run() { while (true){ if (method()) break; } } private synchronized boolean method() { if(ticket100){ return true; }else{ try { Thread.sleep(10); } catch (InterruptedException e) { throw new RuntimeException(e); } ticket; System.out.println(Thread.currentThread().getName()在卖第ticket张票); } return false; } }lock锁死锁等待唤醒机制1.消费者2.生产者public class Test { public static void main(String[] args) { Cook cnew Cook(); Foodie fnew Foodie(); // c.setName(厨师); // f.setName(吃货); c.start(); f.start(); } }public class Foodie extends Thread{ Override public void run() { while (true){ synchronized (Desk.lock){ if(Desk.count0){ break; }else{ if(Desk.foodFlag0){ try { Desk.lock.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); } } else{ Desk.count--; System.out.println(吃货还能吃Desk.count碗); Desk.lock.notifyAll(); Desk.foodFlag0; } } } } } }public class Cook extends Thread{ Override public void run() { while (true){ synchronized (Desk.lock){ if(Desk.count0){ break; }else { if (Desk.foodFlag1){ try { Desk.lock.wait(); } catch (InterruptedException e) { throw new RuntimeException(e); } }else { System.out.println(厨师做了一碗面条); Desk.foodFlag1; Desk.lock.notifyAll(); } } } } } }public class Desk { public static int foodFlag0;//桌上是否有食物 public static int count10; public static Object locknew Object(); }利用阻塞队列方式实现import java.util.concurrent.ArrayBlockingQueue; public class Test { public static void main(String[] args) { ArrayBlockingQueueString queuenew ArrayBlockingQueue(1); Cook cnew Cook(queue); Foodie fnew Foodie(queue); c.start(); f.start(); } }import java.util.concurrent.ArrayBlockingQueue; public class Foodie extends Thread{ ArrayBlockingQueueString queue; public Foodie(ArrayBlockingQueueStringqueue){ this.queuequeue; } Override public void run() { while (true){ try { String foodqueue.take(); System.out.println(food); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }public class Desk { public static int foodFlag0;//桌上是否有食物 public static int count10; public static Object locknew Object(); }import java.util.concurrent.ArrayBlockingQueue; public class Cook extends Thread{ ArrayBlockingQueueString queue; public Cook(ArrayBlockingQueueStringqueue){ this.queuequeue; } Override public void run() { while (true){ try { queue.put(面条); System.out.println(厨师做了一碗面条); } catch (InterruptedException e) { throw new RuntimeException(e); } } } }多线程的6种状态线程池1.创建线程池2.提交任务3.所有任务全部执行完毕关闭线程池import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class n { public static void main(String[] args) { // 1.获取线程池对象 // ExecutorService pool1Executors.newCachedThreadPool();//无上限 ExecutorService pool1Executors.newFixedThreadPool(3);//有上限 // 2.提交任务 pool1.submit(new MyRunnable()); pool1.submit(new MyRunnable()); pool1.submit(new MyRunnable()); pool1.submit(new MyRunnable()); // 3.销毁线程池 pool1.shutdown(); } }public class MyRunnable implements Runnable{ Override public void run() { for (int i 0; i 100; i) { System.out.println(Thread.currentThread().getName()---i); } } }自定义线程池回头再看最大并行数4核8线程最大并行数8public class sa { public static void main(String[] args) { int countRuntime.getRuntime().availableProcessors(); System.out.println(count); } }线程池多大合适CPU密集型运算 最大并行数1I/O密集型运算 最大并行数*期望CPU利用率*总时间(CPU计算时间等待时间)/CPU计算时间多线程的额外扩展内容回头再看

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号