自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

那些年

在慢慢爬的程序员

  • 博客(40)
  • 资源 (3)
  • 收藏
  • 关注

原创 Java-LockSupport的小例子

内容:结合一篇博文和自己写的简单例子学习,当然还可以看文档public class TestLockSupport { public static class MyRunnable implements Runnable { private final Thread currentThread; public MyRunnable(Thread thread) {

2015-05-31 23:02:23 1308

转载 AbstractQueuedSynchronizer的介绍和原理分析

简介提供了一个基于FIFO队列,可以用于构建锁或者其他相关同步装置的基础框架。该同步器(以下简称同步器)利用了一个int来表示状态,期望它能够成为实现大部分同步需求的基础。使用的方法是继承,子类通过继承同步器并需要实现它的方法来管理其状态,管理的方式就是通过类似acquire和release的方式来操纵状态。然而多线程环境中对状态的操纵必须确保原子性,因此子类对于状态的把握,需要使用这个同

2015-05-29 14:35:40 1231

原创 LeetCode Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?题意:和上一题差别在于只能用

2015-05-29 10:14:47 613

原创 Java用读-写锁来包装Map

内容:利用ReentrantReadWriteLock来包装Map,从而使它能在多个读线程之间被安全分享,并且仍然能避免“读-写”或“写-写”冲突。记住重要的一点是:读-写锁实现的加锁策略中,允许多个读操作同时进行,但每次只允许一个写操作。public class ReadWriteMap { private final Map map; private final ReadWriteLo

2015-05-28 00:19:37 2977 1

原创 Java通过锁的顺序避免死锁

内容:通过获取锁的顺序来避免死锁。例如:银行账户转账问题,两个用户转账的话,如果采用一般的synchronized嵌套的话,容易造成死锁,现在我们通过类似哲学家问题的解决方案一样:先获取同一个锁,才有资格获取下一个。而判断是通过System.identityHashCode()来生成类的hashcode()的返回值作为唯一标识,相同的话,我们再加一把锁。class Account { pri

2015-05-26 19:51:12 3731

原创 LeetCode Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]题意:按照图生成矩阵。思路:类似杨辉三角的做法。

2015-05-26 13:04:54 649

原创 LeetCode Rotate Array

Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solutions as yo

2015-05-24 20:26:02 708

原创 LeetCode Populating Next Right Pointers in Each Node

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.

2015-05-20 23:33:43 601

原创 Java简单的UDP通讯例子

内容:简单的UDP通讯例子。Receiver:public class Receiver { public static void main(String[] args) { DatagramSocket ds = null; try { //UDP接收端 ds = new DatagramSocket(8080); //定义将UDP的数据包接收到什么地

2015-05-19 11:32:51 3705

原创 java不同类加载器对instanceof关键字运算的影响

内容:对于任意的一个类,都需要由加载它的类加载器和这个类本身一同确立其在Java虚拟机中的唯一性,每一个类加载器,都拥有一个独立的类空间。只要加载类的类加载器不同的话,那么这个两个类就必定不相等(包括equals()方法,instanceof()方法)。一个简单的例子说明:注意getResourceAsStream的应用:Class.getResourceAsStream(String pat

2015-05-18 15:54:30 3212 4

原创 Java一个简单的死锁例子

内容:一个简单的死锁例子,大概的思路:两个线程A和B,两把锁X和Y,现在A先拿到锁X,然后sleep()一段时间,我们知道sleep()是不会释放锁资源的。然后如果这段时间线程B拿到锁Y,也sleep()一段时间的话,那么等到两个线程都醒过来的话,那么将互相等待对方释放锁资源而僵持下去,陷入死锁。flag的作用就是让A和B获得不同的锁。public class TestDeadLock {

2015-05-16 20:07:53 4276

原创 Java关闭钩子的应用

内容:在很多实际的应用环境中,当用户关闭应用程序时,需要做一些善后清理工作。但问题是,用户有时并不会按照推荐的方法关闭程序,很多可能不做清理工作。关闭钩子是一种解决方案,确保无论用户如果关闭应用程序,清理代码总能够得到执行。我们通过向Runtime类注册关闭钩子:是一个已初始化但尚未启动的线程。虚拟机开始启用其关闭序列时,它会以某种未指定的顺序启动所有已注册的关闭钩子,并让它们同时运行。pub

2015-05-16 11:35:01 1202

原创 Java利用Callable、Future进行并行计算求和

内容:在Java中利用Callable进行带返回结果的线程计算,利用Future表示异步计算的结果,分别计算不同范围的Long求和,类似的思想还能够借鉴到需要大量计算的地方。public class Sums { public static class Sum implements Callable { private final Long from; private fi

2015-05-16 00:18:16 3318

原创 利用ObjectInputStream、ObjectOutputStream序列化多个对象

内容:序列化多个对象,利用一个容器存储你要序列化的多个对象。class Student implements java.io.Serializable{ private String name; public Student(String name) { this.name = name; } public String getName() { return na

2015-05-15 15:18:55 3390

原创 利用URLClassLoader加载两个位置的Class

内容:分别位于\myApp\WEB-INF\classes下的类和\webroot下的类,利用URL数组指定多个仓库位置加载。MyClassLoader:public class MyClassLoader { public static final String WEB_ROOT = System.getProperty("user.dir") + File.separat

2015-05-14 22:54:38 2194

原创 LeetCode Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non

2015-05-13 10:01:21 699

原创 Spring核心学习-AOP(7) 织入和代理

前导:开始学习Spring核心思想,通过一个山寨精简版Spring代码结合学习。AdvisedSupport - 保存AOP配置TargetSource - 保存被代理的数据AopProxy - 对代理对象做代理,在调用目标方法前先调用它.JdkDynamicAopProxy - 使用JDK动态代理对接口做代理Re

2015-05-12 13:48:36 1071

原创 Spring核心学习(6)引用ApplicationContext-包装Bean的初始化过程,对应用透明

前导:开始学习Spring核心思想,通过一个山寨精简版Spring代码结合学习。这是IOC的最终版本,在这里我们将BeanFactory包装了起来,让流程能真正的像Spring那样简单。我们新定义了一个接口去继承BeanFactory,然后通过组合的方式将AbstractBeanFactory添加进来,最后的ClassPathXmlApplicationContex

2015-05-11 17:03:23 685

原创 LeetCode Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1

2015-05-11 13:25:01 616

原创 Spring核心学习(5)将Bean注入Bean-解析依赖

前导:开始学习Spring核心思想,通过一个山寨精简版Spring代码结合学习。内容:1. BeanReference-保存Bean的引用。 2. getBean()中调用createBean()-lazy-init。这次我们用到了在Bean中注入Bean的情况,在这里我们再一次改写了AbstractBeanFactory,改写后的AbstractBeanFactory将多出一

2015-05-11 10:43:04 1104

原创 Spring核心学习(4)从XML中读取BeanDefinition-将代码变成配置

前导:开始学习Spring核心思想,通过一个山寨精简版Spring代码结合学习。内容:1. BeanDefinitionReader-配置读取者。 2. XmlBeanDefinitionReader-从XML中读取配置。 3. Resource-定位资源文件。这次将Bean的配置信息都放到了XML里,所以这里会有一个XML文件的读取,我们通过XmlBeanDefinition

2015-05-10 17:49:31 3491

原创 LeetCode Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \

2015-05-10 09:12:00 772

原创 Spring核心学习(3)为Bean注入属性

前导:开始学习Spring核心思想,通过一个山寨精简版Spring代码结合学习。内容:1.Propertyvalue-保存属性注入信息。2.AutowireCapableBeanFactory-可自动装配的BeanFactory。这里我们重新定义了BeanDefinition,增加了属性列表这个字段,我们将为bean附加额外的属性,所以我们又定了PropertyValu

2015-05-09 16:53:39 1599

原创 Spring核心学习(2)管理Bean的生命周期

前导:开始学习Spring核心思想,通过一个山寨精简版Spriing代码结合学习。内容:1. 抽象BeanFactory-面向接口更易拓展

2015-05-09 14:00:12 780

原创 LeetCode Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum

2015-05-09 13:50:31 582

原创 LeetCode Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.题意:求树的最低深度。思路:还是递归,重点是return 0的时候要

2015-05-08 11:17:45 505

原创 Spring核心学习(1)实现基本的容器-包括注入和获取功能

前导:开始学习Spring核心思想,通过一个山寨精简版Spriing代码结合学习。内容:1. BeanDefinition-保存Bean及配置信息 2. BeanFactory-对Bean进行管理。BeanDefinition:public class BeanDefinition { private Object bean; public BeanDefini

2015-05-07 21:06:00 993

原创 LeetCode Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe

2015-05-07 17:11:48 582

原创 LeetCode Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.题意:给一个有序的链表建立二叉搜索树。思路:这里是先递归建立左子树,然后链表跟着移动,左子树建立完了,此时节点就到了根了,接着建立右子树。/** * Definit

2015-05-06 11:13:05 596

转载 Java并发编程之ConcurrentHashMap

ConcurrentHashMapConcurrentHashMap是一个线程安全的Hash Table,它的主要功能是提供了一组和HashTable功能相同但是线程安全的方法。ConcurrentHashMap可以做到读取数据不加锁,并且其内部的结构可以让其在进行写操作的时候能够将锁的粒度保持地尽量地小,不用对整个ConcurrentHashMap加锁。ConcurrentHas

2015-05-05 21:15:21 408

原创 LeetCode Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.题意:将一个有序数组变成二叉搜索树。思路:简单的递归。/** * Definition for a binary tree node. * public class TreeNode {

2015-05-05 19:44:12 553

原创 利用FileChannel完成文件的读、写、复制

内容:通过NIO中的FileChannel完成文件的读、写、复制。public class NioFileCopy { private RandomAccessFile aFile = null; private FileChannel inChannel = null; private final ByteBuffer buf = ByteBuffer.allocate(1024);

2015-05-05 13:49:02 9478 1

原创 LeetCode Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},

2015-05-05 09:52:51 550

原创 利用NIO的Selector处理服务器-客户端模型

内容:这是一个简单的服务器-客户端模型,利用了NIO的Selector来处理多个管道。至于Selector的介绍看这里NIOServer:public class NIOServer { public static void main(String[] args) throws IOException, InterruptedException { Selector select

2015-05-04 23:49:47 1545

转载 Java NIO与IO

原文地址:http://tutorials.jenkov.com/java-nio/nio-vs-io.html作者:Jakob Jenkov   译者:郭蕾    校对:方腾飞当学习了Java NIO和IO的API后,一个问题马上涌入脑海:我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异、它们的使用场景,以及它们如何影响您的代

2015-05-03 15:48:23 427

原创 LeetCode Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.题意:中序和后序建树。思路:还是简单的递归构造。/** * Definition for a bina

2015-05-03 09:35:24 618

转载 Java线程(七):Callable和Future

接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果。        Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结果,并且无法抛出返回结果的异常,而Callable功能更强大一些,被线程执行后,可以返回值,这个返回值可以被Future拿到,也就是说,Future可以拿到异步执行

2015-05-02 11:06:29 412

原创 LeetCode Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.题意:二叉树前、中序构造出二叉树。思路:经典的题目。/** * Definition for a bin

2015-05-02 09:46:34 600

转载 监视器–JAVA同步基本概念

原文链接  作者:X Wang  译者:candy大学有一门课程叫操作系统,学习过的同学应该都记得,监视器是操作系统实现同步的重要基础概念,同样它也用在JAVA的线程同步中,这篇文章用一种类推的思想解释监视器”monitor”。1.什么是监视器监视器可以看做是经过特殊布置的建筑,这个建筑有一个特殊的房间,该房间通常包含一些数据和代码,但是一次只能一个消费者(thread

2015-05-01 19:13:35 1310

原创 LeetCode Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.题意:求树的高度。思路:就是递归。/** * Definiti

2015-05-01 11:00:35 566

Spring-AOP-JDK动态代理

Spring-AOP-利用java中的动态代理和Spring的拦截器做到AOP

2015-05-12

Spring核心学习IOC部分

Spring核心学习IOC部分:从最简单的BeanFactory开始一步步完善类似Spring的功能

2015-05-11

intellij idea 快捷键

intellij idea 常用的快捷键

2014-05-02

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除