Hi Thomas,
I am trying to answer some of your questions and hope I am not misleading. If any virtuosos on java threads here see any misconceptions, please point out. Thanks in advance.
What is the effect of �synchronized� in the run() method? "synchronized" is to prevent more than one threads accessing a same object concurrently.
I thought �synchronized� would lock the run method for one Thread so that the threads don�t run parallel but sequential Yes. Just think it this way: the purpose you put a "synchronized" modifier is to tell the JVM: Hi JVM, the following method/block might be accessed by more than one threads concurrently, but I don�t want this happen. I need your help, please make the accesses sequential for me. So that the JVM "locks" the object.
When yield() is called, the current running thread releases the CPU resource which it is using, and queued, waits for its next turn to possess some CPU cycles. So that its status is "runnable" after a yield() call. It�s interrupted but not terminated.
wait(1000) No, this is not a proper way to use wait(). wait() has no parameter. You wait() for some notify/notifyAll, but you cannot specify how long. JVM controls it completely. And the wait() method is final in the Object class, you cannot overwrite it.
Is the result of the yield()that the two threads run better parallel than before??? Not really concurrently. you�d rather say: The execution of the two threads was cut into smaller/more pieces than before. In every turn, each threads gets some CPU cycles for small pieces, of course it appears more "concurrently". Actually in a single-CPU box, your two threads are always executed sequentially, never concurrently.
That�s what I can tell.
Here I don�t understand:
hread-1i=576
Thread-1i=577
Thread-1i=578
Thread-1i=579
Thread-2i=0
Thread-2i=1
Thread-2i=2
Thread-2i=3
since the run() is synchronized, why the Thread-1 only reached 579( I think should be 1000) when it passed the control to thread2??
Anyone be so kind and explain? Thank you in advance!
Regards,
Ellen
[ January 17, 2003: Message edited by: Ellen Fu ]