钛极OS(TiJOS)物联网操作系统之小试牛刀(5)——生产者与消费者-多线程

版权声明:本文为博主原创文章,如转载请注明出处和作者。 https://blog.csdn.net/crashMaker/article/details/78815545

钛极OS(TiJOS)物联网操作系统之小试牛刀(5)——生产者与消费者-多线程

作者:crashMaker

说明

开发环境的搭建以及应用工程的创建方法在这里不再赘述,可参考笔者文章《钛极OS(TiJOS)物联网操作系统之初体验》或访问钛极OS(TiJOS)系统官方发布的教程☞钛极OS(TiJOS)应用开发环境搭建

功能实现

验证钛极OS(TiJOS)系统支持多线程功能,使用生产者和消费者方式并采用Java语言编程测试。

硬件准备

  1. TiKit-T600-ESP8266A 开发板

  2. USB线两条

知识背景

生产者-消费者模式是一个经典的多线程设计模式,它为多线程的协作提供了良好的解决方案。在生产者-消费者模式中,通常有两类线程,即若干个生产者线程和若干个消费者线程。生产者线程负责提交用户请求,消费者线程负责处理用户请求。生产者和消费者之间通过共享内存缓冲区进行通信。

生产者-消费者模式中的内存缓冲区的主要功能是数据在多线程间的共享。此外,通过该缓冲区,可以缓解生产者和消费者之间的性能差。

Java多线程编程中,常用的多线程设计模式包括:Future模式、Master-Worker模式、Guarded Suspeionsion模式、不变模式和生产者-消费者模式等。

详细技术文章请参考:http://blog.csdn.net/ghuil/article/details/41044257

程序编写

在Eclipse中新建TiJOS Application工程,编写JAVA代码如下:

CounterThread.java源代码

/** 
 * 扩展Thread类实现多线程程序
 */ 
public class CounterThread extends Thread{ 

     public CounterThread(String name) {
         super(name);
     } 

     public void run() {
         int i = 0;
         while(true)
         {
           i ++ ;
           System.out.println(this.getName() + " count: " + i);
           try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace(); 
            }
         }
     } 

     public static void main(String[] args) {
         Thread t1 = new CounterThread("thread 1");
         Thread t2 = new CounterThread("thread 2");
         t1.start(); 
         t2.start(); 
     } 
 }

ProducerConsumer.java源代码

//生产者-消费者
class  ProducerConsumer
{
    public static void main(String[] args) 
    {
        Warehouse w = new Warehouse();
        ProducerTask p = new ProducerTask(w);
        ConsumerTask c = new ConsumerTask(w);
        Thread tp = new Thread(p);
        Thread tc = new Thread(c);
        tp.start();
        tc.start();
    }
}

//产品
class Product
{
    private int id;

    Product(int id){
        this.id = id;
    }

    public String toString(){
        return "Product :" + id;
    }
}

//产品仓库
class Warehouse
{
    Product productStore[] = new Product[6];
    int index = 0;

    /** 
    * 生产方法.
    * 该方法为同步方法,持有方法锁
    * 首先循环判断满否,满的话使该线程等待,释放同步方法锁,等待消费;
    * 当不满时首先唤醒正在等待的消费方法,但是也只能让其进入就绪状态,
    * 等生产结束释放同步方法锁后消费才能持有该锁进行消费
    * 
    * @param p 产品
    * @return  
    */ 

    public synchronized void push(Product p){
        try{
            while(index == productStore.length){
                System.out.println("[Produce] Warehouse: Full - wait consumer");
                this.wait();
            }
            this.notify();

        }catch(InterruptedException e){
            e.printStackTrace();
        }catch(IllegalMonitorStateException e){
            e.printStackTrace();
        }   

        productStore[index] = p;
        index++;
        System.out.println("[Produce]:" + p + " [Total]: " + index);

    }

    /** 
    * 消费方法
    * 该方法为同步方法,持有方法锁
    * 首先循环判断空否,空的话使该线程等待,释放同步方法锁,允许生产;
    * 当不空时首先唤醒正在等待的生产方法,但是也只能让其进入就绪状态
    * 等消费结束释放同步方法锁后生产才能持有该锁进行生产
    * @return  
    */ 
    public synchronized Product pop(){
        try{
            while(index == 0){
                System.out.println("[Consume] Warehouse: Empty - wait producer");
                this.wait();
            }
            this.notify();

        }catch(InterruptedException e){
            e.printStackTrace();
        }catch(IllegalMonitorStateException e){
            e.printStackTrace();
        }

        index--;
        System.out.println("[Consume]: " + productStore[index] + "[Total]: " + index);
        return productStore[index];
    }
}

/**
 * 
 * 生产者线程
 *
 */
class ProducerTask implements Runnable
{
    Warehouse wh;
    ProducerTask(Warehouse w){
        this.wh = w;
    }

    //生产过程
    public void run(){
        for(int i = 0;i < 20;i++){
            Product m = new Product(i);
            wh.push(m);
            try{
                Thread.sleep((int)(Math.random()*500));
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

/**
 * 
 * 消费者线程
 *
 */
class ConsumerTask implements Runnable
{
    Warehouse wh = null;
    ConsumerTask(Warehouse w){
        this.wh = w;
    }

    //消费过程
    public void run(){
        for(int i = 0;i < 20;i++){
            wh.pop();
            try{
                Thread.sleep((int)(Math.random()*1000));
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
}

功能测试

在Eclipse中执行“Run As TiJOS Application”,应用运行后,在Eclipse中打印日志如下:

log

总结

经过笔者的亲自测试,钛极OS(TiJOS)物联网操作系统支持多线程,并完全遵循Java技术规范标准,多线程及事件的支持为复杂应用的开发提供了完美的解决方案,这也是目前市面上已有物联网操作系统所不具备的特性,到目前笔者玩过的物联网操作系统,如:使用Lua编程的NodeMCU,使用JS编程的Ruff等,这里就不一一介绍,以上都对多线程未做完美支持。

猜你喜欢

转载自blog.csdn.net/crashMaker/article/details/78815545
今日推荐