java多线程间的通信传值_Java 多线程之间的通信
一个线程送水,一个线程出水:多个线程操作同一个资源,但操作的动作不同。两个线程操作同一资源,但操作的动作不一样。两个方法
packagecn.itcast.day5.thread;//一进一出
public class线程通信
{public static voidmain(String[] args)
{
Resouce resouce= newResouce();
Input input= newInput(resouce);
OutPut outPut= newOutPut(resouce);
Thread thread1= newThread(input);
Thread thread2= newThread(outPut);
thread1.start();
thread2.start();
}
}classResouce
{publicString name;publicString sex;
}class Input implementsRunnable
{
Resouce resouce;
Input(Resouce resouce)
{this.resouce =resouce;
}
@Overridepublic voidrun()
{int x = 1;while (true)
{synchronized (Input.class)//两个线程的锁只要是一个object 在内存中的字节码
{if (x == 0)
{
resouce.name= "peter.peng";
resouce.sex= "boy";
}else{
resouce.name= "彭运松";
resouce.sex= "男";
}
x= (x + 1) % 2;
}
}
}
}class OutPut implementsRunnable
{
Resouce resouce;
OutPut(Resouce resouce)
{this.resouce =resouce;
}
@Overridepublic voidrun()
{while (true)
{synchronized (Input.class) //两个线程的锁只要是一个object 在内存中的字节码
{
System.out.println(Thread.currentThread()+ "-----:" + resouce.name + ":" +resouce.sex);
}
}
}
}
进水与进水的案例
如果要达到一进一去,交替出现而不是从上一案例一样,则得用到 wait() notify() notityAll(),理解这几个意义
1 packagecn.itcast.day5.thread;2
3 //一进一出
4 public class线程通信5 {6 public static voidmain(String[] args)7 {8 Resouce resouce = newResouce();9 Input input = newInput(resouce);10 OutPut outPut = newOutPut(resouce);11
12 Thread thread1 = newThread(input);13 Thread thread2 = newThread(outPut);14
15 thread1.start();16 thread2.start();17 }18 }19
20 classResouce21 {22 publicString name;23 publicString sex;24 boolean flag=false;25 }26
27 class Input implementsRunnable28 {29 Resouce resouce;30
31 Input(Resouce resouce)32 {33 this.resouce =resouce;34 }35
36 @Override37 public voidrun()38 {39 int x = 0;40 while (true)41 {42 synchronized (resouce)//两个线程的锁只要是一个object 在内存中的字节码
43 {44 if(resouce.flag)45 try
46 {47 wait();48 //由于第一次flag=false 所以线程1不会等待,就会执行x==0,赋一次值,赋值完后,flag=true,这时线程149 //就会等待,同是叫醒线程2.50 //由于flag=true 则线程2不会等待,就会执行输出语句,输出完后,flag=false,这时线程2就会等待,同时叫醒线程151 //这时就会执行else语句给resource第二次赋值,赋完值后x=(x+1)%2,这里x=0,同时flag=true,线程1又等待。同时52 //第二次叫醒线程2 .............
53 }54 catch(Exception e)55 {56 }57 if (x == 0)58 {59 resouce.name = "peter.peng";60 resouce.sex = "boy";61 } else
62 {63 resouce.name = "彭运松";64 resouce.sex = "男";65 }66 x = (x + 1) % 2;67 resouce.flag = true;68 resouce.notify();//叫醒线程2
69 }70 }71 }72
73 }74
75 class OutPut implementsRunnable76 {77 Resouce resouce;78
79 OutPut(Resouce resouce)80 {81 this.resouce =resouce;82 }83
84 @Override85 public voidrun()86 {87 while (true)88 {89 synchronized (resouce) //两个线程的锁只要是一个object 在内存中的字节码
90 {91 if (!resouce.flag)92 try
93 {94 resouce.wait();95 }96 catch(Exception e)97 {98
99 }100 System.out.println(resouce.name + ":" +resouce.sex);101 resouce.flag = false;102 resouce.notify();//叫醒线程1
103 }104 }105 }106 }
多张程交替打印两个人名
对等待线程的优化(上一程序) 对Resurce进行封装,并把set get 方法进行同步。
生者消费者的例子
1 packagenet.nw.entites;2
3 public classThreadDemo_Product_Customer {4
5 public static voidmain(String[] args) {6
7 Resource resource = newResource();8 Product product = newProduct(resource);9 Customer customer = newCustomer(resource);10
11 Thread t1 = newThread(product);12 Thread t2 = newThread(customer);13 Thread t3 = newThread(product);14 Thread t4 = newThread(customer);15
16 t1.start();17 t2.start();18 t3.start();19 t4.start();20 }21 }22
23 classResource {24 publicString name;25 public booleanflag;26 public int count = 0;27
28 public synchronized voidSet(String name) {29 while (true) {30 while(flag) {31 try{32 wait();33 } catch(Exception e) {34 e.printStackTrace();35 }36 }37 this.name = name + count++;38 System.out.println(Thread.currentThread().getName() + "-P:"
39 + this.name);40 flag = true;41 notifyAll();42 }43 }44
45 public synchronized voidgetOut() {46 while (true) {47 while (!flag) {48 try{49 wait();50 } catch(Exception e) {51 e.printStackTrace();52 }53 }54 System.out.println(Thread.currentThread().getName() + "-C:"
55 + this.name);56 flag = false;57 notifyAll();58 }59 }60 }61
62 class Product implementsRunnable {63 Resource resource;64
65 Product(Resource resource) {66 this.resource =resource;67 }68
69 @Override70 public voidrun() {71 resource.Set("pc");72 }73 }74
75 class Customer implementsRunnable {76 Resource resource;77
78 Customer(Resource resource) {79 this.resource =resource;80 }81
82 @Override83 public voidrun() {84 resource.getOut();85 }86
87 }
两个以上线程的消费与生产关系
JdK1.5 新特性的多线程操作
1 packagenet.nw.entites;2
3 importjava.util.concurrent.locks.Condition;4 importjava.util.concurrent.locks.Lock;5 importjava.util.concurrent.locks.ReentrantLock;6
7 public classThreadDemo_Product_Customer {8
9 public static voidmain(String[] args) {10
11 Resource resource = newResource();12 Product product = newProduct(resource);13 Customer customer = newCustomer(resource);14
15 Thread t1 = newThread(product);16 Thread t2 = newThread(customer);17 Thread t3 = newThread(product);18 Thread t4 = newThread(customer);19
20 t1.start();21 t2.start();22 t3.start();23 t4.start();24 }25 }26
27 classResource {28 publicString name;29 public booleanflag;30 public int count = 0;31
32 final Lock lock = newReentrantLock();33 final Condition notFull =lock.newCondition();34 final Condition notEmpty =lock.newCondition();35
36 public voidSet(String name) {37 lock.lock();38 try{39 while (true) {40 while(flag) {41 try{42 notFull.await();43 } catch(Exception e) {44 e.printStackTrace();45 }46 }47 this.name = name + count++;48 System.out.println(Thread.currentThread().getName() + "-P:"
49 + this.name);50 flag = true;51 notEmpty.signal();52 }53 } finally{54 lock.unlock();55 }56 }57
58 public voidgetOut() {59 lock.lock();60 try{61 while (true) {62 while (!flag) {63 try{64 notEmpty.await();65 } catch(Exception e) {66 e.printStackTrace();67 }68 }69 System.out.println(Thread.currentThread().getName() + "-C:"
70 + this.name);71 flag = false;72 notFull.signal();73 }74 } finally{75 lock.unlock();76 }77 }78 }79
80 class Product implementsRunnable {81 Resource resource;82
83 Product(Resource resource) {84 this.resource =resource;85 }86
87 @Override88 public voidrun() {89 resource.Set("pc");90 }91 }92
93 class Customer implementsRunnable {94 Resource resource;95
96 Customer(Resource resource) {97 this.resource =resource;98 }99
100 @Override101 public voidrun() {102 resource.getOut();103 }104
105 }
两个以上线程的生产与消费关系
说明:Lock提供更为优秀的方法来替换synchronized lock.lock lock.unlock(放在finally)
await single singleall 分别对应 wait notify notifyAll ,但JDK1.5后提供了codition,可以为生产消费指这不同的codition,这样两个线程或是两个以上线程就可以分开出来控制
如果是同一个codition singleAll时就会呼醒其它的线程同时也可以是同类的线程,达不到控制的效果。
线程的中断:其实只要控制住run方法的循环条件就可以了,但有一种情况下await()时就没有办法做了,因为根本就不会执行循环体的语句,这时要用到interruput,使线程势抛出一个异常InterruptedException,然后在验证循环体的条件。
1 packagenet.nw.entites;2
3 importjava.util.concurrent.locks.Condition;4 importjava.util.concurrent.locks.Lock;5 importjava.util.concurrent.locks.ReentrantLock;6
7 public classThreadDemo_Product_Customer {8
9 public static voidmain(String[] args) {10
11 Resource resource = newResource();12 Product product = newProduct(resource);13 Customer customer = newCustomer(resource);14
15 Thread t1 = newThread(product);16 Thread t2 = newThread(customer);17 Thread t3 = newThread(product);18 Thread t4 = newThread(customer);19 t1.start();20 t2.start();21
22 //t3.start();23 //t4.start();
24 }25 }26
27 classResource {28 publicString name;29 public booleanflag;30 public boolean conditionFlag = true;31 public int count = 0;32
33 final Lock lock = newReentrantLock();34 final Condition notFull =lock.newCondition();35 final Condition notEmpty =lock.newCondition();36
37 public voidSet(String name) {38
39 lock.lock();40 try{41 while(conditionFlag) {42 while(flag) {43 try{44 notFull.await();45 Thread.currentThread().interrupt();//把当前的线程中断了。
46 } catch(InterruptedException e) {47 conditionFlag = false;48 }49 }50 this.name = name + count++;51 System.out.println(Thread.currentThread().getName() + "-P:"
52 + this.name);53 flag = true;54 notEmpty.signal();55 }56 } finally{57 lock.unlock();58 if(Thread.interrupted()) {59 System.out.println(Thread.currentThread().getName()60 + ":线程中断了。。。。。。。。。。。");61 }62 }63 }64
65 public voidgetOut() {66
67 lock.lock();68 try{69 while(conditionFlag) {70 while (!flag) {71 try{72 notEmpty.await();73 Thread.currentThread().interrupt();//把当前的线程中断了。
74 } catch(InterruptedException e) {75 conditionFlag = false;76 }77 }78 System.out.println(Thread.currentThread().getName() + "-C:"
79 + this.name);80 flag = false;81 notFull.signal();82 }83 } finally{84 lock.unlock();85 if(Thread.interrupted()) {86 System.out.println(Thread.currentThread().getName()87 + ":线程中断了。。。。。。。。。。。");88 }89 }90 }91 }92
93 class Product implementsRunnable {94 Resource resource;95
96 Product(Resource resource) {97 this.resource =resource;98 }99
100 @Override101 public voidrun() {102 resource.Set("pc");103 }104 }105
106 class Customer implementsRunnable {107 Resource resource;108
109 Customer(Resource resource) {110 this.resource =resource;111 }112
113 @Override114 public voidrun() {115 resource.getOut();116 }117
118 }
多线程安全通信的线程中断
线程的join setDaemon yield
1 packagecn.itcast.day5.thread;2
3 class Demo1 implementsRunnable4 {5 private int count = 100;6
7 @Override8 public voidrun()9 {10 while (count > 0)11 {12 System.out.println(Thread.currentThread().getName() + ":" + "------peter.peng" + count--);13 Thread.yield();14 }15 }16 }17
18 public classThreadJionYield19 {20 public static void main(String[] args) throwsInterruptedException21 {22 //SetDaemon();//设为后台线程23 //ThreadJoin();//join可以用来时加入线程,遇到一个线程的Join主线程冻结,其它的不变,只到Join这个线程结束,主线程再执行。24 //SetPriority();//设置线程的优势级
25 Thread thread1 = new Thread(new Demo1());//yield让当前线程停止执行另一个线程。
26 Thread thread2 = new Thread(newDemo1());27
28 thread1.start();29 thread2.start();30 }31
32 private static voidSetPriority()33 {34 Thread thread1 = new Thread(newRunnable()35 {36 int count = 100;37
38 public voidrun()39 {40 while (count > 0)41 {42 System.out.println(Thread.currentThread().toString() + "------peter.peng" + count--);43 }44 }45 });46
47 Thread thread2 = new Thread(newRunnable()48 {49 int count = 100;50
51 public voidrun()52 {53 while (count > 0)54 {55 System.out.println(Thread.currentThread().toString() + "------peter.peng" + count--);56 }57 }58 });59
60 thread1.start();61 thread1.setPriority(Thread.MAX_PRIORITY);//设线程的优化级
62 thread2.start();63
64 System.out.println(Thread.currentThread().toString());65 }66
67 private static void ThreadJoin() throwsInterruptedException68 {69 Thread thread1 = new Thread(newRunnable()70 {71 int count = 100;72
73 public voidrun()74 {75 while (count > 0)76 {77 System.out.println(Thread.currentThread() + "------peter.peng" + count--);78 }79 }80 });81
82 Thread thread2 = new Thread(newRunnable()83 {84 int count = 100;85
86 public voidrun()87 {88 while (count > 0)89 {90 System.out.println(Thread.currentThread() + "------peter.peng" + count--);91 }92 }93 });94
95 thread1.start();96 //thread1.join();//说明thread1要主线程的执行权,这个时候主线程就会冻结了,只到它不要了才会给主线程,如果把它放在后面来执行
97 thread2.start();98 thread1.join();//这时thread1 与thread2交才替出现,只到结束才执行主线程的方法。
99
100 System.out.println(Thread.currentThread() + ":main");101 }102
103 private static voidSetDaemon()104 {105 Thread thread1 = new Thread(newRunnable()106 {107 int count = 0;108
109 public voidrun()110 {111 while (true)112 {113 System.out.println("------peter.peng" + count++);114 }115 }116 });117
118 Thread thread2 = new Thread(newRunnable()119 {120 int count = 0;121
122 public voidrun()123 {124 while (true)125 {126 System.out.println("------peter.peng" + count++);127 }128 }129 });130 thread1.setDaemon(true);//设为后台线程
131 thread2.setDaemon(true);//设为后台线程
132 thread1.start();133 thread2.start();134 System.out.println("over");135 }136 }
线程的Join,setDaemon yield
java多线程间的通信传值_Java 多线程之间的通信相关推荐
- java不同进程的相互唤醒_JAVA多线程之线程间的通信方式
一,介绍 本总结我对于JAVA多线程中线程之间的通信方式的理解,主要以代码结合文字的方式来讨论线程间的通信,故摘抄了书中的一些示例代码. 二,线程间的通信方式 ①同步 这里讲的同步是指多个线程通过sy ...
- vue中子组件和子组件之间怎么通信_vue.js组件之间如何通信?
vue.js组件之间如何通信?下面本篇文章就来给大家介绍一下Vue.js组件间通信方式.有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助. 平时在使用Vue框架的业务开发中,组件不仅仅要 ...
- java线程异步传值_Java 多线程传值的四种方法
Java 多线程传值的四种方法 作者: sunjs 更新时间:2020-09-11 15:20:16 原文链接 其实大家都知道多线程传值有三种方式: 1:通过构造方法传递数据 2:通过变量和方法传递数 ...
- Java中Thread中的实例方法_Java多线程2:Thread中的实例方法
Thread类中的方法调用方式: 学习Thread类中的方法是学习多线程的第一步.在学习多线程之前特别提出一点,调用Thread中的方法的时候,在线程类中,有两种方式,一定要理解这两种方式的区别: 1 ...
- java多线程生产者与消费者问题_Java多线程详解之四:生产者消费者问题
一.问题描述 生产者消费者问题(Producer-Consumer problem),也称有限缓冲区问题(Bounded-buffer promblem),是一个多线程同步问题的经典案例.对于一个固定 ...
- JAVA所有选手就位后比赛开始_Java多线程-CountDownLatch、CyclicBarrier、Semaphore
上次简单了解了多线程中锁的类型,今天要简单了解下多线程并发控制的一些工具类了. 1. 概念说明: CountDownLatch:相当于一个待执行线程计数器,当计数减为零时表示所有待执行线程都已执行完毕 ...
- java请模拟出双重定时器_Java多线程基础 - osc_czmaebyq的个人空间 - OSCHINA - 中文开源技术交流社区...
( 1 ) 传统使用类Thread和接口Runnable实现 1. 在Thread子类覆盖的run方法中编写运行代码 方式一 newThread(){ @Overridepublic voidrun( ...
- java线程的创建与执行_Java多线程的创建和运行
1.多线程的好处 多线程是一个很有用的东西,它使的系统可以同时运行多个任务,提高程序的执行效率.大家平时可能没有注意到,其实我们电脑能同时执行多个程序的基本原理就是多线程. 每一个程序都是一个进程,而 ...
- java线程初始方法三种_Java 多线程 三种实现方式
Java多线程实现方式主要有三种:继承Thread类.实现Runnable接 口.使用ExecutorService.Callable 实现有返回结果的多线程.其中前两种方式线程执行完后都没有返回值, ...
最新文章
- 机器学习入门(01)— 感知机概念、实现、局限性以及多层感知机
- 在数字时代,如何成为一个真正有身份的人?
- .NET中,字符串首字母大写的方法
- mpvue外卖小程序
- NHibernate3剖析:Query篇之NHibernate.Linq标准查询
- python统计窗口函数怎么处理_python时间序列:移动窗口函数前篇
- 关闭 Visual Studio 2013 的 Browser Link 功能
- Deep Zoom Composer 正式版发布!
- javascript-按圆形排列DIV元素(三)实例---- 图片按椭圆形转动
- 前嗅ForeSpider教程:如何创建新任务
- linux java升级版本_为嵌入式Linux设备实现更新/升级系统
- 【分享一套网站源代码】wufowang网源码下载
- 关于 HDFS Append
- SpringBoot配置文件映射到JavaBean
- [操作系统] 线程和进程的简单解释
- 微信小程序官方demo下载地址
- Javashop电商系统7.0发布
- 25、ExtJs操作用友华表Cell插件(No.1)认识Cell
- MuMu模拟器的安装
- Canvas彩色樱花图案背景js特效
热门文章
- linux中tr命令的用法
- 第十四章 虚拟专网 ×××
- Ubuntu gbk,utf-8 转换
- 百度地图开发总结----3.判断一个点是否在一片区域内
- php 利用fsockopen GET/POST 提交表单及上传文件
- .NET获取不到js写的cookie解决方法
- 定义了过多字段-Excel
- SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59
- 2-django进阶之日志功能(亲测)
- 高考 | 满分作文:《我们都是读“书”人》