Woo Hwang

Greenhorn
+ Follow
since Aug 07, 2001
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 Woo Hwang

What I made out to return true is:
class test {
public static void main(String args[]){
synchtest s=new synchtest();
Thread1 A=new Thread1();
Thread2 B=new Thread2();
Thread t1=new Thread(A);
Thread t2=new Thread(B);
A.Init(s,t2); /// To give handles of synchtest and thread of thread2 to thread1
B.Init(s,t1);/// To give handles of synchtest and thread of thread1 to thread2
t1.start();
t2.start();
}
}
class Thread1 extends Thread{
synchtest s1;
Thread t2;
int i;
void Init(synchtest s,Thread t){
s1=s;
t2=t;
}
public void run(){
do {
System.out.println("Thread 1 goes "+i+++" time(s)");
try{
s1.setX(1);
System.out.println("thread1 sleeps after setting x 1");
System.out.println("now x="+s1.x+" y="+s1.y);
sleep(2000); /// thread1 sleep to give thread2 a chance to run
s1.setY(1);
System.out.println("thread1 sleeps after setting y 1");
System.out.println("now x="+s1.x+" y="+s1.y);
//sleep(2000); Here, I didn't put sleep because setting y in here likely came after thread2's sleep after setting x. So x,y have different value each other
}catch (InterruptedException e){
break;}
}while(s1.check()==false);
System.out.println("Thread1 finishes. x="+s1.x+" \t"+"y="+s1.y);
t2.interrupt(); // When this thread finishes, call interrupt() to another sleeping thread to wake up and break out of while root
}}
class Thread2 extends Thread{
int j;
synchtest s2;
Thread t1;
void Init(synchtest s,Thread t){
s2=s;
t1=t;
}
public void run(){
do {
System.out.println("Thread 2 goes "+j+++" time(s)");
try{
s2.setX(2);
System.out.println("thread2 sleeps after setting x 2");
System.out.println("now x="+s2.x+" y="+s2.y);
sleep(2000);
s2.setY(2);
System.out.println("thread2 sleeps after setting y 2");
System.out.println("now x="+s2.x+" y="+s2.y);
sleep(2000);
}catch(InterruptedException e){
break;}
}while(s2.check()==false);
System.out.println("Thread 2 Finishes. x="+s2.x+" \t"+"y="+s2.y);
t1.interrupt();
}}
class synchtest {
public int x;
public int y;
public void setX(int i){
x=i;
}
public void setY(int i){
y=i;
}
public synchronized boolean check(){
return x!=y;}
}

And the result is:

Thread 2 goes 0 time(s)
thread2 sleeps after setting x 2
now x=2 y=0
Thread 1 goes 0 time(s)
thread1 sleeps after setting x 1
now x=1 y=0
thread2 sleeps after setting y 2
now x=1 y=2
thread1 sleeps after setting y 1
now x=1 y=1
Thread 1 goes 1 time(s)
thread1 sleeps after setting x 1
now x=1 y=1
Thread 2 goes 1 time(s)
thread2 sleeps after setting x 2
now x=2 y=1
thread1 sleeps after setting y 1
now x=2 y=1
Thread1 finishes. x=2 y=1
Thread 2 Finishes. x=2 y=1

Sorry. The code might seem to be a bit mess
As to SetXY method, it isn't synchronized but the only chance the thread scheduler let another thread go is when the thread running finishes setting one value and moves to set another one. But could it be possible? When I ran the code without sleep() method, the thread didn't stops until its process finished. Some functions are needed to put in between setX and setY to pause one thread and let another go. Without that function, we rarely ,scarcely, hardly, and seldom see one thread let another thread go in the middle of perfoming setXY method.
I need BIG discussion on this y'all


[This message has been edited by Woo Hwang (edited August 13, 2001).]
[This message has been edited by Woo Hwang (edited August 13, 2001).]
But, why do we need to call the main() method, though it automatically runs when JVM starts? Is there any situation to call the main() method intentially? And What does it do to thread precess by calling the main method?

