Richard Yi

Greenhorn
+ Follow
since Feb 04, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Richard Yi

Below is the extract from API. But I can't find out the reason.
renameTo
public boolean renameTo(File dest)Renames the file denoted by this abstract pathname.
Whether or not this method can move a file from one filesystem to another is platform-dependent. The return value should always be checked to make sure that the rename operation was successful.

Parameters:
dest - The new abstract pathname for the named file
Returns:
true if and only if the renaming succeeded; false otherwise
20 years ago
Hi,
The program do these things:
1.read the source file line by line;
2.modify the string;
3.write the string to a temp file;
3.rename the temp to the source file.
But I found sometimes the method renameTo() would return false. Would you pls tell me why? What is the mechanism of file's renaming? When it return false and when do true? Thank you !
The code lists below:
try {
//turn the source file f to the Reader
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(f)));
//turn the temp file temp to the writer
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(temp))),true);
String s = new String();
while ( (s = in.readLine()) != null) {

s = modifyString(s);//invoke my own method
//to modify string
out.println(s);
}
in.close();
out.close();
System.out.println(temp.renameTo(f));//rename temp file to the
//original file
System.out.println(temp.getPath());
System.out.println(f.getPath());
}
catch (FileNotFoundException fnfe) {}
catch (IOException e) {}
20 years ago
En~~~~~,Lasse Koskela, Would you pls give some comments? I think your code seem to how to read info from client, but not how to send info to server.
Busyness logic and DB have been done. The remainder simple task is write an application client that building a connetion to server and deliver the parameter(e.g String) with this connection.
However, the server just can accept HTTP request, but not socket. My question is: how can build HTTP connetion and deliver the parameter? Very appreciate if you can provide some codes in details.
Thank you!
Thank you Satish! I think there is another difference between these 2 Q:
setX() and setY() in Q107 are public, however, in Q57 they are private.
I think it is a typo for Q107(i.e.they should have been private!) for option D will be non-sense if they are public! (It originally allow x and y to be set separately, NOT need change!)
Thus the ans should be A.
Pls refer to below question as well. The only difference between these 2 (Q57 & Q107) is at line6 of the program: one is synchronized but the other is not.
The standard ans for Q57 is B. C is incorrect ans so I think it is incorrect for Q107 as well (But I cannot catch the real reason
). Thus A will be the ans for my first question(Q107), is this right?
Q57. Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX (int i) {x=i ;}
5. private synchronized void setY (int i) {y=i ;}
6. public void setXY(int 1){set X(i); setY(i) ;}
7. public synchronized Boolean check() {return x !=y ;}
8. }
Under which conditions will check () return true when called from a different class?
A. Check() can never return true
B. Check() can return true when setXY is called by multiple threads
C. Check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
[ February 09, 2004: Message edited by: Richard Yi ]
107
====
Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. public synchronized void setX (int i) { x = i;}
5. public synchronized void setY (int i) { y = i;}
6. public synchronized void setXY(int i) {setX(i); setY(i);}
7. public synchronized Boolean check() {return x !=y;}
8. }
Under which conditions will check () return true when called from a different class?
A. check() can never return true
B. check() can return true when setXY is called by multiple threads
C. check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
=============================================
I cannot confirm the corrected ans is A or C. Anyone could give me the explanation? Thank you!
Oh! I'm wrong! I looked the word "unchecked" as "check" in your last point!
I read it too rapidly!!
I know that if we use criteria section like below:
synchronized(Object o) {
//some code...
}
the section will obtain the lock of object o;
But the confusing thing for me is that which lock will be obtained by the section when defining a method with "synchronized", like below:
synchronized void aMethod() {
//some code...
}
then aMethod() obtain whose lock??
Thank you!
The ans is exactly abde, but there are some error in Vicken's explanation I think.
ArithmeticException is extend from RuntimeException, which can be thrown automatically by JVM. This is the exact reason for it is the right ans.
However, if declaring a method (overridding) throw an Exception not belong to RuntimeException or IOException in this question, it will be illegle!DataFormatException for Example!
Thank you Iain and Stephen!
Many appreciate Sudhakar! Your explanation is so clear that I catch it completely.
Below is the question. The ans is DE but mine is AB
61. Click the exhibit button:
1.public class SyncTest{
2.public static void main(String[] args) {
3.final StringBuffer s1= new StringBuffer();
4.final StringBuffer s2= new StringBuffer();
5. new Thread () {
6. public void run() {
7.synchronized(s1) {
8.s1.append(�A�);
9.synchronized(s2) {
10.s2.append(�B�);
11.System.out.print(s1);
12.System.out.print(s2);
13.}
14.}
15.}
16.}.start();
17.new Thread() {
18.public void run() {
19.synchronized(s2) {
20.s2.append(�C�);
21.synchronized(s1) {
22.s1.append(�D�);
23.System.out.print(s2);
24.System.out.print(s1);
25.}
26.}
27.}
28.}.start();
29.}
30. }
Which two statements are true? (Choose Two)
A. The program prints �ABBCAD�
B. The program prints �CDDACB�
C. The program prints �ADCBADBC�
D. The output is a non-deterministic point because of a possible deadlock condition
E. The output is dependent on the threading model of the system the program is running on.
I think the program create 2 objects of 2 annymous inner classes extends Thread in class SynTest and call start() immediately. In every annymous class run() method, there is a synchronized block till to the end of the run(), and this block also embeded another synchronized block but it cannot affect the outer block I think. Thus run() will execute all the codes in it without any interrupting! Of course we cannot ensure which thread will run first. so the answer I think is AB.
But the standard answer say it should be DE, Why?
Thank you !
Below are the 2 questions regarding thread, and in parentheses are the standard answers provide to me:
55. Which two CANNOT directly cause a thread to stop executing? (Choose Two) (A,E)
A. Calling the yield method
B. Calling the wait method on an object
C. Calling the notify method on an object
D. Calling the NotifyAll method on an object
E. Calling the start method on another Thread object
60. Which two CANNOT directly cause a thread to stop executing? (Choose Two) (A,C)
A.Existing from a synchronized block
B.Calling the wait method on an object
C.Calling notify method on an object
D.Calling read method on an InputStream object
E.Calling the SetPriority method on a Thread object
You should note that the question is the same but the items for choosing are different. Also you should note that the "notify" was chosen as answer in the latter but not in the former! This is so strange and conflict and I cannot understand!
According my knowledge, I think yield(), wait() and read()(I/O block) could cause a thread to stop directly, but notify() and notifyAll() should wake a thread up and start() is the method let the method run! But the above answers frustrate me! Maybe I'm confusing regarding this conception, would you pls help me and give me the detailed and clear explanation for each item enumerated above? Are there some errors in standard answers?
Thank you very much!
Mn~~~ Ray, I think the question is "MUST extend", NOT "could extend" . An inner class could extend any classes and interfaces of course.
[ February 04, 2004: Message edited by: Richard Yi ]