Code Monkey home page Code Monkey logo

java-internal-work's Introduction

Java 内功修炼

记录平时学习积累,以笔记的形式共享给大家,如果大家发现有什么问题,欢迎大家批评指出。谢谢!

主要有以下目的:

* 作为学习笔记,供自己以后复习
* 开源给大家,帮助有需要的基友,哈哈
* 共同探讨学习

Java基础笔记

Java多线程

JVM相关

数据结构与算法

  • 稀疏数组与队列
  • 链表
  • 递归
  • 排序算法

网络

Java 项目常用工具类

java-internal-work's People

Contributors

pangh-space avatar dependabot[bot] avatar

Watchers

James Cloos avatar  avatar

java-internal-work's Issues

消息队列(第一版)

package com.ph.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ShareData{
private int number = 0;
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();

public void increment() throws Exception{

    lock.lock();
    try{
        // 1. 判断
        while (number != 0){
            // 等待不能生产
            condition.await();
        }
        // 2. 干活
        number++;
        System.out.println(Thread.currentThread().getName() + "\t " + number);
        // 3.通知、唤醒
        condition.signalAll();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        lock.unlock();
    }



}

public void decrement() throws Exception{

    lock.lock();
    try{
        // 1. 判断
        while (number == 0){
            // 等待不能生产
            condition.await();
        }
        // 2. 干活
        number--;
        System.out.println(Thread.currentThread().getName() + "\t " + number);
        // 3.通知、唤醒
        condition.signalAll();
    }catch(Exception e){
        e.printStackTrace();
    }finally{
        lock.unlock();
    }



}

}

/**

  • @author panghui
  • @Version 1.0
  • @SInCE 2019-09-03
  • 一个初始值为零的变量,两个线程对其进行交替操作,一个加1一个减1,来5轮
    1. 线程-操作-资源类
    1. 判断-干活-通知
    1. 防止虚假唤醒机制

*/
public class ProdConsumer_TraditionDemo {

public static void main(String[] args) throws Exception{

    ShareData shareData = new ShareData();

    new Thread(()->{

        for (int i = 1; i <= 5; i++) {
            try {
                shareData.increment();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    },"AA").start();

    new Thread(()->{

        for (int i = 1; i <= 5; i++) {
            try {
                shareData.decrement();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    },"BB").start();

}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.