still
public class SyncTest{
private int x;
private int y;
private synchronized void SetX(int i){
x=i;}
private synchronized void SetY(int i){
y=i;}
public void setXY(int i){
SetX(i);
SetY(i);
}
public synchronized boolean check(){
return x!=y;
}
Question is under what condition does check() return true when called from different class?
1.check() can never return true.
2.check() can return true when setXY() is called by multiple thread.
3.check() can return true when multiple thread calls setX() and setY() separately.
4.check() can only return true if SynchTest is changed to allow x and y to be set separately.
I think answers are 1,4 though different people have different opinions. My answer comes like this:
Because setX and setY have private class, it can't? be accessed outside of this class and the only way to reach these methods is by accessing setXY. But if one thread( let's assume there are two threads) reaches setX to modify x value, because setX and setY are synchronized, another thread can't run Synchtest while the first thread is working on setX or setY.
Someone says that it could be possible that right after the first thread finishes setting x, it stops and the second thread starts run and set x with different value from the first thread?. Then the 2nd thread stops, and the the 1st continues its own operation setting y and call check() . In this case, x and y can have different values and number 3 could be an answer. But I don't? think that's?general answer. As I know ,OS with time-sliced model doesn't? follow that rule, and when I had made out whole code and ran, thread didn't? go like the preposition above. It's? more like one thread starts and finishes, and do this operation several times or let another thread go.
Therefore I think, without modifying the code above to set x and y separately, check() never return true.
But it's still confusing to me. Thread is soooooo difficult... That's why I post this to clear this confusion. What do you think the answer is ?
a. Exiting from a synchronized block
b. Callling the main method on an object
c. Calling the notify method on an object
d . Calling a read method on an InputStream object
e. Calling the setPriority method on a Thread object
I found this questionary from a SJCP studying site. I think answer is can be all.
a. Exiting sychronized block means that the thread releases its lock, not stop executing.
c. Wait () can stop thread executing not notify() or notifyAll()
d. when a thread tries to read or write from or to stream and those activities were delayed because of ,in example, sluggish stream rate , the thread stop executing(actually it is blocked) until the delay goes away. So, if stream is not delayed, thread process doesn't stop.
e. I heard that setPriority can change the thread's status and might stop the current thread. However, I heard that this process is platform-dependant. OS which use pre-emptive process such as Solaris follows this rule but OS which has a time-slicing architecture does not.

Can someone check if I'm right for all of these explanations?
In addtion, I don't know what it means to call main method on an object. Can you expain me what it is calling main method? Because I've never seen this before. And effect on thread running.
Thank you in advance.
I found this code snippet from one of my favorite SCJP study sites.
class synchtest {
private int x;
private int y;
public void setX(int i){
x=i;
}
public void setY(int i){
y=i;
}
public synchronized SetXY(int i){
setX(i);
setY(i):
}
public synchronized boolean check(){
return x!=y;}
}

