线程间的经典操作案例——生产者与消费者案例
程序基本模型:
package Project.Study.Multithreading;
class Message{
private String title; //保存信息的标题
private String content; //保存信息的内容
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
class Producer implements Runnable{ //定义生产者
private Message msg=null;
public Producer(Message msg){
this.msg=msg;
}
@Override
public void run(){
for (int x=0;x<50;x++){ //生产50次数据
if(x%2==0){
this.msg.setTitle("小关"); //定义title属性
try {
Thread.sleep(100); //延迟操作
}catch (InterruptedException e){
e.printStackTrace();
}
this.msg.setContent("学习Java");//设置content属性
}else{
this.msg.setTitle("小梁"); //设置title属性
try {
Thread.sleep(100); //延迟操作
}catch (InterruptedException e){
e.printStackTrace();
}
this.msg.setContent("学习python");//设置content属性
}
}
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer(Message msg){
this.msg=msg;
}
@Override
public void run(){
for(int x=0;x<50;x++){
try{
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(this.msg.getTitle()+"-->"+this.msg.getContent());
}
}
}
public class Test10 {
public static void main(String []args){
Message msg=new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
//结果:
//小梁-->学习Java
//小关-->学习python
//小梁-->学习Java
//小关-->学习python
//小梁-->学习Java
//小关-->学习python
//小梁-->学习Java
//小关-->学习python
//小梁-->学习Java
//(...)
本来上程序预期的输出结果是:小关–>学习Java,小梁–>学习python
但很明显出现了问题,设置的数据发生了错位、数据重复取出和重复设置。
解决数据错乱问题
数据错位完全是因为非同步的操作,所以应该使用同步处理。
例:加入同步,解决数据错乱问题
package Project.Study.Multithreading;
class Message2{
private String title; //保存信息的标题
private String content; //保存信息的内容
public synchronized void set(String title,String content){
this.title=title;
try{
Thread.sleep(100);
}catch(InterruptedException e){
e.printStackTrace();
}
this.content=content;
}
public synchronized void get(){
try{
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(this.title+"-->"+this.content);
}
}
class Producer2 implements Runnable{
private Message2 meg=null;
public Producer2(Message2 msg){
this.meg=msg;
}
@Override
public void run(){
for(int x=0;x<20;x++){
if(x%2==0){
this.meg.set("小关","学习Java");
}else{
this.meg.set("小梁","学习Python");
}
}
}
}
class Consumer2 implements Runnable{
private Message2 msg=null;
public Consumer2(Message2 msg){
this.msg=msg;
}
@Override
public void run(){
for (int x=0;x<20;x++){
this.msg.get();
}
}
}
public class Test11 {
public static void main(String []args)throws Exception{
Message2 msg=new Message2();
new Thread(new Producer2(msg)).start();
new Thread(new Consumer2(msg)).start();
}
}
//结果:
//小梁-->学习Python
//小梁-->学习Python
//小梁-->学习Python
//小梁-->学习Python
//(...)
上程序解决了数据错位问题,但还是有重复取出和重复设置的问题。
解决数据重复问题
Object类对多线程的支持
No. | 方法 | 类型 | 描述 |
---|---|---|---|
1 | public final void wait() throws InterruptedException | 普通 | 线程的等待 |
2 | public final void notify() | 普通 | 唤醒第一个等待线程 |
3 | public final void notifyAll() | 普通 | 唤醒全部等待线程 |
一般来说,所有等待的线程会按照顺序进行排列。
如果使用了notify()方法,则会唤醒第一个等待的线程执行;
如果使用了notifyAll()方法,则会唤醒所有的等待线程,哪个线程的优先级高,哪个线程就有可能先执行。
例:
package Project.Study.Multithreading;
class Message3{
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){
e.printStackTrace();
}
}
this.title=title;
try {
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
this.content=content;
this.flag=false; //已经生产完成,修改标志位
super.notify(); //唤醒等待线程
}
public synchronized void get(){
if (this.flag==true){ //未生产,不能取走
try {
super.wait(); //等待
}catch (InterruptedException e){
e.printStackTrace();
}
}
try {
Thread.sleep(100);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println(this.title+"-->"+this.content);
this.flag=true; //已经取走了,可以继续生产
super.notify(); //唤醒等待线程
}
}
class Producer3 implements Runnable{
private Message3 meg;
public Producer3(Message3 msg){
this.meg=msg;
}
@Override
public void run(){
for(int x=0;x<10;x++){
if(x%2==0){
this.meg.set("小关","学习Java");
}else{
this.meg.set("小梁","学习Python");
}
}
}
}
class Consumer3 implements Runnable{
private Message3 msg;
public Consumer3(Message3 msg){
this.msg=msg;
}
@Override
public void run(){
for (int x=0;x<10;x++){
this.msg.get();
}
}
}
public class Test12 {
public static void main(String []args){
Message3 msg=new Message3();
new Thread(new Producer3(msg)).start();
new Thread(new Consumer3(msg)).start();
}
}
//结果:
//小关-->学习Java
//小梁-->学习Python
//小关-->学习Java
//小梁-->学习Python
//小关-->学习Java
//小梁-->学习Python
//小关-->学习Java
//小梁-->学习Python
//小关-->学习Java
//小梁-->学习Python
上程序解决了数据错位、数据重复取出和重复设置问题。
提示:
sleep()和wait()的区别:
1.sleep()是Thread类定义的static方法,表示线程休眠,将执行机会给其他线程,但是监控状态依然保持,会自动恢复;
2.wait()是Object类定义的方法,表示线程等待,一直到执行了notify()或notifyAll()后才结束等待。