galen wang

Greenhorn
+ Follow
since Sep 21, 2001
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by galen wang

Hi Muhammad,
please check
http://java.sun.com/j2se/1.4/docs/api/index.html -> Class FileOutputStream
in the Constructor Summary, there are five constructors:
--------------------------------------------------------
FileOutputStream(File file)
FileOutputStream(File file, boolean append)
FileOutputStream(FileDescriptor fdObj)
FileOutputStream(String name)
FileOutputStream(String name, boolean append)
--------------------------------------------------------
click FileOutputStream, you will see
--------------------------------------------------------
FileOutputStream
public FileOutputStream(File file,
boolean append)
throws FileNotFoundExceptionCreates a file output stream to write to the file represented by the specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument.
If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

Parameters:
file - the file to be opened for writing.
append - if true, then bytes will be written to the end of the file rather than the beginning
Throws:
FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
Since:
1.4
See Also:
File.getPath(), SecurityException, SecurityManager.checkWrite(java.lang.String)
--------------------------------------------------------
galen
Hi Guoqiao,
srry to bother you but owing to java's inperfect, growing and updating, modify our mind to synchronize with it seems unavoidable. for another example the mock-exam-01-15:


Select valid contructors for FileOutputStream.
(Select four correct answers)
-----------------------------------------------------------------------
-----------------------------------------------------------------------
A: FileOutputStream(File file)
B: FileOutputStream(File file, boolean append)
C: FileOutputStream(FileDescriptor fd)
D: FileOutputStream(FileDescriptor fd, boolean append)
E: FileOutputStream(String fileName)
F: FileOutputStream(String fileName, boolean append)


the answer and explanation is


Answers:
: A C E F
Explanation:
: Please refer to Java API for the details.


but from the API of JavaTM 2 Platform Std. Ed. v1.4.0, there is a FileOutputStream(File file, boolean append) existing. so the answers should include B.
it's annoy but ...
galen
mock-exam-02-38:

-----------------------------------------------------------------------
A: The code does not compile due to line //1.
B: The code does not compile due to line //2.
C: The code does not compile due to line //3.
D: The code compiles and runs with output:
first
second
third

</BLOCKQUOTE>
I think the correct answer is A.
that is because the class "Nested" is a top-level nested class (it's static), generally we can use below three ways to call the methord.
new Test038().Nested.method("by instance of Test038");
new Test038.Nested().method("by instance of Nested");
Test038.Nested.method("direct by class name");
but the answer and explanation is


Answers:
: B
Explanation:
: //2 tries to access Nested as if it is a variable
of Test038, which is not the case.


I can't agree with that and try to compile and run it.
the compile result is
Test038.java:5: qualified new of static class
new Test038().new Nested().method("first"); //1

then i modified the
new Test038().new Nested().method("first"); //1 => new Test038.Nested().method("first") //1;
it compiles and runs. the output is
first
second
third
wath's wrong in the Test038's answer?
I found that different java vesion will bring different result. (above I use jdk1.4.)
I tried jdk1.2.2, to the origin code the compile result is
Test038.java:7: No variable Nested defined in class Test038.
new Test038().Nested.method("second"); //2

if I commended this line then the compile was ok but the run result is
Exception in thread "main" java.lang.VerifyError: (class: Test038, method: main signature: ([Ljava/lang/String V) Expecting to find unitialized object on stack
I also tried jdk1.3.0_02, to the origin code the compile result is awful, it gives
An exception has occurred in the compiler (1.3.0_02). Please file a bug at the Java Developer Connection (http://java.su
n.com/cgi-bin/bugreport.cgi). Include your program and the following diagnostic in your report. Thank you.java.lang.NullPointerException
at com.sun.tools.javac.v8.comp.TransInner.access(TransInner.java:725)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1563)
at com.sun.tools.javac.v8.tree.Tree$Select.visit(Tree.java:963)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1557)
at com.sun.tools.javac.v8.tree.Tree$Select.visit(Tree.java:963)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1490)
at com.sun.tools.javac.v8.tree.Tree$Apply.visit(Tree.java:785)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:179)
at com.sun.tools.javac.v8.tree.Tree$Exec.visit(Tree.java:699)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:47)
at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:111)
at com.sun.tools.javac.v8.tree.Tree$Block.visit(Tree.java:492)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.tree.TreeTranslator._case(TreeTranslator.java:100)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1423)
at com.sun.tools.javac.v8.tree.Tree$MethodDef.visit(Tree.java:441)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner._case(TransInner.java:1360)
at com.sun.tools.javac.v8.tree.Tree$ClassDef.visit(Tree.java:402)
at com.sun.tools.javac.v8.tree.TreeTranslator.translate(TreeTranslator.java:35)
at com.sun.tools.javac.v8.comp.TransInner.translate(TransInner.java:1322)
at com.sun.tools.javac.v8.comp.TransInner.translateTopLevelClass(TransInner.java:1588)
at com.sun.tools.javac.v8.JavaCompiler.compile(JavaCompiler.java:397)
at com.sun.tools.javac.v8.Main.compile(Main.java:247)
at com.sun.tools.javac.Main.main(Main.java:16)

