Tag Archives: tips

JFreeChart中的TimeSeries可能导致内存泄露

前段时间说到现在的项目里在用JFreeChart。昨天晚上走之前没把客户端关掉,今天中午到公司发现程序已经崩了,狂抛OutOfMemoryError。用NB的profiler跟了一下,发现是JFreeChart或者说是没有正确使用JFreeChart的TimeSeries导致的后果 TimeSeries有一个方法setMaximumItemAge: public void setMaximumItemAge(long periods) public void setMaximumItemAge(long periods) 该方法文档如下: Sets the number of time units in the ‘history’ for the series. This provides one mechanism for automatically dropping old data from the time series. For example, if a series contains daily data, you might set the history count to 30. Then, when you read more »

如何cancel一个swing worker(续)

上一次谈到如何去cancel一个swing worker,今天在代码里又出了问题:即使使用swingWorker.cancel(true)仍然无法在sleep时中止线程。追了一下代码,最后在javax.swing.ImageIcon类里找到了原因: protected void loadImage(Image image) { synchronized(tracker) { int id = getNextID(); tracker.addImage(image, id); try { tracker.waitForID(id, 0); } catch (InterruptedException e) { System.out.println(“INTERRUPTED while loading Image”); } loadStatus = tracker.statusID(id, false); tracker.removeImage(image, id); width = image.getWidth(imageObserver); height = image.getHeight(imageObserver); } } 其中第7行会抛出InterruptedException,而在第10行捕捉了该异常,导致InterruptedException无法抛到我的代码里。很典型的“eat-up exception”的例子。解决该问题可以在初始化ImageIcon前sleep一下,比如sleep(5),让interrupted状态在sleep中触发InterruptedException

小心Derby的ResultSet陷阱

Derby作为一个纯Java实现的嵌入式DB一直很受Java社区的欢迎,在我们的项目中也用到了。但昨天晚上遇到的一个问题让我对Derby相当失望——Derby实现的ResultSet会将列名中的表名抹掉 具体代码: SQL: SELECT staff.staffid FROM staff WHERE … JDBC: ResultSet rs = stmt.executeQuery(…); if(rs.next()){     String id = rs.getString("staff.id"); } ResultSet rs = stmt.executeQuery(…); if(rs.next()){ String id = rs.getString(“staff.id”); } 运行时抛出异常: Caused by: java.sql.SQLException: 列“staff.staffid”未找到。     at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)     at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)     at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)     at read more »

如何使用反射调用静态方法

其实很简单,真的,将invoke(Object o, Object… params)的第一个参数传入null即可

如何cancel一个swingworker

最近在项目里一直在用jdesktop的swingworker(已经合入JDK 6),是个不错的swing线程库。今天需要在界面里cancel一个swingworker。研究了一下午,结合swingworker的文档、源代码和自己的代码实验,以下是两种安全cancel一个swingworker的方法 方法一,使用isCancelled: protected Object doInBackground() throws Exception {     while (!isCancelled()) {        …     }       return null; } protected Object doInBackground() throws Exception { while (!isCancelled()) { … } return null; } 需要cencel时调用swingworker.cancel(false),不中断线程,只置cancel标记。 方法二,使用sleep: protected Object doInBackground() throws Exception {     while (running) {   read more »

JScrollBar的unitIncrement和blockIncrement

这两天在写swing的时候遇到一个问题,JScrollPane在相应鼠标滚轮的时候很慢,滚了一大段才移了一点点,给人的感觉就是鼠标很“硬”。刚才查了一下javadoc,看到JScrollbar有个方法:setUnitIncrement(int) 和 setBlockIncrement(int)。前一个是设置点击上下箭头的移动距离(也包括滚轮滚动),后一个是单击滚动条上空白处的移动距离,单位为像素。输出了一下unitIncrement的默认值,竟然是1 -_-|||。手动设为10,感觉鼠标终于灵活了……具体代码: jScrollPane.getVerticalScrollBar().setUnitIncrement(10); jScrollPane.getVerticalScrollBar().setUnitIncrement(10); 如果遇到和我相同问题的可以用上面的方法试一下。有点不爽的就是没有一个类似UIManager的全局变量可以设置,只能在各个JScrollBar上单独设置,有点麻烦。

在Linux下设置ntp服务

节省时间,什么是ntp在这里就不解释了,下面就大概讲一下在Linux下如何配置、启动ntp服务。当然,前提是在系统中已经安装了ntp服务 首先手动同步一下时间: # ntpdate  222.73.214.125 # ntpdate  222.73.214.125 其中222.73.214.125是ntp服务器的IP,这里可以找到一堆的ntp服务器,挑一个国内的就行了 同步后使用date命令验证当前时间,如果不对需要配置系统时区。然后修改/etc/ntp.config文件,将刚才选择的ntp服务器添加进去,语法为: server 222.73.214.125 server 222.73.214.125 最后在系统服务中启动ntp: # /etc/init.d/ntpd start # chkconfig –level 35 ntpd on # /etc/init.d/ntpd start # chkconfig –level 35 ntpd on

如何在Java的enum中使用annotation

刚才在写一个方法的时候试图在enum上使用annotation: public enum DataKey {     @Incremental     @FromProbe     @Transient(replacePolicy = ReplacePlolicy.REPLACE_IF_LATER_THAN)     VISIT_COUNT } public enum DataKey { @Incremental @FromProbe @Transient(replacePolicy = ReplacePlolicy.REPLACE_IF_LATER_THAN) VISIT_COUNT } 然后在merge的时候使用annotation: if (key.getClass().isAnnotationPresent(Transient.class)) {     … } if (key.getClass().isAnnotationPresent(Transient.class)) { … } 结果不进if,debug时发现key(DataKey的对象)的类型是DataKey(其实也挺顺理成章的),于是使用如下代码: if (DataKey.class.getField(key.name()).isAnnotationPresent(Transient.class)) {     … } if (DataKey.class.getField(key.name()).isAnnotationPresent(Transient.class)) { read more »

无觅相关文章插件,快速提升流量