流程:
- 生产者负责信息内容的生产,消费者负责信息内容的使用。
- 生产者完成一项完整的信息之后消费者要从其中取走信息。
- 如果没有产品,消费者等待生产者生产,如果产品生产满,生产者等待消费者消费产品。
基本代码:
package synchronization;
public class Syntest {
public static void main(String[] args) {
Message msg=new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer(Message msg) {
this.msg=msg;
}
public void run() {
for(int x=0;x<100;x++) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.msg.getTitle()+" "+this.msg.getContent());
}
}
}
class Producer implements Runnable{
private Message msg;
public Producer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
if(x%2==0) {
this.msg.setTitle("xiaoming");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.msg.setContent("很帅");
}else {
this.msg.setTitle("xiaomei");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.msg.setContent("很美");
}
}
}
}
class Message{
private String title;
private String content;
public void setContent(String content) {
this.content = content;
}
public void setTitle(String title) {
this.title=title;
}
public String getContent() {
return content;
}
public String getTitle() {
return title;
}
}
或者
package synchronization;
public class Syntest {
public static void main(String[] args) {
Message msg=new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer(Message msg) {
this.msg=msg;
}
public void run() {
for(int x=0;x<100;x++) {
System.out.println(this.msg.get());
}
}
}
class Producer implements Runnable{
private Message msg;
public Producer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
if(x%2==0) {
this.msg.set("xiaoming","很帅");
}else {
this.msg.set("xiaomei","很美");
}
}
}
}
class Message{
private String title;
private String content;
public void set(String title,String content) {
this.title=title;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content = content;
}
public String get() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return title+" "+content;
}
}
会发现有两个主要问题:
- 数据不同步
- 生产一个取走一个,但是有重复生产重复取出。
只考虑到了结构,没有考虑到同步问题。
解决同步操作
package synchronization;
public class Syntest {
public static void main(String[] args) {
Message msg=new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer(Message msg) {
this.msg=msg;
}
public void run() {
for(int x=0;x<100;x++) {
System.out.println(this.msg.get());
}
}
}
class Producer implements Runnable{
private Message msg;
public Producer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
if(x%2==0) {
this.msg.set("xiaoming","很帅");
}else {
this.msg.set("xiaomei","很美");
}
}
}
}
class Message{
private String title;
private String content;
public synchronized void set(String title,String content) {
this.title=title;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content = content;
}
public synchronized String get() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return title+" "+content;
}
}
但是小明不见了,部分数据不见了。
同步只能保证执行的时候是一个线程一个线程的执行,而不是并发访问处理。不能保证重复操作问题。
利用object类解决重复操作
线程等待与唤醒
如果想要解决生产者与消费者问题,最好使用等待与唤醒机制。
关于等待与唤醒,在object中有提供有三个方法:
- 等待:
- 死等:public final void wait() throws InterruptedException;
- 设置等待时间:public final void wait(long timeout) throws InterruptedException;
- 设置等待时间:public final void wait(long timeout,int nanos) throws InterruptedException;
- 唤醒第一个等待线程:public final void notify();
- 唤醒全部等待线程:public final void notifyAll();
notify()唤醒若干线程中第一个等待的,其他的继续等待。
notifyAll()唤醒全部线程,根据线程优先级高的可能先执行。
主要通过修改Message类实现
package synchronization;
public class Syntest {
public static void main(String[] args) {
Message msg=new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer(Message msg) {
this.msg=msg;
}
public void run() {
for(int x=0;x<100;x++) {
System.out.println(this.msg.get());
}
}
}
class Producer implements Runnable{
private Message msg;
public Producer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
if(x%2==0) {
this.msg.set("xiaoming","很帅");
}else {
this.msg.set("xiaomei","很美");
}
}
}
}
class Message{
private String title;
private String content;
private boolean flag=true;//true表示生产中,不允许消费。false表示消费中不允许生产。
public synchronized void set(String title,String content) {
if(!flag) {
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.title=title;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content = content;
flag=false;//生产完毕
super.notify();
}
public synchronized String get() {
if(flag) {
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
return title+" "+content;
}finally {
flag=true;
super.notify();
}
}
}
多线程开发中最原始的处理方案,所有的等待,同步,唤醒机制都由开发者自行通过原生代码实现控制。