my conclusion is:
1. befor version jdk1.4 there was bugs in treating inner class.
2. the correct answer of Test038 should be A not B.

[This message has been edited to add code tags]
[This message has been edited by Marilyn deQueiroz (edited October 05, 2001).]
Hi Val
A)
you should not use "System.out.println(l.getItems());", it proves nothing because if you run below code it will get the same result "[Ljava.lang.String;@58957f".

you should use

then the result will be
befor add(), length is 0
after add(), length is 1
B)
but I think the "list" they are talking about is the java.util.List.
if you run the code

the result tell us
befor add(), empty is true
after add(), empty is false
befor add(), empty is true
after add(), empty is false
befor add(), empty is true
after add(), empty is false
so the answer should be c.
galen

[This message has been edited by galen wang (edited September 30, 2001).]
more clearly,
the 'this' is used to indicate current instance.
when the flow is entered the run(), the 'this' instance included two class instances: Inner and Inner's outer OuterTest2. java uses the clas name as a prefix to distinguish these two instances.
so at this code position you can use OuterTest2.this to reference the instance of OutTest2 and you can also use Inner.this/this/(non frefix) to reference the instance of Inner.
for example in the row of ///question here, you can use
System.out.print(s); or System.out.println(this.s); or System.out.println(Inner.this.s);
to print out the Inner's s, but you can only use
System.out.println(OuterTest2.this.s);
to print out OuterTest2's s.
Hi Elizabeth,
below has 3 pattern of code
A)
cool c = new cool();
new Thread(c).start();
new Thread(c).start();
b)
cool c = new cool();
c.start();
c.start();
c)
new cool().start();
new cool().start();
they are different.
as A), two threads are created and started, both of them call the same run() method which belong to the one object c (although c itself is created but not be run). the synchronize acted on method run() is valid.
as B), first a thread object c is created and then it started to run. but at the second call of c.start(), an exception is occured
Exception in thread "main" java.lang.IllegalThreadStateException
at java.lang.Thread.start(Native Method)
at cool.main(cool.java:17)
that's because when you call start() if the thread was already started then IllegalThreadStateException will be throw. (ref API)
after this exception, althoug the thread c still alive but the caller thread will die.
as C), two threads are created and started, but they are two independent objects. although the run() method has a synchronize modificator, the synchronize won't be valid in this situation.
galen
Hi ashok, you are right!
a thread running in a region of synchronized code won't give up the mutually exclusive lock of this object even though it gets into a non-runnable state (waitting, sleeping, blocked), it only gives up the CPU time when it gets into the non-urnnable state. in this situation the other thread can be run if it needs not the same mutually exclusive lock.
it seems like when thread responds an interrupt, it will clear this thread's interrupt status first, so after the running flow enter the catch(InterruptedException ie) block, you can not be able to test the isInterrupted() as true any more.
below is modified code, it may help to understand.

