java线程学习(四)---线程间的通信
来源:互联网 发布:linux中echo命令 编辑:程序博客网 时间:2024/05/07 20:24
线程之间的通信
描述:存在两个线程,一个线程负责写入信息,另一个线程负责打印信息。model类Student name sex (私有类)
线程:Input类,Output类。
启动两个线程分别执行打印和写入操作.
public class InputOutputDemo{ public static void main(String args[]){ Student stu = new Student(); Input in = new Input(stu); Output out = new Output(stu); Thread t1 = new Thread(in); //存入学生信息的线程 Thread t2 = new Thread(out); //打印学生信息的线程 t1.start(); t2.start(); }}class Input implements Runnable{ private Student stu; boolean flag = false; public Input(Student stu){ this.stu = stu; } public void run(){//这个线程的run方法负责将姓名和性别存入 /*在这里定义两个学生切换着存入学生的信息 */ while(true){ if(flag){ stu.name = "xy"; stu.sex = "woman"; flag = false; }else{ stu.name = "李志磊"; stu.sex = "男男男男男男"; flag = true; } } }}class Output implements Runnable{ private Student stu; public Output(Student stu){ //保证传入对象唯一性 this.stu = stu; } public void run(){ while(true) System.out.println(stu.name+"..."+stu.sex); }}class Student{ String name; String sex;}打印信息:
xy...womanxy...womanxy...男男男男男男xy...woman李志磊...woman李志磊...男男男男男男李志磊...男男男男男男李志磊...男男男男男男当线程执行之后,会出现线程间数据问题。(这里需要使用同步的方法来修改代码)
同步的前提:
1、至少有两个线程,在程序中运行
2、同步时使用同一个锁对象。
public class InputOutputDemo{ public static void main(String args[]){ Student stu = new Student(); Input in = new Input(stu); Output out = new Output(stu); Thread t1 = new Thread(in); //存入学生信息的线程 Thread t2 = new Thread(out); //打印学生信息的线程 t1.start(); t2.start(); }}class Input implements Runnable{ private Student stu; boolean flag = false; public Input(Student stu){ this.stu = stu; } public void run(){//这个线程的run方法负责将姓名和性别存入 /*在这里定义两个学生切换着存入学生的信息 */ while(true){ synchronized(stu){ if(flag){ stu.name = "xy"; stu.sex = "woman"; flag = false; }else{ stu.name = "李志磊"; stu.sex = "男男男男男男"; flag = true; } } } }}class Output implements Runnable{ private Student stu; public Output(Student stu){ //保证传入对象唯一性 this.stu = stu; } public void run(){ while(true) synchronized(stu){ System.out.println(stu.name+"..."+stu.sex); } }}class Student{ String name; String sex;}
唤醒等待机制(重要)
java Object类中存在以下方法:
wait():
notify();
notifyAll();
特点:都使用在同步当中,因为要对持有锁(监视器)的对象操作。
所以要在同步中使用,因为同步中才有锁。
描述:要求input类读入一个信息,紧接着output就打印出这条信息。
解决思路:
我们需要将这个类添加一个标识flag。
flag==flase时:表示input没有信息
input开始读入信息,将flag设置为true,并将output唤醒
flag==true时:表示input有信息。
ouput开始打印信息,将flag设置为false,并唤醒input
public class InputOutputDemo1{ public static void main(String args[]){ Student stu = new Student(); Input in = new Input(stu); Output out = new Output(stu); Thread t1 = new Thread(in); //存入学生信息的线程 Thread t2 = new Thread(out); //打印学生信息的线程 t1.start(); t2.start(); }}class Input implements Runnable{ private Student stu; boolean flag = false; int x=0; public Input(Student stu){ this.stu = stu; } public void run(){//这个线程的run方法负责将姓名和性别存入 /*在这里定义两个学生切换着存入学生的信息 */ while(true){ if(flag){ stu.set("xy","woman"); flag = false; }else{ stu.set("李志磊","男男男男男"); flag = true; } } }}class Output implements Runnable{ private Student stu; public Output(Student stu){ //保证传入对象唯一性 this.stu = stu; } public void run(){ while(true){ stu.out(); } }}class Student{ private String name; private String sex; private boolean flag; //设置标志,默认为false public synchronized void set(String name,String sex){ if(flag) try{this.wait();}catch(Exception e){}; this.name = name; this.sex = sex; flag = true; this.notify(); } public synchronized void out(){ if(!flag){ try{this.wait();}catch(Exception e){}; }else{ System.out.println(name+"-----"+sex); flag = false; this.notify(); } }}
1 0
- java线程学习(四)---线程间的通信
- Java线程的学习_线程通信
- java学习--线程间通信
- 多线程基础学习四:线程间的通信
- JAVA多线程-线程间通信(四)-join的使用
- java实现线程间通信的四种方式
- 线程间的四种通信方式
- JAVA线程间的通信
- java线程间的通信
- Java 线程间的通信
- java间的线程通信
- java线程间的通信
- Java线程间的通信
- java线程间的通信
- JAVA线程-线程间通信
- java的线程通信
- java线程学习(四)—Java 多线程间的通讯
- java多线程学习五:线程间的通信
- [Canvas绘图] 第12节 帅哥降临
- js给select设置value值和显示值
- Spring注解@Component、@Repository、@Service、@Controller区别
- MyBatis学习笔记(五)关联查询
- ListView加载图片的错位解决方案(二)
- java线程学习(四)---线程间的通信
- TabActivity+Tabhost+ActivityGroup 创建多tab的分页APP
- IO多路复用之poll总结
- Android Messenger简单的跨进程通信
- 双线性插值
- iOS开发系列--通知与消息机制
- MyBatis 判断条件为等于的问题
- 我所经历的JS性能优化
- Redhat 7.0使用CentOS 7 的Yum 网络源