
java
文章平均质量分 77
DViewer
求知者
展开
-
Updating a JAR File
The Jar tool provides a u option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files.The basic command for adding files has this format:转载 2017-11-08 15:03:34 · 338 阅读 · 0 评论 -
DBUtils 处理Oracle 日期类型
先建表create table dbuser.msg_table( MSG_ID INTEGER, SYNC_TIME DATE)使用DBUtils插入数据import java.sql.*;import org.apache.commons.dbutils.DbUtils;import org.apache.commons.dbutils.QueryRunner;原创 2016-08-12 20:23:41 · 3550 阅读 · 0 评论 -
Java 中的Unicode与PrintWriter
前面曾介绍过PrintStream,它可以将Java的基本数据类型等数据,直接转换为系统默认编码下对应的字符,再输出至OutputStream中。而这里要介绍的 java.io.PrintWriter在功能上与PrintStream类似,除了接受OutputStream实例作为变量之外,PrintWriter还可以接受Writer对象作为输出的对象。当原先是使用Writer对象在作字符处理,而现在转载 2016-08-11 17:54:29 · 659 阅读 · 0 评论 -
Java Regex - Matcher
The Java Matcher class (java.util.regex.Matcher) is used to search through a text for multiple occurrences of a regular expression. You can also use a Matcher to search for the same regular expressi转载 2016-08-22 14:32:15 · 769 阅读 · 0 评论 -
Java 8 functional interfaces
In the first part of this series, we learned that lambdas are a type of functional interface – an interface with a single abstract method. The Java API has many one-method interfaces such as Runnabl转载 2016-08-08 18:44:12 · 700 阅读 · 0 评论 -
java Future用法和意义一句话击破
在并发编程时,一般使用runnable,然后扔给线程池完事,这种情况下不需要线程的结果。 所以run的返回值是void类型。 如果是一个多线程协作程序,比如菲波拉切数列,1,1,2,3,5,8...使用多线程来计算。 但后者需要前者的结果,就需要用callable接口了。 callable用法和runnable一样,只不过调用的是call方法,该方法有一个泛型返回值类型,你可以转载 2016-07-30 21:39:33 · 839 阅读 · 0 评论 -
Read / Write Locks in Java
A read / write lock is more sophisticated lock than the Lock implementations shown in the text Locks in Java. Imagine you have an application that reads and writes some resource, but writing it is n转载 2016-07-20 11:48:32 · 458 阅读 · 0 评论 -
Semaphores in Java
Semaphores are a really simple concept, invented by the famous Dutch computer scientist Edsger Dijkstra. Basically a semaphore is a counter (integer) that allows a thread to get into a critical region转载 2016-07-18 16:40:36 · 451 阅读 · 0 评论 -
ConcurrentHashMap 的实现原理
概述我们在之前的博文中了解到关于 HashMap 和 Hashtable 这两种集合。其中 HashMap 是非线程安全的,当我们只有一个线程在使用 HashMap 的时候,自然不会有问题,但如果涉及到多个线程,并且有读有写的过程中,HashMap 就不能满足我们的需要了(fail-fast)。在不考虑性能问题的时候,我们的解决方案有 Hashtable 或者Collections.syn转载 2016-07-18 16:39:04 · 411 阅读 · 0 评论 -
非阻塞同步算法与CAS(Compare and Swap)无锁算法
锁(lock)的代价锁是用来做并发最简单的方式,当然其代价也是最高的。内核态的锁的时候需要操作系统进行一次上下文切换,加锁、释放锁会导致比较多的上下文切换和调度延时,等待锁的线程会被挂起直至锁释放。在上下文切换的时候,cpu之前缓存的指令和数据都将失效,对性能有很大的损失。操作系统对多线程的锁进行判断就像两姐妹在为一个玩具在争吵,然后操作系统就是能决定他们谁能拿到玩具的父母,这是很慢的。用转载 2016-07-18 16:30:08 · 618 阅读 · 0 评论 -
The Event-Dispatching Thread
Swing event-handling and painting code executes in a single thread, called the event-dispatching thread. This ensures that each event handler finishes executing before the next one executes and that转载 2016-07-18 14:37:42 · 563 阅读 · 0 评论 -
Class.getResourceAsStream() VS. ClassLoader.getResourceAsStream()
lass.getResource can take a "relative" resource name, which is treated relative to the class's package. Alternatively you can specify an "absolute" resource name by using a leading slash. Classloade转载 2016-10-08 20:19:23 · 727 阅读 · 0 评论 -
VisualVM: Monitoring Remote JVM Over SSH
VisualVM is a great tool for monitoring JVM (5.0+) regarding memory usage, threads, GC, MBeans etc. Let’s see how to use it over SSH to monitor (or even profile, using its sampler) a remote JVM either转载 2016-10-10 09:12:07 · 814 阅读 · 0 评论 -
关于Class.getResource和ClassLoader.getResource的路径问题
Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题。Class.getResource(String path)path不以’/'开头时,默认是从此类所在的包下取资源;path 以’/'开头时,则是从ClassPath根下获取;什么意思呢?看下面这段代码的输出结果就明白了:转载 2017-09-20 10:52:43 · 445 阅读 · 0 评论 -
从jar包中读取资源文件
Rather than trying to address the resource as a File just ask the ClassLoader to return an InputStream for the resource instead via getResourceAsStream:InputStream in = getClass().getResourceA转载 2017-09-18 20:35:41 · 627 阅读 · 0 评论 -
synchronized 的另个一重要作用:内存可见性
加锁(synchronized 同步)的功能不仅仅局限于互斥行为,同时还存在另外一个重要的方面:内存可见性。我们不仅希望防止某个线程正在使用对象状态而另一个线程在同时修改该状态,而且还希望确保当一个线程修改了对象状态后,其他线程能够看到该变化。而线程的同步恰恰也能够实现这一点。内置锁可以用于确保某个线程以一种可预测的方式来查看另一个线程的执行结果。为了确保所有的线程都能看到共享变量的最新值转载 2017-09-09 21:06:56 · 465 阅读 · 0 评论 -
Use varargs judiciously
Item 42: Use varargs judiciouslyvarargs的原理是调用时首先创建一个数组,然后把传入的参数放入数组,数组长度为参数个数一个方法需要0或多个同类型数据这个需求很常见,然而也有另一个很常见的需求:需要一个或多个同类型数据,此时单纯用varargs不太优雅,可以让方法先接受一个数据,在接受一个varargsvarargs最初是为了printf和反射设计的可以通转载 2017-08-24 10:35:52 · 452 阅读 · 0 评论 -
ArrayList removeRange方法分析
先给出removeRange(int fromIndex,int toIndex)方法的源码(这段代码是干什么的就不再解释了,源码分析一文中已经说明) 1 protected void removeRange(int fromIndex, int toIndex) { 2 modCount++; 3 int numMoved = size - toIndex; 4转载 2017-08-13 11:32:57 · 2269 阅读 · 0 评论 -
Java Thread join() 的用法
Java Thread中, join() 方法是让调用该方法的主线程执行run()时暂时卡住,等run()执行完成后, 主线程再调用执行join()后面的代码。示例:class ThreadTesterA implements Runnable { private int counter; @Override public void run() { while (counter转载 2016-10-30 23:22:08 · 612 阅读 · 0 评论 -
Google Gson - deserialize list<class> object
Method to deserialize generic collection:Type listType = new TypeTokenArrayListYourClass>>(){}.getType();ListYourClass> yourClassList = new Gson().fromJson(jsonArray, listType);Import : import j转载 2016-10-30 11:13:02 · 397 阅读 · 0 评论 -
Java中的Copy-On-Write容器
Copy-On-Write简称COW,是一种用于程序设计中的优化策略。其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容Copy出去形成一个新的内容然后再改,这是一种延时懒惰策略。从JDK1.5开始Java并发包里提供了两个使用CopyOnWrite机制实现的并发容器,它们是CopyOnWriteArrayList和CopyOnWriteArraySet。转载 2016-10-30 10:08:30 · 444 阅读 · 0 评论 -
SynchronizedList和Vector的区别
Vector是java.util包中的一个类。 SynchronizedList是java.util.Collections中的一个静态内部类。在多线程的场景中可以直接使用Vector类,也可以使用Collections.synchronizedList(List list)方法来返回一个线程安全的List。那么,到底SynchronizedList和Vector有没有区别,为什么转载 2016-10-30 10:00:40 · 5158 阅读 · 2 评论 -
Java实现定时任务的三种方法
在应用里经常都有用到在后台跑定时任务的需求。举个例子,比如需要在服务后台跑一个定时任务来进行垃圾回收(译者注:个人觉得用定时任务来跑垃圾回收不是很好的例子,从译者接触到的项目来看,比较常见的是用定时任务来进行非实时计算,清除临时数据、文件等)。在本文里,我会给大家介绍3种不同的实现方法:普通thread实现TimerTask实现ScheduledExecutorService实现转载 2016-10-10 15:28:04 · 1241 阅读 · 0 评论 -
SWING WORKER EXAMPLE
Java provides a neat way to carry out long lasting jobs without have to worry about the complexity of threads or lack of responsiveness in an application (by application we mean Swing applications). I转载 2016-07-18 14:27:23 · 535 阅读 · 0 评论 -
BlockingQueue
The Java BlockingQueue interface in the java.util.concurrent package represents a queue which is thread safe to put into, and take instances from. In this text I will show you how to use this Blocki转载 2016-07-28 13:11:25 · 368 阅读 · 0 评论 -
Java与字符编码问题详谈
一、字符集和字符编码方式 计算机只懂得0/1两种信号,而人类所使用的符号却无法尽数。要让计算机能够表示大千世界的符号,就一定要为每个符号指定一个唯一的整数。而这一套符号与整数的对应集合,就是我们经常谈论的字符集 。而且,每一个字符所对应的整数用多少个计算机字节表示,也就涉及到了字符编码方式 的问题。我们用比较规范的语言来定义这两个概念:(1) 字符集:抽象字符集合和整数集合之间的映转载 2016-07-05 23:09:00 · 1818 阅读 · 0 评论 -
java日志,需要知道的几件事
如果对于commons-loging 、log4j 、slf4j 、LogBack 等都已经非常清楚了,可以忽略本文。几次解决日志冲突问题时对这几个概念的简单总结,希望对这块基础没有理解透的同学能有所帮助,当然如果对这块有更深刻理解的同学,也贡献出自己的知识和见解。一、 概念Commons-logging : apache最早提供的日志的门面接口。避免和具体的日志方案直接耦合。类似转载 2016-01-08 23:02:16 · 435 阅读 · 0 评论 -
理解类加载器:j2ee 环境下的 log4j
类加载器。顾名思义,表示在java虚拟机中加载calsses.在我们的class执行和被访问之前。它必须通过类加载器加载使之有效。给定一个class名字,类加载器会定位class并且将它加载到java虚拟机。但是类加载器本事就是class。这就带来一个问题。是谁来加载这些类加载器呢。 当你运行一个java程序。(例如在命令行内输入java命令),它就会执行并且启动一个本地的native ja转载 2016-01-08 22:59:31 · 942 阅读 · 0 评论 -
JVM 类加载器介绍及其父亲委托机制 Parent Delegation
1、类加载器:类加载器用来把类加载到Java虚拟机中。从JDK1.2版本开始,类的加载过程采用父亲委托机制,这种机制能更好的保证Java平台的安全。在此委托机制中,除了Java虚拟机自带的根类加载器之外,其余的类加载器都有且只有一个父加载器。当Java程序请求加载器loader1加载Sample类是,loader1类首先委托自己的父加载器去加载Sample类,若父加载器能加载,则由父加载器转载 2016-01-08 22:57:44 · 750 阅读 · 0 评论 -
How to set field values using Java reflection
In the Java reflection API, the java.lang.reflect.Field class represents a generic field in a class or interface, and allows to retrieve programmatically field information, such as name, type, or anno转载 2015-12-18 18:28:22 · 684 阅读 · 0 评论 -
Looking "Under the Hood" with javap
There's a utility that is included with the Java Development Kit that I find is seldom used but can be remarkably handy. In fact, I almost never used it until it came time for me to study for the SCJP转载 2015-11-30 15:44:13 · 615 阅读 · 0 评论 -
Java中的几种引用类型:强引用、软引用、弱引用和虚引用
Java虽然有内存管理机制,但仍应该警惕内存泄露的问题。例如对象池、缓存中的过期对象都有可能引发内存泄露的问题。从JDK1.2版本开始,加入了对象的几种引用级别,从而使程序能够更好的控制对象的生命周期,帮助开发者能够更好的缓解和处理内存泄露的问题。这几种引用级别由高到低分别为:强引用、软引用、弱引用和虚引用。强引用:平时我们编程的时候例如:Object object=new Objec转载 2015-12-30 17:52:55 · 1091 阅读 · 0 评论 -
关于Java中的数据表示的一些讨论
1. 一个类型转换的例子 public class NarrowConversion {public static void main(String[] args) {int iValue=233;//强制把一个int类型的值转换成byte类型的值 byte bValue=(byte) iValue; //将输出-23 System.out.println(原创 2016-01-15 20:35:41 · 714 阅读 · 0 评论 -
使用SL4J桥接遗留的API(Bridging legacy APIs)
Often, some of the components you depend on rely on a logging API other than SLF4J. You may also assume that these components will not switch to SLF4J in the immediate future. To deal with such circum转载 2016-01-08 23:07:01 · 531 阅读 · 0 评论 -
jcl-over-slf4j log桥接工具简介
Java 界里有许多实现日志功能的工具,最早得到广泛使用的是 log4j,许多应用程序的日志部分都交给了 log4j,不过作为组件开发者,他们希望自己的组件不要紧紧依赖某一个工具,毕竟在同一个时候还有很多其他很多日志工具,假如一个应用程序用到了两个组件,恰好两个组件使用不同的日志工具,那么应用程序就会有两份日志输出了。 为了解决这个问题,Apache Commons Logging转载 2016-01-08 23:20:51 · 1602 阅读 · 0 评论 -
解析Unicode编码和Java char
Java的字符类型采用的是UTF-16编码方式对Unicode编码表进行表示。其中一个char类型固定2Bytes(16bits)。首先先介绍一下Unicode编码表和UTF-16编码算法: Unicode编码表的专业术语: 代码点 (code point): 指在Unicode编码表中一个字符所对应的代码值。如汉字“一”的代码点是U+4E00,英文字母“A”的代码转载 2016-07-05 23:07:26 · 591 阅读 · 0 评论 -
Using wait(), notify() and notifyAll() in Java: common problems and mistakes
Here are some limitations and common things that go wrong with the wait() / notify() paradigm in Java:You need to check the condition before entering wait(), else you may never be notified.Waking转载 2016-07-12 21:45:50 · 361 阅读 · 0 评论 -
Thread Signaling
The purpose of thread signaling is to enable threads to send signals to each other. Additionally, thread signaling enables threads to wait for signals from other threads. For instance, a thread B migh转载 2016-07-12 21:34:18 · 662 阅读 · 0 评论 -
Java序列化机制和原理
Java序列化算法透析 Serialization(序列化)是一种将对象以一连串的字节描述的过程;反序列化deserialization是一种将这些字节重建成一个对象的过程。Java序列化API提供一种处理对象序列化的标准机制。在这里你能学到如何序列化一个对象,什么时候需要序列化以及Java序列化的算法,我们用一个实例来示范序列化以后的字节是如何描述一个对象的信息的。序列化的必要性J转载 2016-07-11 21:47:19 · 398 阅读 · 0 评论 -
Java classpath and directories
The java classpath and directories has always been quite a sad story. Of course you were always able to pass a directory of classes to the jvm like this:java -classpath classes MainBut what you转载 2016-04-16 11:36:11 · 406 阅读 · 0 评论