the result is
in Main
in fort..run
t now sleep
in thr..run
outside t: befor call t.interrupt(), the interrupt status is false
-- inside t: befor call super.interrupt(), the interrupt status is false
-- inside t: after call super.interrupt(), the interrupt status is true
outside t: after call t.interrupt(), the interrupt status is true
outside t: waiting for t response ...
-- inside t: respond for catching InterruptedException, now the interrupt status is false
-- inside t: but interrupted is catched
-- inside t: false
-- inside t: retd. from interruption..bye
outside t: after t t.responded(), the interrupt status is false
after thr.. run
run your code the system will indeed give you twice prompt, but only first time it stoped to wait your input.
the reason is that even you only entered a return, the system's key buffer will contain at least two key value (13, 10). the first thread's System.in.read() will get only one key value from the key buffer and the other key values will be remained in the key buffer. then the second thread's System.in.read() will get a key value from the key buffer immediately.
if you mordify the statement
System.in.read(); => System.out.println("output: " + System.in.read());
then you will understand this reason.
below are some result
1.
in run
01
output: 48
after run
in run
output: 49
after run
2.
in run
ab
output: 97
after run
in run
output: 98
after run
3.
in run
AB
output: 65
after run
in run
output: 66
after run
4.
in run
output: 13
after run
in run
output: 10
after run
the same problom as your topic sleep()
the point is only those threads' synchronized method runs inside an object then the synchronize is valid.
1)
for example
code
-----
class TestThread extends Thread {
String ClassName;
TestThread(String classname) {
ClassName = classname;
}
public synchronized void run() {
while(true) {
System.out.println(ClassName);
}
}
public static void main(String args[]) {
new TestThread("class object 1").start();
new TestThread("class object 2").start();
}
}
-----
will output something like
......
class object 2
class object 1
class object 2
class object 1
class object 2
class object 2
......
that's because two thread objects are created and each has its own run() method. two objects can not be synchronized.
2)
if you modify the statement
synchronized(fort.class) { => synchronized(this) {
in your second code, then the result will be same as your first codes. that's alse because two objects can not be synchronized.
3)
code
-----
class synchronizedPart {
public synchronized void run(TestThread t) {
System.out.println("entered in run");
System.out.println(t.getPriority());
t.yield(); //1
System.out.println("end of run");
}
}
class TestThread extends Thread {
synchronizedPart sp;
TestThread(synchronizedPart sp) {
this.sp = sp;
}
public synchronized void run() {
sp.run(this);
}
public static void main(String args[]) {
synchronizedPart sp = new synchronizedPart();
Thread t = new TestThread(sp);
t.start();
t = new TestThread(sp);
t.start();
}
}
-----
will get the same result as your original second code, although two threads are created but the method
synchronizedPart.run()
belong to one object, so this method can be synchronized.
from jdk api definition of Class Float:
public boolean equals(Object obj)
Compares this object against the specified object. The result is true if and only if the argument is not null and is a Float object that represents a float with the same value as the float represented by this object. For this purpose, two float values are considered to be the same if and only if the method floatToIntBits(float) returns the identical int value when applied to each.
Note that in most cases, for two instances of class Float, f1 and f2, the value of f1.equals(f2) is true if and only if
f1.floatValue() == f2.floatValue()

also has the value true. However, there are two exceptions:
1) If f1 and f2 both represent Float.NaN, then the equals method returns true, even though Float.NaN==Float.NaN has the value false.
2) If f1 represents .0f while f2 represents -0.0f, or vice versa, the equal test has the value false, even though 0.0f==-0.0f has the value true.

[This message has been edited by galen wang (edited September 21, 2001).]
that's right. you created 2 instance of TestThread, each has a instance method run() belong to different object.
if you change the code to
class TestThread extends Thread {
public void run() {
run_test();
}
public static synchronized void run_test() {
System.out.println("entered in run"); //1
try {
sleep(1000);
} catch(InterruptedException ie) {}
System.out.println("end of run"); //2
}
public static void main(String args[]) {
new TestThread().start();
new TestThread().start();
}
}
then the resule will be
entered in run
end of run
entered in run
end of run
this is because the method run_test() only have one entity, its belong to the class.
when using add() and there is no room in the Vector, then the capacity will be increased. the rule is:
1. if the Vector is created by no parameter (e.g. Vector v = new Vector()) then the capacity will be doubled based on 10. that is 2*10, 2*2*10, 2*2*2*10, .... (2^i * 10)
2. if the Vector is created by one parameter (e.g. Vector v = new Vector(n)) then the capacity will be doubled based on n. that is 2*n, 2*2*n, 2*2*2*n, .... (2^i * n)
for n = 1, it will be 2,4,8,18...
for n = 2, it will be 4,8,16,32...
for n = 3, it will be 6,12,24,48...
...
3. if the Vector is created by two parameter (e.g. Vector v = new Vector(l, m)) then the capacity will be increased by the size. that is 2*(m-l+1), 3*(m-l+1), 4*(m-l+1) .... (i*(m-l+1))
for n = 1, it will be 1,2,3,4...
for n = 2, it will be 2,4,6,8...
for n = 3, it will be 3,6,9,12...
...