The question is what make the check() return true supposing we access this class by mutiple thread, and the answer was check() never return true, and I aggreed to that.
But another possible answer which I doubted was that the check() might have returned true if x,y could have been set seperately. What I doubted here was if I have editted code to set x,y seperately, I would have made out thread code snippet like this :
thread 1:
synchtest s1=new synchtest();
s1.setX(1);
s1.setY(1);
System.out.println(s1.check());
thread2:
synchtest s2=new synchtest();
s2.setX(2);
s2.setY(2);
System.out.println(s2.check());
and the reason I disagreed with possible true proposition was that if thread1 and thread2 have different value set on x,y , the possible situation that check() returns true is:
1. thread1 (Let's assume that thread1 starts first) sets x to 1.
2. thread1 stops executing because thread2 starts runnig with dominated priority.
3. thread2 sets x to 2 and on the way to setting y to 2, thread2 stops execution because thread1 gets previlege to continue its process, so y is set to 1 also, thread1 is not intervened by thread2, and successfully performs check() method.
In this case, check() returns true, I thought.
But in a real situation, thread doesn't go like that so far as I know. It's like thread 1 operates all the way to the end, then thread2 does same as thread1. When I ran that code with do/while block embracing those processes above , it never did like the preposition above.
Then I put sleep(2000) in between each code setting x and y value except SetY() in thread1 with assumption that if thread1 sets x and goes on sleeping then thread2 takes chance to setX to 2 and goes on sleep and thread1 continues its process to set y to 1 but no sleep. It continues to process to call check() method. Therfore, x=2, y=1 which check() returns true.
thread 1:
synchtest s1=new synchtest();
s1.setX(1);
sleep(2000);
s1.setY(1);
System.out.println(s1.check());
thread2:
synchtest s2=new synchtest();
s2.setX(2);
sleep(2000);
s2.setY(2);
sleep(2000);
System.out.println(s2.check());
But my assumption was wrong. What I found out from running this code was that ,yes, thread ran like this:
Run thread1 and set x=1 --> thread1 sleeps---> thread2 starts to run and set x=2 --->thread2 sleeps --->thread1 resumes and set y=1----> no sleep in thread1 and call check()
As you can see before calling check, value x,y are 2,1, different !! So check() should return true. But it didn't return true. I wonder why. So I put some tracers to check values of x,y . What I saw was that x,y in thread1 were independent on x,y in thread2. I mean, each thread has its own x,y values and what happened in a real situation was different from my assumption. The REAL situation is :
Run thread1 and set x=1 --> thread1 sleeps---> thread2 starts to run and set x=2 --->thread2 sleeps --->thread1 resumes and set y=1----> no sleep in thread1.Call check()---> returns false because x,y values in thread1 are 1,1 not 2,1. x,y values in Thread2 are 2,2
Why two thread have thier own value? is that a result of race condition that people warn of





[This message has been edited by Woo Hwang (edited August 11, 2001).]
Thank you very much for your replay,Shankar. So, according to your answer, if all the class in that code didn't have any relation to each other(no inheritance), and in that case, if I put a code like line 5, it would cause exception too. Am I right?
I found this code snippet from a java mocking exam
class parent{}
class derivedOne extends parent{}
class derivedTwo extends parent{}
1.public class test {
2.public static void main(String args[]){
3.parent p=new parent();
4.derivedOne d1=new derivedOne();
5.d1=(derivedOne) p;
6.System.out.println("done");
7.}
8.}
They asked what the result of this code was. The answer is exception at runtime because of line #5. After I ran this code, yes, line 5 occured runtime error. I want to know whyyyyy that line caused exception.
thank you in advance.
I found this question from one of my favorite SCJP websites. It has an answer, but I doubt it, so I put this question again.
Which statements about listeners are true?
a. At most one listener can be added to any single component.
b. If multiple listeners are added to a single component, the order of invocation of the listener is not specified.
c. The return value from a listener is used to control the invocation of other listeners
d. In the java package, listener methods generally take an argument which is an instance of the some subclass of java.awt.AWTEvent class
They said the answer were b,d. Yes, I agree to b, but I'm not sure of d. Can somebody give me an answer for this?
22 years ago
When I complied this code below, an error occured because of use of this() in an overloading method, and compiler also said that only contructors could invoke contructors. What does that mean?
public class test {
private int x,y;
private float z;

public void setVar(int a,int b,float c){
x=a;
y=b;
z=c;
}

public void setVar(int a,float c, int b){
this(a,c,b);
}
}

22 years ago
Look at this code:
public class test extends Thread{
public static void main(String args[]){
final StringBuffer s1=new StringBuffer();
final StringBuffer s2=new StringBuffer();
new Thread(){
public void run(){
synchronized(s1){
s1.append("a");
synchronized(s2){
s2.append("b");
System.out.println(s1);
System.out.println(s2);
}
}
}
}.start();
new Thread(){
public void run(){
synchronized(s2){
s2.append("c");
synchronized(s1){
s1.append("d");
System.out.println(s1);
System.out.println(s2);
}
}
}
}.start();
}}
Regarding that these two thread run concurrently, what could be happened if the1st thread holds a lock on s1 and the 2nd thread holds on s2, then 1st one attemps to hold a lock on s2 and the 2nd one to hold on s1? Is it possible that this code is stuck in deadlock condition? So far I run this code, it doesn't cause any deadlock yet. But I think it has possibility. What do you think of this ?

[This message has been edited by Woo Hwang (edited August 09, 2001).]
In case of an inner class, what I learnt is that all modifiers except transient can be used, but when I used private or static modifiers in front of ordinary classes, compile error occured. Can someone tell me why?
22 years ago
Thank you for your reply. Now I understand that. But I don't know what is like "place its code in-line" . Sorry, Engllish is not my native one.
Thank you
Woo
22 years ago
I'm reading "Getting in touch with your Inner Class" article, I encounter a term reading a paragraph blow:
Another warning about all inner classed is that that can't declare ant static members unless they are compile-time constant and are promitives or Strings( This does not apply to static nested classed, of course)
I don't know what is compile-time constant. Can anybody tell me what it is?
22 years ago