线程间的通讯问题及Object类的支持
范例:初步实现生产者与消费者问题
class Info {
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
class Productor implements Runnable{
private Info info = null;
public Productor(Info info){
this.info=info;
}
public void run(){
for(int x=0;x<50;x++){
if(x%2==0){
this.info.setTitle("陶帅帅");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setContent("一个小帅哥");
}else{
this.info.setTitle("可爱的小动物");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setContent("草泥马");
}
}
}
}
class Consumer implements Runnable{
private Info info;
public Consumer (Info info){
this.info=info;
}
public void run(){
for(int x=0;x<50;x++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.info.getTitle()+"-->"+this.info.getContent());
}
}
}
public class Hello{
public static void main(String args[]) {
Info info = new Info();
Productor p = new Productor(info);
Consumer c = new Consumer(info);
new Thread(p).start();
new Thread(c).start();
}
}
解决数据不同步问题
要想解决同步问题一定使用同步代码块或者是同步方法,既然要同步,那么肯定将设置属性和取得属性的内容都统一交给Info类完成。
class Info {
private String title;
private String content;
public synchronized void set(String title,String content){
this.title=title;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content=content;
}
public synchronized void get(){
System.out.println(this.title+"-->"+this.content);
}
}
class Productor implements Runnable{
private Info info = null;
public Productor(Info info){
this.info=info;
}
public void run(){
for(int x=0;x<50;x++){
if(x%2==0){
this.info.set("陶帅帅", "一个小帅哥");
}else{
this.info.set("草泥马", "可爱的小动物");
}
}
}
}
class Consumer implements Runnable{
private Info info;
public Consumer (Info info){
this.info=info;
}
public void run(){
for(int x=0;x<50;x++){
this.info.get();
}
}
}
public class Hello{
public static void main(String args[]) {
Info info = new Info();
Productor p = new Productor(info);
Consumer c = new Consumer(info);
new Thread(p).start();
new Thread(c).start();
}
}
所有的设置和取得数据的操作都交给了同步的方法完成。
现在同步的问题解决了,但是重复的操作更严重了。
解决重复问题
范例:修改Info类
class Info {
private String title;
private String content;
private boolean flag = true;
//flag=true,表示可以生产不可以取
//flag=false,表示可以取走不允许生产
public synchronized void set(String title,String content){
if (this.flag==false){//表示已经生产未取走
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//没有生产过,可以生产
this.title=title;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content=content;
this.flag=false;//表示生产过了
super.notify();
}
public synchronized void get(){
if(this.flag==true){
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.title+"-->"+this.content);
this.flag=true;//表示取过了
super.notify();
}
}
面试题:请解释sleep()与wait()的区别?
方法来唤醒