多线程基础

简介:

线程是一个程序内部的顺序控制流。


线程和进程的区别:
  每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销。
  线程可以看成是轻量级的进程,同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器(PC),线程切换的开销小。
  多进程:在操作系统中能同时运行多个任务(程序)
  多线程:在同一应用程序中有多个顺序流同时执行

 


当多线程sleep时,如何停止呢?
1、interrupt()方法,这个方法很粗暴不建议使用,这个相当于睡觉的时候一盆凉水泼下来,会抛出interruptedException。
2、stop()方法,已经废弃了,这个比interrupt更粗暴,直接停止线程。不建议使用。
3、用标量在外部控制。


join方法:合并某个线程。把处于并行运行状态的线程合并到当前线程中。


yield()方法:让出CPU,给其他线程执行的机会。


setPriority():设置优先级,优先级越高,cpu分得时间片就越多,越容易得到执行。


线程同步:在Java语言中,引入了对象互斥锁的概念,保证共享数据操作的完整性。每个对象都对应于一个可称为“互斥锁”的标记。这个标记保证在任何一个时刻,只能有一个线程访问该对象。
关键字synchronized来与对象的互斥锁联系。当某个对象synchronized修饰时,表明该对象在任一时刻只能由一个线程访问。synchronized可以作为关键字设在方法上,也可以使用synchronized(this){}中表明锁定该对象的内容。


尽量只锁定一个对象,避免死锁。


当一个线程锁定了一个方法的时候,其他线程可以通过其他方法修改值,所以,一定要保证所有有关这个变量的方法都要加锁。


同步实例:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:Microsoft YaHei;"><span style="font-size:12px;">package thread;  
  2.   
  3. public class TT implements Runnable{  
  4.   
  5.     int b = 100 ;  
  6.       
  7.     @Override  
  8.     public void run() {  
  9.         m1() ;  
  10.     }  
  11.       
  12.     //只会锁定m1方法内的内容,不会锁定b  
  13.     public synchronized void m1() {  
  14.         b = 1000 ;  
  15.         try {  
  16.             Thread.sleep(5000);  
  17.         } catch (InterruptedException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.         System.out.println("b = " + b);  
  21.     }  
  22.   
  23.     public void m2() {  
  24.         System.out.println(b);  
  25.     }  
  26.       
  27.     public static void main(String[] args) {  
  28.         TT t = new TT() ;  
  29.         Thread thread = new Thread(t) ;  
  30.         thread.start() ;  
  31.         try {  
  32.             Thread.sleep(1000);  
  33.         } catch (InterruptedException e) {  
  34.             e.printStackTrace();  
  35.         }  
  36.         t.m2();  
  37.     }  
  38. }  
  39. </span></span>  




生产者与消费者实例:

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-family:Microsoft YaHei;">package thread;  
  2.   
  3. public class ProducerConsumer {  
  4.   
  5.     public static void main(String[] args) {  
  6.         SyncStack ss = new SyncStack() ;  
  7.         Producer p = new Producer(ss) ;  
  8.         Consumer c = new Consumer(ss) ;  
  9.         new Thread(p).start();  
  10.         new Thread(c).start();  
  11.     }  
  12. }  
  13.   
  14. class WoTou {  
  15.     int id ;  
  16.     WoTou(int id) {  
  17.         this.id = id ;  
  18.     }  
  19.     public String toString() {  
  20.         return "id = " + this.id ;  
  21.     }  
  22. }  
  23.   
  24. class SyncStack {  
  25.     int index = 0 ;  
  26.     WoTou[] arrWt = new WoTou[6] ;  
  27.       
  28.     public synchronized void push(WoTou wt) {  
  29.         while(index == arrWt.length) {  
  30.             try {  
  31.                 this.wait();  
  32.             } catch (InterruptedException e) {  
  33.                 // TODO Auto-generated catch block  
  34.                 e.printStackTrace();  
  35.             }  
  36.         }  
  37.         this.notify();  
  38.         arrWt[index] = wt ;  
  39.         index ++ ;  
  40.     }  
  41.       
  42.     public synchronized WoTou pop() {  
  43.         while(index == 0) {  
  44.             try {  
  45.                 this.wait();  
  46.             } catch (InterruptedException e) {  
  47.                 e.printStackTrace();  
  48.             }  
  49.         }  
  50.         this.notify();  
  51.         index -- ;  
  52.         return arrWt[index] ;  
  53.     }  
  54. }  
  55.   
  56. class Producer implements Runnable {  
  57.       
  58.     SyncStack s = null ;  
  59.       
  60.     public Producer(SyncStack s) {  
  61.         this.s = s ;  
  62.     }  
  63.   
  64.     @Override  
  65.     public void run() {  
  66.         for(int i=0; i<20; i++) {  
  67.             WoTou wt = new WoTou(i) ;  
  68.             System.out.println("push : " + wt);  
  69.             s.push(wt);  
  70.         }  
  71.     }  
  72. }  
  73.   
  74. class Consumer implements Runnable {  
  75.       
  76.     SyncStack s = null ;  
  77.       
  78.     public Consumer(SyncStack s) {  
  79.         this.s = s ;  
  80.     }  
  81.   
  82.     @Override  
  83.     public void run() {  
  84.         for(int i=0; i<20; i++) {  
  85.             WoTou wt = s.pop() ;  
  86.             System.out.println("pop : " + wt);  
  87.         }  
  88.     }  
  89. }</span>  


wait和sleep的区别
wait时别的线程可以访问锁定对象,调用wait方法的时候必须锁定该对象。sleep时别的线程也不可以访问锁定对象。
目录
相关文章
|
机器学习/深度学习
L4-数字特征:期望、方差、协方差、相关系数等
L4-数字特征:期望、方差、协方差、相关系数等
L4-数字特征:期望、方差、协方差、相关系数等
|
前端开发
css如何显示文字垂直居中
css如何显示文字垂直居中
278 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8059 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18321 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
778 0
|
8天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1479 5