2005年8月11日
Garbage Collection
State the behavior that is guaranteed by the garbage collection system and write code that explicitly makes objects eligible for collection.
1. Garbage collection is a mechanism for reclaiming memory from objects that are no longer in use, and making the memory available for new objects.
2. An object being no longer in use means that it can’t be referenced by any ‘active’ part of the program.
3. Garbage collection runs in a low priority thread. It may kick in when memory is too low. No guarantee.
4. It’s not possible to force garbage collection. Invoking System.gc may start garbage collection process.
5. There are no guarantees that the objects no longer in use will be garbage collected and their finalizers executed at all. gc might not even be run if the program execution does not warrant it. Thus any memory allocated during program execution might remain allocated after program termination, unless reclaimed by the OS or by other means.
6. There are also no guarantees on the order in which the objects will be garbage collected or on the order in which the finalizers are called.
7. Circular references do not prevent objects from being garbage collected.
8. We can set the reference variables to null, hinting the gc to garbage collect the objects referred by the variables. Even if we do that, the object may not be gc-ed if it’s attached to a listener. (Typical in case of AWT components) Remember to remove the listener first.
9. All objects have a finalize method. It is inherited from the Object class.
10. finalize method is used to release system resources other than memory. (such as file handles and network connections) The order in which finalize methods are called may not reflect the order in which objects are created. Don’t rely on it. This is the signature of the finalize method.
protected void finalize() throws Throwable { }
In the descendents this method can be protected or public. Descendents can restrict the exception list that can be thrown by this method.
11. finalize is called only once for an object. If any exception is thrown in finalize, the object is still eligible for garbage collection (at the discretion of gc)
12. gc keeps track of unreachable objects and garbage-collects them, but an unreachable object can become reachable again by letting know other objects of its existence from its finalize method (when called by gc). This ‘resurrection’ can be done only once, since finalize is called only one for an object.
13. finalize can be called explicitly, but it does not garbage collect the object.
14. finalize can be overloaded, but only the method with original finalize signature will be called by gc.
15. finalize is not implicitly chained. A finalize method in sub-class should call finalize in super class explicitly as its last action for proper functioning. But compiler doesn’t enforce this check.
16. System.runFinalization can be used to run the finalizers (which have not been executed before) for the objects eligible for garbage collection.
17. Local variables in methods go out of scope when the method exits. At this point the methods are eligible for garbage collection. Each time the method comes into scope the local variables are re-created.
18. Java uses a "mark sweep garbage collection algorithm, which traverses all the object references, marking any objects that are referred to and then garbage collecting any objects that are unmarked.
19. Java allows you to add a finalize() method to any class. The finalize() method will be called before the garbage collector sweeps away the object. In practice, do not rely on the finalize method for recycling any resources that are in short supply - you simply cannot know when this method will be called.
In the exam point of view :
You must be able to identify when an object is available for gc - you have either set it to null or you
have "redirected" the variable that was originally referring to it, so that it now refers to a different
object.
if you have a reference to an object say, A and then you pass A as an argument to some constructor -
new obj(A); - then even if you null your reference - A=null; - you can't say that A is available for
gc. So just follow the references and when they drop to zero you know its eligible/available for gc,
not that it will happen.
I can not full understand these statements which are above.
eg,1. obj = new Jo();
2. obj.doSomething();
3. obj = new Jo(); //Same as obj=null;
4. obj.doSomething();
Object a = new Object();
Object a=null; //Now the object created in 1st line is available for gc
Object a=new Object();
a = new Object(); //same.
// Now original object created in line 1 is available for gc and a new
object is now out there referenced by "a".
Aclass a = new Aclass(); // Object 1
Aclass b= new Aclass(); // Object 2
Aclass c = new Aclass(); // Object 3
a=b; // now we have no valid object reference to object "a" and it will be
// garbage collected sometime after this statement. But when?......
a=c;
c=null; // no garbage collection will be eligible since
// "a" still refers to Object 3
a=null; // now object "c" is eligible for gc since it always had a valid reference.
// Should "b" go out of scope; then we would possibly have eligibility for gc.
// there might still be other references to object "b" preventing the collection.
posted @
2005-08-11 13:46 hegen 阅读(234) |
评论 (0) |
编辑 收藏
2005年8月10日
Text Color Codes
In order to change text colors, you will need two things:
1. A command to change the text.
2. A color (hex) code.
Changing Full-Page Text Colors
You have the ability to change full-page text colors over four levels:
<TEXT="######"> -- This denotes the full-page text color.
<LINK="######"> -- This denotes the color of the links on your page.
<ALINK="######"> -- This denotes the color the link will
flash when clicked upon.
<VLINK="######"> -- This denotes the colors of the links after they have been visited.
These commands come right after the <TITLE> commands. Again, in that position they affect everything on the page. Also... place them all together inside the same command along with any background commands. Something like this:
< BODY BGCOLOR="######" TEXT="######" LINK="######" VLINK="######">
Please note: When you write these codes, you can write them with a # sign in front of the hex code or not. It used to be that that was required, but not any more. I still use it just because I started that way. You may want to just go with the six-digit code. Also make sure to place a space between each command and be sure to enclose it in quotation marks, like so:
<VLINK="#FFFFFF">
问题:
上面的那几个东西还没有搞懂.不知道什么用?
Changing Specific Word ColorBut I only want to change one word's color!
You'll use a color (hex) code to do the trick. Follow this formula:
<FONT COLOR="######">text text text text text</FONT>
It's a pain in the you-know-where, but it gets the job done. It works with all H commands and text size commands. Basically, if it's text, it will work.
posted @
2005-08-10 20:48 hegen 阅读(320) |
评论 (0) |
编辑 收藏
Please note:
"Aqua" and "Cyan" produce the same color: 00FFFF
"Fuchsia" and "Magenta" produce the same color: FF00FF
Aliceblue F0F8FF |
Antiquewhite FAEBD7 |
Aqua 00FFFF
|
Aquamarine 7FFFD4 |
Azure F0FFFF
|
Beige F5F5DC
|
Bisque FFE4C4 |
Black 000000 |
Blanchedalmond FFEBCD
|
Blue 0000FF |
Blueviolet 8A2BE2 |
Brown A52A2A
|
Burlywood DEB887 |
Cadetblue 5F9EA0 |
Chartreuse 7FFF00
|
Chocolate D2691E |
Coral FF7F50 |
Cornflowerblue 6495ED
|
Cornsilk FFF8DC |
Crimson DC143C |
Cyan 00FFFF
|
Darkblue 00008B |
Darkcyan 008B8B |
Darkgoldenrod B8860B
|
Darkgray A9A9A9 |
Darkgreen 006400 |
Darkkhaki BDB76B
|
Darkmagenta 8B008B |
Darkolivegreen 556B2F |
Darkorange FF8C00
|
Darkorchid 9932CC |
Darkred 8B0000 |
Darksalmon E9967A
|
Darkseagreen 8FBC8F |
Darkslateblue 483D8B |
Darkslategray 2F4F4F
|
Darkturquoise 00CED1 |
Darkviolet 9400D3 |
deeppink FF1493
|
Deepskyblue 00BFFF |
Dimgray 696969 |
Dodgerblue 1E90FF
|
Firebrick B22222 |
Floralwhite FFFAF0 |
Forestgreen 228B22
|
Fuchsia FF00FF |
Gainsboro DCDCDC |
Ghostwhite F8F8FF
|
Gold FFD700 |
Goldenrod DAA520 |
Gray 808080
|
Green 008000 |
Greenyellow ADFF2F |
Honeydew F0FFF0
|
Hotpink FF69B4 |
Indianred CD5C5C |
Indigo 4B0082
|
Ivory FFFFF0 |
Khaki F0E68C |
Lavender E6E6FA
|
Lavenderblush FFF0F5 |
Lawngreen 7CFC00 |
Lemonchiffon FFFACD
|
Lightblue ADD8E6 |
Lightcoral F08080 |
Lightcyan E0FFFF
|
Lightgoldenrodyellow FAFAD2 |
Lightgreen 90EE90 |
Lightgrey D3D3D3
|
Lightpink FFB6C1 |
Lightsalmon FFA07A |
Lightseagreen 20B2AA
|
Lightskyblue 87CEFA |
Lightslategray 778899 |
Lightsteelblue B0C4DE
|
Lightyellow FFFFE0 |
Lime 00FF00 |
Limegreen 32CD32
|
Linen FAF0E6 |
Magenta FF00FF |
Maroon 800000
|
Mediumauqamarine 66CDAA |
Mediumblue 0000CD |
Mediumorchid BA55D3
|
Mediumpurple 9370D8 |
Mediumseagreen 3CB371 |
Mediumslateblue 7B68EE
|
Mediumspringgreen 00FA9A |
Mediumturquoise 48D1CC |
Mediumvioletred C71585
|
Midnightblue 191970 |
Mintcream F5FFFA |
Mistyrose FFE4E1
|
Moccasin FFE4B5 |
Navajowhite FFDEAD |
Navy 000080
|
Oldlace FDF5E6 |
Olive 808000 |
Olivedrab 688E23
|
Orange FFA500 |
Orangered FF4500 |
Orchid DA70D6
|
Palegoldenrod EEE8AA |
Palegreen 98FB98 |
Paleturquoise AFEEEE
|
Palevioletred D87093 |
Papayawhip FFEFD5 |
Peachpuff FFDAB9
|
Peru CD853F |
Pink FFC0CB |
Plum DDA0DD
|
Powderblue B0E0E6 |
Purple 800080 |
Red FF0000
|
Rosybrown BC8F8F |
Royalblue 4169E1 |
Saddlebrown 8B4513
|
Salmon FA8072 |
Sandybrown F4A460 |
Seagreen 2E8B57
|
Seashell FFF5EE |
Sienna A0522D |
Silver C0C0C0
|
Skyblue 87CEEB |
Slateblue 6A5ACD |
Slategray 708090
|
Snow FFFAFA |
Springgreen 00FF7F |
Steelblue 4682B4
|
Tan D2B48C |
Teal 008080 |
Thistle D8BFD8
|
Tomato FF6347 |
Turquoise 40E0D0 |
Violet EE82EE
|
Wheat F5DEB3
|
White FFFFFF |
Whitesmoke F5F5F5 |
this color model is about html
posted @
2005-08-10 20:37 hegen 阅读(218) |
评论 (0) |
编辑 收藏
实例形式的变压器模式的类图定义如下。
图2. 实例变压器模式的类图定义 在图1可以看出,模式所涉及的成员有:
- 目标(Target)。这就是我们所期待得到的接口。目标可以是实的或抽象的类。
- 源(Adaptee)。现有需要适配的接口。
- 变压器(Adapter)。变压器类是本模式的核心。变压器把源接口转换成目标接口。 显然,这一角色必须是实类。
本模式的示范代码如下:
package com.javapatterns.adapter;
public interface Target {
/**
* Class Adaptee contains operation sampleOperation1.
*/
void sampleOperation1();
/**
* Class Adaptee doesn't contain operation sampleOperation2.
*/
void sampleOperation2();
}
代码清单4. Target的源代码。 package com.javapatterns.adapter;
public class Adapter implements Target {
public Adapter(Adaptee adaptee){
super();
this.adaptee = adaptee;
}
public void sampleOperation1(){
adaptee.sampleOperation1();
}
public void sampleOperation2(){
// Write your code here
}
private Adaptee adaptee;
}
代码清单5. Adapter的源代码。 package com.javapatterns.adapter;
public class Adaptee {
public void sampleOperation1(){}
}
代码清单6. Adaptee的源代码。 实例形式的变压器模式的效果 第一、 一个变压器可以把多种不同的源适配到同一个目标。换言之,同一个变压器可以把源类和它的子类都适配到目标接口。
第二、 与类形式的变压器模式相比,要想置换源类的方法就不容易。如果一定要置换掉源类的一个或多个方法,就只好先做一个源类的子类, 将源类的方法置换掉,然后再把源类的子类当作真正的源进行适配。
第三、 虽然要想置换源类的方法不容易,但是要想增加一些新的方法则方便得很。 而且新增加的方法同时适用于所有的源。
利用变压器模式指方为圆
中国古代有赵高指鹿为马的故事。鹿与马有很多相似之处,没见过的人本就分辨不清,指一指可能没什么大不了的。 指方为圆是否太过?非也。本例就是要指方为圆,需要的只是变压器模式这个魔术手指(Magic Finger)。
变压器模式在本例子的类图如下。
图6. 指方为圆的变压器模式类图 package com.javapatterns.adapter.cube2ball;
public class Cube
{
public Cube(double width)
{
this.width = width;
}
public double calculateVolume()
{
return width * width * width;
}
public double calculateFaceArea()
{
return width * width;
}
public double getWidth()
{
return this.width;
}
public void setWidth(double width)
{
this.width = width;
}
private double width;
}
代码清单8. Cube类的源代码。。 package com.javapatterns.adapter.cube2ball;
public interface BallIF
{
double calculateArea();
double calculateVolume();
double getRadius();
void setRadius(double radius);
}
代码清单9. BallIF接口的源代码。 package com.javapatterns.adapter.cube2ball;
public class MagicFinger implements BallIF
{
public MagicFinger(Cube adaptee)
{
super();
this.adaptee = adaptee;
radius = adaptee.getWidth();
}
public double calculateArea()
{
return PI * 4.0D * ( radius * radius );
}
public double calculateVolume()
{
return PI * 4.0D/3.0D * ( radius * radius * radius );
}
public double getRadius()
{
return radius;
}
public void setRadius(double radius)
{
this.radius = radius;
}
private double radius = 0;
private static final double PI = 3.14D;
private Cube adaptee;
}
代码清单10. MagicFinger类的源代码。 如果读者还记得中学的数学的话,应该可以看出,我们的指方为圆系统其实还是有道理的。它接受一个正方体, 返还此正方体的内切球,也就是能放进此正方体的最大的球。
显然,本例子里,我们使用的是实例形式的变压器模式。这样做的好处是,如果一旦我们决定不仅要支持正方体, 而且要支持四面体等多面体,我们可以使用同一个MagicFinger类,而不必针对每一个多面体都建立一个MagicFinger类。 这样也比较符合“魔术手指”这个名字。
posted @
2005-08-10 18:40 hegen 阅读(272) |
评论 (0) |
编辑 收藏