首页 > 编程语言 >Java并发编程学习

Java并发编程学习

时间:2024-03-25 18:33:06浏览次数:38  
标签:Java String MyQueue 编程 private 并发 notify myQueue public

1.4 wait() 与 notify

例如消息队列生成和消费消息时候,可以用  wait (),  notify 通知对方

wait 和 notify 必须在 synchronized方法里。

1)  wait

释放当前线程的锁,线程进入等待状态,直到其他线程调用该对象的notify

2) notify

其他随机一个线程对应的等待状态解除。可以竞争当前锁。如果要解除所有解除线程的等待状态使用 notifyAll

package concurrent;

public class MyQueue {
    private String[] data = new String[10];
    private int size = 0;
    private int getInd = 0;
    private int putInd = 0;

    public synchronized void put(String ele) throws InterruptedException {
        if (size == 10) {
            wait();
            put(ele);
        } else {
            size++;
            data[putInd++] = ele;
            if (putInd == data.length) putInd = 0;
            System.out.println("produce " + ele);
            notify();
        }
    }

    public synchronized void get() throws InterruptedException {
        if (size == 0) {
            wait();
            get();
        } else {
            size--;
            String consume = data[getInd++];
            if (getInd == data.length) getInd = 0;
            System.out.println("consume " + consume);
            notify();
        }
    }
}
package concurrent;

public class ConsumerThread extends Thread{
    private final MyQueue myQueue;

    public ConsumerThread(MyQueue myQueue) {
        this.myQueue = myQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                myQueue.get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
package concurrent;

public class ProducerThread extends Thread {
    private final MyQueue myQueue;
    private int index = 1;

    public ProducerThread(MyQueue myQueue) {
        this.myQueue = myQueue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                if (index == 10) index = 1;
                String putStr = "data" + index;
                myQueue.put(putStr);
                index++;
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
package concurrent;

public class Main {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        new ConsumerThread(myQueue).start();
        new ProducerThread(myQueue).start();
    }
}

 

1.5

标签:Java,String,MyQueue,编程,private,并发,notify,myQueue,public
From: https://www.cnblogs.com/ylxn/p/18095043

相关文章

  • 【08每天十分钟,javase基础全搞定-秒懂参数传递问题上】
    参数传递问题基本类型按值传递引用类型按照内存地址进行传递。基本类型按值传递publicclassStudent{publicstaticvoidmain(String[]args){inta=10;tese(a);//相当于把10作为参数给了tese这个方法。System.out.println(a);//输......
  • 【白嫖无废话-秒懂javascript基础知识-免费持续更新】
    一、js须知的概念js是一门事件驱动型的编程语言,依靠事件去驱动,然后执行对应的程序。(一)JS中的事件在JS中有很多事件,其中有一个事件叫做:鼠标单击,单词:click。并且任何事件都会对应一个事件句柄叫做:onclick。注意:事件和事件句柄的区别是事件句柄是事件单词前加on而且事件......
  • 基于java的少儿编程网上报名系统—论文
    少儿编程网上报名系统的开发过程中,采用B/S架构,主要使用java技术进行开发,中间件服务器是Tomcat服务器,使用Mysql数据库和Eclipse开发环境。该少儿编程网上报名系统包括用户和管理员。其主要功能包括管理员:首页、个人中心、用户管理、课程类型管理、课程信息管理、课程购买管理......
  • 【05每天十分钟,javase基础全搞定-构造方法】
    构造方法格式:[修饰符]类名(形参列表){}构造方法也叫构造器,用于对象的初始化。构造器的名称应与它所在的类的名称一致构造方法的返回值跟普通方法有区别,普通方法的那种返回值,构造方法没有,也不能用void修饰符构造方法通过new关键字调用!构造方法的返回值类型是这个类本身。(......
  • JavaScript初识及基本语法详解
    JavaScript是一种轻量级的编程语言,它可以在网页中嵌入,用来控制网页的动态效果和用户交互。JavaScript是所有现代网页浏览器都支持的脚本语言,它可以让网页变得“活”起来,实现各种复杂的功能。JavaScript的基本语法JavaScript的语法基础与Java语言类似,但它是解释型语言,不需......
  • Java String类深入了解JDK各个版本进阶版本
    JavaString类深入了解JDK各个版本进阶版本一,底层类型在jdk11中Stringvalue存储字符串值是byte[]数组,String中存储字节码的是coder也是byte类型,因此String的底层数据存储类型成为了byte类型而在jdk8中String的Stringvalue存储字符串值是char[]数组,因此因此可......
  • Java版商城:Spring Cloud+SpringBoot b2b2c实现多商家入驻直播带货及 免 费 小程序商城
    随着互联网的快速发展,越来越多的企业开始注重数字化转型,以提升自身的竞争力和运营效率。在这个背景下,鸿鹄云商SAAS云产品应运而生,为企业提供了一种简单、高效、安全的数字化解决方案。鸿鹄云商SAAS云产品是一种基于云计算的软件服务,旨在帮助企业实现业务流程的自动化和优化。......
  • Java版企业电子招投标系统源代码,支持二次开发,采用Spring cloud技术
     在数字化时代,采购管理也正经历着前所未有的变革。全过程数字化采购管理成为了企业追求高效、透明和规范的关键。该系统通过SpringCloud、SpringBoot2、Mybatis等先进技术,打造了从供应商管理到采购招投标、采购合同、采购执行的全过程数字化管理。通过待办消息、招标公告、......
  • 卡码java基础课 | 18.洗盘子
    学习内容:栈的基本概念(空栈、栈顶、栈底)和特点(先入后出)入栈、出栈、获取栈顶元素和判断栈是否为空栈等基本操作Stack类的使用重点归纳:栈:后进先出,LIFO,lastinfirstout。使用方法:importjava.util.Stack。常用方法:isEmpty():判断栈是否为空栈,如果为空栈返回true,否则或者f......
  • java实现Array
    publicclassMyArray{privateint[]array;privateintsize;publicMyArray(intcapacity){this.array=newint[capacity];size=0;}publicvoidinsert(intelement,intindex)throwsException{//判断访问下标是否超出......