线程之间的通信
描述:存在两个线程,一个线程负责写入信息,另一个线程负责打印信息。
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;
}
当线程执行之后,会出现线程间数据问题。(这里需要使用同步的方法来修改代码)。
同步的前提:
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){
//flag==flase时:表示input没有信息,input开始读入信息。out开始wait
try{this.wait();}catch(Exception e){};
}else{
System.out.println(name+"-----"+sex);
flag = false;
this.notify();
}
}
}
在现实中,需要线程之间的协作,比如说最经典的生产者-消费者模型:
1. 当队列满时,生产者需要等待队列有空间才能继续往里面放入商品;
2. 而在等待的期间内,生产者必须释放对临界资源(即队列)的占用权;
因为生产者如果不释放对临界资源的占用权,那么消费者就无法消费队列中的商品,就不会让队列有空间,那么生产者就会一直无限等待下去。
因此,一般情况下,当队列满时,会让生产者交出对临界资源的占用权,并进入挂起状态。然后等待消费者消费了商品,然后消费者通知生产者队列有空间了。同样地,当队列空时,消费者也必须等待,等待生产者通知它队列中有商品了。**这种互相通信的过程就是线程间的协作**。
/**
* Wakes up a single thread that is waiting on this object's
* monitor. If any threads are waiting on this object, one of them
* is chosen to be awakened. The choice is arbitrary and occurs at
* the discretion of the implementation. A thread waits on an object's
* monitor by calling one of the wait methods
*/
public final native void notify();
1). wait()、notify()和notifyAll()方法是本地方法,并且为final方法,无法被重写。
2).调用某个对象的wait()方法能让当前线程阻塞,并且当前线程必须拥有此对象的monitor(即锁)
3).调用某个对象的notify()方法能够唤醒一个正在等待这个对象的monitor的线程,如果有多个线程都在等待这个对象的monitor,则只能唤醒其中一个线程;具体唤醒哪个线程则不得而知。
4).调用notifyAll()方法能够唤醒所有正在等待这个对象的monitor的线程;
如果调用某个对象的wait()方法,当前线程必须拥有这个对象的monitor(即锁),因此调用wait()方法必须在同步块或者同步方法中进行(synchronized块或者synchronized方法)。
调用某个对象的wait()方法,相当于让当前线程交出此对象的monitor,然后进入等待状态,等待后续再次获得此对象的锁(Thread类中的sleep方法使当前线程暂停执行一段时间,从而让其他线程有机会继续执行,但它并不释放对象锁);
同样地,调用某个对象的notify()方法,当前线程也必须拥有这个对象的monitor,因此调用notify()方法必须在同步块或者同步方法中进行(synchronized块或者synchronized方法)。
nofityAll()方法能够唤醒所有正在等待该对象的monitor的线程,这一点与notify()方法是不同的。这里要注意一点:notify()和notifyAll()方法只是唤醒等待该对象的monitor的线程,并不决定哪个线程能够获取到monitor。
举个简单的例子:假如有三个线程Thread1、Thread2和Thread3都在等待对象objectA的monitor,此时Thread4拥有对象objectA的monitor,当在Thread4中调用objectA.notify()方法之后,Thread1、Thread2和Thread3只有一个能被唤醒。注意,被唤醒不等于立刻就获取了objectA的monitor。假若在Thread4中调用objectA.notifyAll()方法,则Thread1、Thread2和Thread3三个线程都会被唤醒,至于哪个线程接下来能够获取到objectA的monitor就具体依赖于操作系统的调度了。
一个线程被唤醒不代表立即获取了对象的monitor,只有等调用完notify()或者notifyAll()并退出synchronized块,释放对象锁后,其余线程才可获得锁执行