Java
文章平均质量分 81
DrifterJ
You must be willing to let go of the life you have planned so as to have the life that is waiting for you. Like a drifter. This is the ultimate dream of my life. I will treasure her at the bottom of my heart.
展开
-
Java多线程编程--(1)初识线程
随着多核处理器的流行,多线程编程在我们现实编码中也变得越来越普遍。和其他编程语言一样,Java本身就内置了多线程的支持。一个线程就是程序的一个执行线索,如果你的程序只有一个main方法,则这个程序就只有一个执行线索,就是这个主线程。Java中通过类 java.lang.Thread 代表一个线程。Thread类中的run方法就是该线程运行的执行代码。Thread类中有一个java.lang.R原创 2012-07-23 08:56:54 · 1329 阅读 · 0 评论 -
通过自定义类型计算大数阶乘
今天和同事聊天,他说别人问他怎么算5000的阶乘?这个问题的难点有两个:1》 怎么存储这个大数据,因为经试验,Java中,long类型顶多支持到20的阶乘,超过就溢出了。2》 效率,这个可以在1》实现后,再考虑。和同事商讨一会,感觉必须得自定义类型了,先将原始代码贴出吧:package cn.test;import java.util.Arrays;/** *原创 2012-08-01 22:21:16 · 1773 阅读 · 1 评论 -
Actor模型的本质:究竟是要解决什么问题
Actor模型的本质已经被强调了无数遍:万物皆Actor。Actor之间只有发送消息这一种通信方式,例如,无论是管理员让工作者干活,还是工作者把成果交还给管理员,它们之间也要通过发送消息的方式来传递信息。这么做看似不如直接方法调用来的直接,但是由于大量的消息可以同时执行。同样,消息让Actor之间解耦,消息发出之后执行成功还是失败,需要耗费多少时间,只要没有消息传递回来,这一切都和发送方无关。Ac转载 2012-09-06 20:50:45 · 1992 阅读 · 0 评论 -
Annotation & Enum :Java中注解和枚举的使用
Java从5.0推出了很多新的东西,其中包含了标题所示两大利器。现在开始分别介绍一下:【Annotation】注解,用于为一些信息添加额外解释信息时使用,类似元数据的概念,用于描述数据的数据。这个东西在设计基础性框架的时候非常有用,你可以通过定义一些注解,让应用层程序员能够将自己的代码嵌入并运行在你的框架中。当没有Annotation的时候,框架通常需要应用层开发人员写配置文件,有了这个,原创 2012-08-06 16:18:27 · 11625 阅读 · 0 评论 -
5个让人激动的Java项目
每个 Java/JVM 领域的技术专家都应从那些项目中获益,他们中有 2011 在 SanJose 举办的 Jax 会议中提名为最好的技术产品奖的。我之所以选择他们是因为他们可以广泛用于一系列的项目中解决真实问题。作为开发者,我们也不得不在每天都讨论这些,那么 我们赶紧来看看究竟有哪些值得令人激动的项目。 1、Neo4j 图形数据库 Neo4j 是一个高性能转载 2012-09-13 14:39:59 · 1112 阅读 · 0 评论 -
Java多线程编程--(9)学习Java5.0 并发编程包--线程工具类
并发包java.util.concurrent不仅提供了线程池相关的接口和类,同时也提供了几个很有用的工具类!这里打算介绍4个,包括Semaphore,CyclicBarrier,CountDownLatch,Exchanger。在实际编码中,进行多线程的并发互斥和同步控制,使用这些工具类可以节省不少代码!【Semaphore】信号灯工具类。我们想想以前控制线程互斥的方式:synchron原创 2012-08-15 14:40:37 · 1850 阅读 · 0 评论 -
Java的一些注意--随意整理(一)
(一):Java中所有的private方法均可以被反射调用,调用方法为:Class cl = obj.getClass();Method method = cl.getDeclaredMethod("readResolve", null);method.setAccessible(true);method.invoke(obj, null);包括诸如虚拟机自动调用的方法,如wr原创 2012-09-24 14:35:19 · 834 阅读 · 0 评论 -
JSP页面中的几种注释
在JSP中的注释一共分为两种注释:显式注释:在HTML中存在注释“”隐式注释:可以使用java中的“//”、“/*….*/”,以及JSP中自己的注释:“”所谓是显式或隐式实际上就是指在查看源文件的时候的显示的代码。 <% // 注释,看不见 /* 注释,看不见 */%> 上面对注释说的很明白了不过个人见解,要弄懂这个问题首先要明白注转载 2013-01-22 17:11:14 · 13136 阅读 · 0 评论 -
RMI与RPC的区别
本质上,RMI与RPC都是远过程调用,在实际应用情景中,这些技术用于分布式的不同系统间进行功能调用!但如果追究起来,这两个概念还是有一些区别的,RPC是一个很早就出现的概念,而RMI通常来说就是指不同的java系统间的远程调用:远程对象方法调用并不是新概念,远程过程调用 (RPC-remote procedurecall) 已经使用很多年了。远程过程调用被设计为在应用程序间通信原创 2013-03-11 12:44:52 · 1353 阅读 · 0 评论 -
使用多线程的利弊
1. Amdahl定律 一个很简单的量化公式,用来计算一个程序中串行部分所占多少对程序加速比的影响或者用来计算计算机硬件配置中某个设备的速度提高能够将整个系统的速度提高多少。 假设一个串行程序执行的总时间为1,不能被并行化的部分占的时间比例为p,即串行化的部分为p,可并行化的时间为:1-p。如果用n个核用来加速的话,加速比为: 如果一个程序中只有50%转载 2013-05-22 13:02:03 · 1348 阅读 · 0 评论 -
Web网站性能测试工具
随着Web 2.0技术的迅速发展,许多公司都开发了一些基于Web的网站服务,通常在设计开发Web应用系统的时候很难模拟出大量用户同时访问系统的实际情况,因此,当Web网站遇到访问高峰时,容易发生服务器响应速度变慢甚至服务中断。为了避免这种情况,需要一种能够真实模拟大量用户访问Web应用系统的性能测试工具进行压力测试,来测试静态HTML页面的响应时间,甚至测试动态网页(包括ASP、PHP、JSP等)转载 2013-06-17 12:56:47 · 1081 阅读 · 0 评论 -
多线程写文件问题解答
问题 : 4个线程 线程1 打印A 线程2打印B 线程3打印C 线程4打印D 向文件1中写入ABCDABCDABCD... 向文件2写入BCDABCDA...... 向文件3写入CDABCDAB...... 向文件4写入DABCDABC...... 解答源码: package com.test.play;import java.io.Buffere原创 2013-08-01 14:28:46 · 1293 阅读 · 0 评论 -
两个 java 反编译器效果的小比较
最近在看一个补丁程序,因为没有源码,只能借助反编译工具了,对于java 而言,比较流行的反编译工具就是 jd-gui (java decompiler) 和 JAD, 因为 jd-gui 是图形化界面,所以我刚开始也选用的这个工具。反编译后,发现了如下结果 :这个 “jsr 46;” 不知道对应什么代码..... 换另一个编译器,jad, 我们目前只能通过命令行来使用原创 2013-11-19 14:51:04 · 2685 阅读 · 0 评论 -
JSP页面包含其他页面的三种方式及区别
今天在调试一个问题时,发现系统中一个页面无法正常显示,查看log,后台抛了这么个错:...................................................../_CategoriesDisplayMercado.java:861: code too largepublic void _jspService(HttpServletRequest request转载 2014-01-01 17:53:05 · 13944 阅读 · 0 评论 -
Java JDBC 中获取 ResultSet 的大小
当我们执行完一条Sql语句,获取到一个 ResultSet 对象后,有时我们需要立即知道到底返回了多少个元素,但是 ResultSet 并没有提供一个 size() 方法 or length 的属性,我们可以一个一个调用 next() 去累加计算,但是我们还有更好的办法 :PreparedStatement statement = ......;ResultSet results原创 2013-12-31 19:40:32 · 21614 阅读 · 2 评论 -
Java递归获取目录下所有特定后缀文件
一段非常简单的java代码,写成了一个小util :package com.util;import java.io.File;import java.io.FileFilter;import java.util.List;public class FileUtil { /** * provide two default file filters */ pub原创 2013-12-25 18:45:57 · 4401 阅读 · 0 评论 -
如何切换eclipse中svn用户
最近几次在项目中提交代码,用的是别的保存的帐号,项目经理今天强调要用自己的,搜到了如下内容,并且成功切换到了自己的帐号 :两种解决办法: 1. 通过删除SVN客户端的账号配置文件 1) 查看你的Eclipse中使用的是什么SVN Interface(中文:svn接口)windows > preference > Team > SVN 在右边转载 2014-03-11 12:00:43 · 31496 阅读 · 3 评论 -
JAVA开源数据库连接池C3P0简介
先看官方对C3P0的介绍:C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展,目前在Hibernate和Spring中,均有使用。工欲善其事必先利其器,先下相关的jar包吧,CSDN上一搜一大把,我的资源中也有。C3P0就是一个数据库连接池,我们先看一个巨简单的使用C3P0的例子吧,访问本机Oracle的一张表:package c原创 2012-08-16 16:29:41 · 5269 阅读 · 0 评论 -
Serializable接口的基本理解(2)
Effective Java一书在讲解Serializable接口时,开篇就强调这个接口要慎用!因为这个不需要实现类去实现任何方法的接口太容易被误用了。有时是我们有意为之,认为这样我们的类就会强大一些,有时我们基于某个平台或框架去写代码而被迫接受。但不论哪种,我们必须明白,实现这个接口是我们对他人,对未来的一个承诺!如果你的类实现了Serializable接口,其实默认你给你自己脑袋上套了个紧原创 2012-07-23 08:54:51 · 1034 阅读 · 0 评论 -
Java多线程编程--(2)Timer & TimerTask
Java中还封装提供了另一种非常有用的线程使用方法,这就是定时任务:Java在util包中提供了Timer类和TimerTask类。Timer类代表定时器,TimerTask是定时器任务。先讲一下比较简单的TimerTask类,这是一个抽象类,实现了Runnable接口,但并没有实现run方法。你的定时器任务就是继承TimerTask类,实现run方法即可。Timer类,定时器,其中定义了原创 2012-07-23 08:57:13 · 3115 阅读 · 0 评论 -
Java的代理和CGLib的比较及使用
代理模式是我们写代码过程中经常使用的模式,其通常由两部分构成,即代理类和委托类,他们对外暴露同样的业务方法(即实现相同的接口)。委托类是实际进行业务操作的类,代理类中会封装一个委托类实例,对业务方法的处理就是转调委托类实例相应方法。实际使用中,用户得到的是代理类实例,那为什么要在委托类上封装一层呢?我们先看一个实际生活的例子吧。我们以教育局为例吧,目前假设教育局就局长一人,局长有义务向社会公众原创 2012-08-07 17:02:06 · 4532 阅读 · 0 评论 -
Java多线程编程--(4)ThreadLocal的使用
ThreadLocal是Java从1.2版本就开始提供的一个类,顾名思义,就是线程级别的本地变量。目前在两种情况下采用了ThreadLocal类,以下分别进行介绍:1》 为多线程并发的互斥控制提供了另一种全新的解决思路。前面提到多线程对同一个资源进行访问的互斥是通过关键字synchronized进行的。但使用这个关键字有一个副作用,那就是性能的损耗并且会遏制虚拟机对字节码的优化处理。我们来看一原创 2012-07-26 18:44:43 · 2569 阅读 · 0 评论 -
Java多线程编程--(5)Java多线程互斥小例子
前几篇写了Java从出生就支持的多线程的一些技术,打算往后就写一下Java 5.0新推出的多线程编程相关的包和类,今天就写一个小例子作为老技术部分的小结吧。上题:“A线程循环n次输出语句,然后B线程循环m次输出语句,然后再由A线程循环n次输出语句,B循环m次输出语句.....如此反复50次即可。”package cn.test;public class ThreadExclusiv原创 2012-07-27 10:14:37 · 2340 阅读 · 1 评论 -
Java多线程编程--(3)线程互斥、同步的理解
多线程并行编程中,线程间同步与互斥是一个很有技巧的也很容易出错的地方。线程间互斥应对的是这种场景:多个线程操作同一个资源(即某个对象),为保证线程在对资源的状态(即对象的成员变量)进行一些非原子性操作后,状态仍然是正确的。典型的例子是“售票厅售票应用”。售票厅剩余100张票,10个窗口去卖这些票。这10个窗口,就是10条线程,售票厅就是他们共同操作的资源,其中剩余的100张票就是这个资源的一个原创 2012-07-24 23:24:41 · 16488 阅读 · 7 评论 -
Java多线程编程--(6)学习Java5.0 并发编程包--原子操作的一些类型
Java虽然语言本质就支持多线程并发,但支持力度一直不够,各类数据结构对并发的支持和处理效率也不好。所以从JDK 5.0 开始,Java推出了一批类来支持并发编程,统一放在java.util.concurrent 包及其子包下。今天开始我就介绍一下这些类及基本用法。Java5.0推出的这些并发编程相关的类,放置在3个包中:java.util.concurrent, java.util.conc原创 2012-07-28 22:41:37 · 1231 阅读 · 0 评论 -
Serializable接口基本理解(3)--序列化代理的使用
Effective Java一书一直在强调,你的类实现Serializable接口前,一定要进行价值评估,是否值得这么做,尤其你的类是一个需要进行大量继承扩展的类。因为实现了Serializable接口,就会大大的增加出错和出现安全漏洞的可能性。蓄意攻击者可以通过伪造修改字节码的方式使你的代码出现安全漏洞!从一个角度看, readObject方法是一个参数为“字节流”的构造函数,因此篡改了字节流,原创 2012-07-30 21:52:47 · 2431 阅读 · 0 评论 -
Java多线程编程--(10)学习Java5.0 并发编程包--线程工具类
上篇介绍了Semaphore 和 CyclicBarrier,这次说一下另外两个:CountDownLatch 和 Exchanger:【CountDownLatch 】倒计数器!用这个类,可以很好的模拟一个运动会场景:3个运动员1个裁判,运动员准备好后,裁判发令,然后运动员开跑,运动员结束后,告知裁判,等所有运动员都返回终点,裁判宣布比赛结果:package cn.test;原创 2012-08-15 16:40:36 · 2036 阅读 · 0 评论 -
Java多线程编程--(8)学习Java5.0 并发编程包--线程池、Callable & Future 简介
线程池是程序设计领域池化技术的一种应用(数据库连接池也是一个典型的池化技术),池化技术解决了大量的短请求带来的系统频繁创建对象对性能的影响。我们可以实现自己的线程池,但往往因为考虑不周全如容错性、自动扩容与缩容等导致性能不佳!Java5.0 内置了对线程池的支持,提供了性能比较优越的线程池相关的类!我们就来简单介绍一下如何使用这个线程池!【ExecutorService & Executors原创 2012-08-14 11:45:58 · 6364 阅读 · 4 评论 -
Java多线程编程--(7)学习Java5.0 并发编程包--Lock & Condition
今天我们来试验一下Java5.0 并发编程包的java.util.concurrent.locks子包。这里提供了一些对多线程进行互斥同步控制的类,用以取代之前我们一直使用的synchronized关键字,wait()、notify()、notifyAll()方法。【Lock】Lock是一个接口,和关键字synchronized的功能一致,对多线程进行互斥控制!其使用更加面向对象化!对互斥原创 2012-08-09 12:47:47 · 3308 阅读 · 1 评论 -
Java多线程编程--(11)学习Java5.0 并发编程包--支持并发的集合类
记得刚接触程序设计时,书上就说:程序 = 数据结构 + 算法! 算法通俗来讲就是一些分析模型的代码实现,比如我们的排序算法等。数据结构,就是组织存储数据的方式,Java语言通过集合类为我们提供了基本的数据结构的支持。这些类在java.util包中。比如顺序存储结构:ArrayList,LinkedList,Vector;集合:HashSet,TreeSet;Map:Hashtable,HashMa原创 2012-08-20 10:32:06 · 3428 阅读 · 1 评论 -
使用C3P0依赖的另一个jar包
java.lang.NoClassDefFoundError: com/mchange/v2/ser/Indirectorhttp://download.csdn.net/detail/drifterj/7433791原创 2014-06-01 20:58:23 · 16653 阅读 · 1 评论