crossoverjie / jcsprout Goto Github PK
View Code? Open in Web Editor NEW👨🎓 Java Core Sprout : basic, concurrent, algorithm
Home Page: https://crossoverjie.top/JCSprout
License: MIT License
👨🎓 Java Core Sprout : basic, concurrent, algorithm
Home Page: https://crossoverjie.top/JCSprout
License: MIT License
Do not repeat collection.
之前的面试中被问过这两个,其中第一个还被某桔场在线结对编程写过,if necessary,今天整理好算法后发pr
ArrayList/Vector
在该章节的ArrayList
小节的 扩容 部分,为什么不使用ArrayList
的源码呢?
其实扩容最终调用的代码:
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
如上的扩容因子为3/2
,源码里面是为:
int newCapacity = (oldCapacity * 3)/2 + 1;
在源码里找了一下,没有找到grow()
方法,所以应该是你自己的实现吗?
即便扩容因子没有硬性规定...但是是不是遵循源码会好一些?
感谢~
https://github.com/crossoverJie/Java-Interview
README中数据结构与算法 -->二叉数中序遍历
看代码应该是层序遍历
Whether in an interview or in development, this is very useful.
LinkedList 底层分析 中的查询方法写着“使用二分查找”。严格来说,并不是二分查找,因为二分查找每一次都会减半搜索空间。(详见 https://zh.wikipedia.org/wiki/%E4%BA%8C%E5%88%86%E6%90%9C%E7%B4%A2%E7%AE%97%E6%B3%95)
优化 内存可见性的应用 例子程序
位置:https://github.com/crossoverJie/Java-Interview/blob/master/MD/Threadcore.md#双重检查锁的单例模式
双重检查锁的单例模式这部分
文中写到:有可能第二步在第三步之前被执行就有可能某个线程拿到的单例对象是还没有初始化的,以致于报错。
应该是第三步在第二步之前执行,导致其他线程拿到单利对象还没有初始化
其实现TwoThread.java类有可能打印出101,102
去掉奇偶线程中的下面这段代码
try {
//防止线程空转
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
会造成并不总是输出全部100个数字,会有卡住现象,并伴随CPU飙升,这是什么原因造成的呢
Java多线程中常见问题CAS算法跳转链接失效
你好,我正在学习JAVA中,在BinaryNode.java中写好那些函数的功能之后,怎样在主函数中实现一个实例呢?我不会写。
你好:
阅读了作者您的博客:https://crossoverjie.top/2018/08/29/java-senior/OOM-Disruptor/ 之后,自己也动手写了下单元测试,oom问题无疑,但是对于博客中有一段话存在不解:“我设置队列大小为 8 ,从 0~9 往里面写 10 条数据,当写到 8 的时候就会把之前 0 的位置覆盖掉,后面的以此类推(类似于 HashMap 的取模定位)。”;而我的单侧中,event数大于buffersize的时候,produce.publish方法回阻塞,我的单测地址:
sosojustdo/disruptor-learning@fb74612
在提交issue之前请回答以下问题,谢谢!
建议首先查看是否已经有类似的 Issues (提交时可删除该提示)
Latest
This documentation will be available on multiple languages. For the beginning English will be good in addition to Chinese
It is available on Chinese only
Walk through the text
None
文章中写道
【由此可以看出是使用二分查找来看 index 离 size 中间距离来判断是从头结点正序查还是从尾节点倒序查】
二分法查找的时间复杂度是logn,是个递归的过程。
但是,看代码中,本身数据结构是双向链表,只能从头往后,或者从后往前访问,所以,这个应该是指计算出来index属于上半区还是下半区,上半区就从头往后, 下半区就从后往前。 时间复杂度应该是n的。
JCSprout/src/main/java/com/crossoverjie/actual/TwoThread.java
这个交叉打印奇偶数的程序,取消防止线程空转的Thread.sleep(10),为了加大重现概率,适当调大打印上限(while (number.start <= 100) {改成while (number.start <= 100000) )
正常交叉打印奇偶数
打印途中卡住
如果将判断条件(private boolean flag = false)加上volatile修饰,则无论是否加上线程休眠,都不会卡死。理论上锁应该也有可见性和顺序性的效果,为何加上volatile的效果会不同?
public Object put(Object key, Object value) {
int hash = hash(key);
int index = hash % arraySize ;
Node currentNode = (Node) arrays[index] ;
if (currentNode == null){
arrays[index] = new Node(null,null, key, value);
//写入队列
QUEUE.offer((Node) arrays[index]) ;
sizeUp();
}else {
Node cNode = currentNode ;
Node nNode = cNode ;
//存在就覆盖
if (nNode.key == key){
cNode.val = value ;
}
while (nNode.next != null){
//key 存在 就覆盖 简单判断
if (nNode.key == key){
nNode.val = value ;
break ;
}else {
//不存在就新增链表
sizeUp();
Node node = new Node(nNode,null,key,value) ;
//写入队列
QUEUE.offer(currentNode) ;
cNode.next = node ;
}
nNode = nNode.next ;
}
}
return null ;
}
其中while (nNode.next != null)这个条件在key第一次冲突的条件时是不是不满足哇?求解惑,^_^
在提交issue之前请回答以下问题,谢谢!
建议首先查看是否已经有类似的 Issues (提交时可删除该提示)
Clone: Invalid path: MD/TCP:IP.md
smart git clone项目
smart git
version 18.1.3 #12142, installed: #12104
jdk 1.8.0_144-b01 (D:\smartgit\jre)
volatile keyword is very important in interview and development, we need to master it.
在提交issue之前请回答以下问题,谢谢!
建议首先查看是否已经有类似的 Issues (提交时可删除该提示)
上图为您ThreadPoolExecutor.md
中execute()
函数解释部分的内容,对照源码读到这里的时候产生了几点小疑问,希望您能帮我解答一下。
execute()
方法的comments中提到了So we recheck state and if necessary roll back the enqueuing if stopped ...(line 1347 ~ 1379)
因为我在代码中没有看到这个判断,所以有这个疑问;
else if
是和第三点的if
同级的条件语句,执行该语句的原因您指出为“在第三步的判断为非运行状态”。因为第三点的if
语句中包括两个条件,您默认线程是非运行状态的会执行该else if
语句,那会不会有当前线程仍是运行的状态但调用offer()
方法失败的情况呢?如果是入队失败的原因,新建线程调用addWorker()方法会将该任务作为第一个任务执行,这样会对现在线程池产生什么影响呢?还是这种可能性不存在呢?希望得到您的解答,THX~
在LRUCahce实现一中的remove方法有一个疑问:
首先通过Hash找到Key对应的数组位置,然后将其置为NULL
但是为什么会有一个QUEUE.poll() 方法,remove的Node不一定就是QUEUE的头结点啊
新增通知/等待机制实现交替打印奇偶数
是抽象类就会返回不能加载吗?如果父类能加载,那子类拿到父类加载的结果之后又要做什么?
为什么需要 这段代码
初始的时候minMoney =1;minMoney ==maxMoney的时候,肯定是最后一个红包了,感觉不需要这段代码
自己写代码的时候想不到实际场景,请LZ指点一二
加油,你是最棒的
Spring AOP 的实现原理、SpringBoot 启动过程与Tomcat 类加载机制这三方面的内容是否会继续完善呢~
通过HashMap.forEach遍历,其实是遍历的HashMap的变量table。
ConcurrentHashMap 实现原理还是要备注下版本吧,jdk8实现方式就发生变化了。
RT
如果在 setNX 之后释放锁的时候挂掉了那么这个 key 将永远挂起,等到超时之后自动删除,如果在超时时间之内这个操作还没有完成就容易发生并发问题
可以在tryLock的生成一个随机数并setNX。release的时候,比对这个随机数是否一致,一致的情况才去delete,这样在某个进程获取lock后,直到 lock expire,再去delete时不会误删其他进程获取的lock。
least recent used.
以上内容 base JDK1.7,1.8 的实现更加复杂但是原理类似,建议在 1.7 的基础上查看源码。
1.8中的ConcurrentHashMap主要是基于CAS+Synchronized实现?
想请问下作者用的是什么markdown编辑器
题目:
一个“.”
代表一个任意字母。
注意事项:可以假设所有的单词只包含小写字母“a-z”
样例:
addWord(“bad”);
addWord(“dad”);
addWord(“mad”);
search(“pad”); // return false;
search(“bad”); // return true;
search(“.ad”); // return true;
search(“b..”); // return true;
如果有并发的情况下,addWord()
怎么处理?
/**
* Creates and enqueues node for current thread and given mode.
*
* @param mode Node.EXCLUSIVE for exclusive, Node.SHARED for shared
* @return the new node
*/
private Node addWaiter(Node mode) {
Node node = new Node(Thread.currentThread(), mode);
// Try the fast path of enq; backup to full enq on failure
Node pred = tail;
if (pred != null) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
enq(node);
return node;
}
在 addWaiter() 这个方法中,JDK 为何要先用一次 CAS 尝试将新的 node 添加到队尾,而不直接调用 enq() 方法来入队呢? enq() 方法的实现也是使用 CAS 操作入队,自旋至入队成功才会退出。
/**
* Inserts node into queue, initializing if necessary. See picture above.
* @param node the node to insert
* @return node's predecessor
*/
private Node enq(final Node node) {
for (;;) {
Node t = tail;
if (t == null) { // Must initialize
if (compareAndSetHead(new Node()))
tail = head;
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}
并且两个方法存在一部分相同的代码,这样设计不会冗余吗?希望得到您的解答,谢谢~
在提交issue之前请回答以下问题,谢谢!
建议首先查看是否已经有类似的 Issues (提交时可删除该提示)
md hashset linked to hashmap
在JVM->Java 运行时内存划分一文中,提到方法区也属于老年代,这个不是在hotspot8之前称为永久代吗?
肯定是遍历购物车产品插入订单详情了,arraylist遍历只能加锁实现了吗?我想用消息队列插入,但是算总价还是需要遍历
也许和你的数据结构有关,如果你的链表保存了链表的length,可以遍历一遍,没遍历一个元素,size加一,size大于length的时候,即有环,只需遍历length+1次。如果采取快慢指针,大概率需要循环1.5length以上次数
offer
😭建议:多来点干货。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.