### 線程之間的通信
描述:存在兩個線程,一個線程負責寫入信息,另一個線程負責打印信息。
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...woman
xy...woman
xy...男男男男男男
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();
}
}
}
~~~