??xml version="1.0" encoding="utf-8" standalone="yes"?>
快速排序的法思想Q?br />
快速排序采用了分治的策略,原问题分解q个规模更小但结构与原问题相似的子问题。用递归Ҏ解决子问题,然后这些子问题的解l合为原问题的解?br />
快速排序的E序的一般过E可单描qCؓQ?br />
1.用统一的方法取?pivot(??br />
2.Ҏpivot 对已有数l进行排?br />
1) array[pivot]存储在tmp变量中,作ؓ比较基准?br />
以low、high分别从前向后、从后向前遍历数l?br />
2) 从后向前遍历Q找到第一个小于tmp的数Q将其移动到low的位|?br />
3) 从前向后遍历Q找到第一个大于tmp的数Q将其移动到high的位|?br />
4) 循环2?步,直到两指针重?即退出@环的条g?low >= high)Q将tmpUd到low(此时low与high重合)的位|,q将lowq回成ؓ新的pivot?br />
5) Ҏ4步返回的pivotQ对已有数组q行划分Q?~pivot-1 ?pivot+1 ?array.lenghtQ递归1?步。直到调用退出?br />
怿对于以上理论大家一定是耳熟能详了,但理解v来还是比较抽象,下面我就用Exceld单的描述一?快速排?的过E?br />
假设我们要写一个程序对已有数组q行排序Q简单v见,讑֮待排序数lؓ int[] array = { 4, 2, 1, 7, 5, 3, 8, 6 }。对其用快速排序算法进行排序,q程描述如下Q?br />
1.Ҏ已有待排序数l,取得pivotQ我在这里取得pivot的策略就??数组的第一个数Q这里即?4?br />
tmp = 4Q?br />
待排序数l:黄色底色表示pivot?br />
2.从后向前UdhighQ找到第一个小于tmp的数Q则该数移动到low的位|?br />
3.从前向后UdlowQ找到第一个大于tmpQ?Q的敎ͼ其Ud到high的位|?br />
4.然后再向前移动highQ试图找到第一个小于tmp(4)的数Q但没有扑ֈQ此时low与high重叠Q将tmp的值放入low的位|,q将low作ؓpivotq回?br />
Ҏ新的pivotq行递归调用Q将原待排序数组 分解Z块,index区间分别??Q??Q即以下两个子数l?br />
(q未新徏数组Q只是只xq个区间的数据,对其q行排序Q也是问题分解ؓ两个的子问题,但问题很cM?
q两个数l的排序q程q里׃MQ一Lq程?br />
下面来看看实现的代码Q与刚刚的过E描q是W合的:
试代码如下Q?br />
在网上查了一些关于此异常的解x案,基本上都是说要扩大linuxpȝ的文件句柄数限制?br />
但如果程序对于Socket、Stream{用后没能及时关闭的话Q扩大这个文件句柄数限制是治标不L的?br />
我先是在试环境扩大了linux的文件句柄数限制Q随后提高测试压力,q一D|间后发现q是会报q个异常?br />
Q中间也用lsof命o查看占用的文件句柄数Q不断的增加啊,心寒啊。)
现象??lsof -p *** 来查看,形如
java 22055 webapp 21w FIFO 0,6 29300342 pipe
java 22055 webapp 22r FIFO 0,6 29256305 pipe
在不断增加?br />
所以我果断对代码进行了排查。文件的IO操作、对数据库的操作Q看了都没有什么问题,
最后排查到由JavaE序去调用Shell脚本的代码,
代码写的q是很简单的Q看上去很清晎ͼ但是有明昄问题Q?/p>
q里的问题是 Ҏ没有在finally处做关闭处理。这个问题比较明显?br />
q有一个问题就是Process的用问题,
如果对Process的不熟悉的话Q可能会以ؓreturn proc.exitValue();之后׃事大吉了?br />
QexitValue()实很像是已l退Zq得到返回值的意思,估计是这个方法的名字qh了我们的开发h员。)
实际不然Q看Jdk的帮助文档可以发玎ͼ要通过destroy()来实现对子进E的销毁ƈ释放占用的File Descriptor?/p>
q个问题Q短旉的测试是不会有问题的Q但在投入生产后Q随着E序的长期运行,开发中的疏忽就会暴露了?br />
所以在对用的Ҏ拿不准的情况下,q是要多做调查,谨慎使用啊?br />
希望能让在排查类似问题的朋友注意Q如果你排查的代码中也存在Runtime.getRuntime().exec(cmd)q样的调用,那么L保那D代码没有问题?br />
本文为原创,Ƣ迎转蝲Q{载请注明出处BlogJava?/span>
想重现这个场景很ҎQ请看代码?/p>
执行此代码结果:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
问题出在Hugecȝ getSubString ҎQ它调用了StringcȝsubstringҎ?/span>
来让我们看看 substring cȝ实现吧,JDK源码如下Q?/span>
再让我们接下来看?new String(offset + beginIndex, endIndex - beginIndex, value); 的实玎ͼ
char[] value 数组被共享了?/span>
在我们的main函数里的循环中,每@环一ơ后Q我们希望Huge对象被回Ӟ且释攑֮占有的内存?/span>
但实际上 private String str = new String(new char[100000]); 占有的内存ƈ不会被释放?/span>
因ؓ 我们通过 Huge cȝ getSubString Ҏ得到?String 对象q存?存在?/span>handler的列表中)Q?/span>
它虽然是 length 只有 4 的对象,却n有着 char[100000] 的空间?/span>
解决ҎQ?/span>
可以修改Huge cȝ getSubString Ҏ如下Q?/span>
只要再套一个String的构造方法即可?/span>
至于Z么,看看JDK源码Q一看便知了。这里就不脓出来了?/span>
唉,以后写代码得多多心啊?/span>
http://topic.csdn.net/u/20090113/17/5abc9a50-64dd-4277-af73-e8d2b762a469.html
我脓q来Q自己做一下?/p>
题目1Q?One team tries to buy several bottle of drinks for 27 athletes. In ABC store three empty bottles can be exchanged
with one new bottle of drinks. Which of the following numbers is the minimal one that the team should buy for the 27
athletes?
{:自己实现了一个,比较单的?br />题目2Q?How can you create a listener class that receives events when the mouse is moved(single Answer)
A By extending MouseListener
B By implementing MouseListener
C By extending Mouse Motion Listener
D By implementing Mouse Motion Listener
E Either by extending Mouse Motion Listener or extending MouseListener
F Either by implementing Mouse Motion Listener or implementing MouseListener
题目3Q?You are assign the task of building a panel containing a TextArea at the top, a label directly below it, and a button
directly below the label. If the three components are added directly to the panel, which layout manager can be panel use to
ensure that the TextArea absorbs all of the free vertical space when the parel is resized?
题目4Q?Which are not containers in Java?(Multiple answer)
A ScollPane
B Canvas
C Scrollbar
D Applet
E Dialog
题目5QYou need to store elements in a collection that
guarantees that no duplicates are stored and all elements
can be access in nature order, which interface provides
that capability?
A java.uil.Map
B java.util.Collection
C java.util.List
D java.util.SortedSet
E java.util.SortedMap
F java.util.Set
题目6QWhat will happen when you attempt to compile and run this
code?
abstract class Base{
abstract public void myfunc();
public class Abs extends Base{
public static void main(String argv[])
{
Abs a = new Abs();
a.amethod();
}
public void amethod(){
System.out.println("A method");;
}
}
A The code will compile and run, printing out the words "A
method"
B The compiler will complain errors in Base class.
C The code will compile but complain at run time that the
Base class has none abstract methods.
D The compiler will complain errors in Abs class
题目7Q?Description
import java.util.*;
public class Test
{
private String value = null;
public Test(String v)
{
value = v;
}
public boolean equals(Test o)
{
if(o==this) return true;
if(o instanceof Test)
{
Test test =(Test) o;
return value.equals(test.value);
}
return false;
}
public static void main(String[] args)
{
List list = new ArrayList();
Test test1 = new Test("object");
Test test2 = new Test("object");
Test test3 = new Test("object");
Object test4 = new Test("object");
list.add(test1);
System.out.println(list.contains(test2));
System.out.println(test2.equals(test3));
System.out.println(test3.equals(test4));
}
}
题目8Q?
Which of the following is NOT true regarding to RuntimeException?
A RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtul
Machine.
B A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the
execution of the method but not caught.
C An RuntimeException is a subclass of Throwable that indicates serious problems that a reasonable application should not try
to catch.
D NullPointerException is one kind of RuntimeException.
题目9Q?Which of the following items demonstrates the key characteristics of Web 2.0
A Centralized
B User centered design
C Open
D Light Weight
题目10Q?When using the writeObject method to store the state of n object, how can you protect sensitive data from being
accessed in the stored object?
A Implement the Object as Exteralizable
B Declare the sensitive fields as private transient
C Declare the sensitive fields as static transient
D Declare the sensitive fields as protected transient
用Java设计一个程?实现一个字W串的对UC?如字W串"effeghg",?ff","effe","ghg"q三个对U字W?所以返?.
我实现的思\是遍历q个字符Ԍ
先选定头位|ؓW一个字W,然后从最后向前遍历这个字W串Q?/p>
头尾两个字符相同Q则取中间字W串Q进行递归?/p>
递归l束后得到结果,
l箋头向后?位,然后再从字符串最后向前遍历,
如此循环Q当于头Ӟ退出最外层循环Q输出结果?/p>
具体实现Q?/p>