Hi Saidamin

It seems to me that the 1st thread starts and calls DateAndTime.show(...). This action locks the access of other threads to this method until the end of its execution by the first thread.

When the 2nd thread starts, the method is locked and the thread must wait. According to the implementation of the "retry" mechanism (we may guess that the thread sleeps some time and tries again), it may miss the moment when the method is released and it will be the third thread that passes first (as it was started at the right moment).

The moral of this behavior is that when more than one thread are waiting in queue for running a locked synchronized method, the execution order may be unpredictable.

Hope it helps
Mihai

Saidamin Usmanov a écrit :
Hello Everyone,

I am doing home work on Thread part.

I have following classes:


public class MyCurrentDate implements Runnable{
   Thread thread;
   String name;

   public MyCurrentDate(String thr){            name = thr;
       thread = new Thread(this);
       thread.start();
   }

   public void run() {              DateAndTime.show(name);
   }
}


import java.util.Date;
public class DateAndTime {
   static synchronized void show(String thr) {
       for(int i = 0; i < 5; i++){
System.out.println(thr + ": " + String.format("%tc", new Date()));
       }
       try {
         Thread.sleep(100);
       } catch (InterruptedException e) {}          }
}



When I run following code

public class Main {
   public static void main(String[] args) {
     new MyCurrentDate("Thread 1");
     new MyCurrentDate("Thread 2");
     new MyCurrentDate("Thread 3");
   }
}

I get

Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010


Instead of
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 1:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 2:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010
Thread 3:  Sun Jun 20 16:28:05 EDT 2010

Does anyone know how to fix the problem?

Thank you in advance,
Saidamin Usmanov


--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to