JAVA 多线程实例-生产者及消费者

  • 需要解决同步问题
  • 加入等待与唤醒
    在这里插入图片描述
  • 线程是指程序的运行流程.多线程机制可以同时运行多个程序块,使程序运行效率更高,在每一个线程创建和消亡之前,均会创建,就绪,运行,阻塞,终止状态之一
package study;

import java.lang.*;
import java.util.regex.Pattern;

class Info{                                                         //保存信息类
   private String name="你好";private String major="2018";
   private boolean flag=false;
   public synchronized void set(String name,String major){   //同步
       if(!flag) {
           try{
               super.wait();                                   //等待消费者取走
           }catch (InterruptedException e){e.printStackTrace();}}
           this.setName(name);
           try {
               Thread.sleep(20);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           this.setMajor(major);
           flag=false;
           super.notify();          //唤醒消费者等待线程
   }
   public synchronized void get(){
       if(flag) {
           try{
               super.wait();   //等待生产者生产
           }catch (InterruptedException e){
               e.printStackTrace();
           }}
           try {
               Thread.sleep(20);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }
           System.out.println(this.getName() + "-->" + this.getMajor());
           flag=true;
           super.notify();    //唤醒生产者等待线程

   }

   public String getMajor() {
       return major;
   }

   public String getName() {
       return name;
   }

   public void setMajor(String major) {
       this.major = major;
   }

   public void setName(String name) {
       this.name = name;
   }
}
class Producer implements Runnable{
     private Info info=null;
     public Producer(Info info){
         this.info=info;
     }
     public void run(){
         boolean flag=false;
         for(int i=0;i<50;i++) {
             if (flag) {
                     this.info.set("你好","2018");
                     flag=false;
             }else{
                 this.info.set("你不好","2018");
                         flag=true;
             }
         }
     }
}
class Consumer implements Runnable{
   private Info info=null;
   public Consumer(Info info){
       this.info=info;
   }
   public void run(){
       for(int i=0;i<50;i++){
           try{
               Thread.sleep(20);
       }catch (InterruptedException e){
               e.printStackTrace();
           }
           this.info.get();}
   }
}

public class demo {
   public static void main(String[] args) {
       Info i=new Info();
       Producer pro=new Producer(i);
       Consumer con=new Consumer(i);
       new Thread(pro).start();
       new Thread(con).start();
   }
}
  • 线程操作范例
//继承Thread类
package study;

import java.lang.*;
import java.util.regex.Pattern;
class Mythread extends Thread{
    private  int time;
    public Mythread (String name,int time){
        super(name);
        this.time=time;
    }
    public void run(){
        try{
            Thread.sleep(this.time);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"线程,休眠"+this.time+"毫秒");
    }
}

public class demo {
    public static void main(String[] args) {
       Mythread my1=new Mythread("A",10000);
       Mythread my2=new Mythread("B",20000);
       my1.start();
       my2.start();
    }
}
//实现Runnable接口
package study;

import java.lang.*;
import java.util.regex.Pattern;
class Mythread implements Runnable{
    private  int time;
    private  String name;
    public Mythread (String name,int time){
        this.name=name;
        this.time=time;
    }
    public void run(){
        try{
            Thread.sleep(this.time);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"线程,休眠"+this.time+"毫秒");
    }
}

public class demo {
    public static void main(String[] args) {
       Mythread my1=new Mythread("A",10000);
       Mythread my2=new Mythread("B",20000);
       new Thread(my1).start();
       new Thread(my2).start();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值