生产者与消费者,同步数据问题
package duoxian;
class Message {
private String tile;
private String content;
public void setContent(String content) {
this.content = content;
}
public String getTile() {
return tile;
}
public String getContent() {
return content;
}
public void setTile(String str) {
this.tile=str;
}
}
class Producer implements Runnable{
private Message msg=null;
public Producer(Message msg) {
this.msg=msg;
}
@Override
public synchronized void run() {
for (int i = 0; i < 50; i++) {
if (i%2==0){
this.msg.setTile("麦观文");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.msg.setContent("Java讲师");
}else{
this.msg.setTile("mldn");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.msg.setContent("www.baidu.com");
}
}
}
}
class Consumer implements Runnable {
private Message msg=null;
public Consumer(Message msg){
this.msg=msg;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.msg.getTile()+"-->"+this.msg.getContent());
}
}
}
存在两个错误:数据错位,重复操作,生产者或消费者多次连续重复操作
解决数据同步问题
使用synchronized同步的方式解决“生成者-消费者”代码中出现的不同步问题
package duoxian;
class Message {
private String tile;
private String content;
public synchronized void set(String tile,String content){
this.tile=tile;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.content=content;
}
public synchronized String get(){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return this.tile+"-->"+this.content;
}
}
class Producer implements Runnable{
private Message msg=null;
public Producer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
if (i%2==0){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.msg.set("麦观文","Java讲师");
}else{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.msg.set("maid","www.baidu.com");
}
}
}
}
class Consumer implements Runnable {
private Message msg=null;
public Consumer(Message msg){
this.msg=msg;
}
@Override
public void run() {
for (int i = 0; i < 50; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.msg.get());
}
}
}
Object线程等待与呼唤
修改Message类,解决数据的重复设置和重复取出操作
使用wai(),notify(),notifyAll(),方法
package duoxian;
class Message {
private String tile;
private String content;
private boolean flag=true;
public synchronized void set(String tile,String content){
if(this.flag==false){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.tile=tile;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.content=content;
this.flag=false;
super.notify();
}
public synchronized String get(){
if (this.flag==true){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
try{return this.tile+"-->"+this.content;
}finally {
this.flag=true;
super.notify();
}
}
}
package duoxian;
public class ThreadDemo {
public static void main(String[] args) {
Message msg=new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}