Hi all,

I also have difficulty to understand this homework.

As we know, there are two ways to synchronize: declare synchronized
method OR insert a synchronized statement.

I tried synchronized method - it did not work for me as well.

To implement synchronized statement, a separate class should be declared
and object of this class should be passed as an argument - like
TwoStrings class in threading examples -- but in our assignment we have
to: 

-- Create a class called MyCurrentDate that implements Runnable
interface
-- Create a class called MyMain, which contans main() method

 Meaning - these two classes only?? -- so we cannot use synchronizes
statement???
I probably missing something, please help!!!

Thanks,
Moria.

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Zhao
Lin
Sent: Saturday, October 17, 2009 10:58 AM
To: [email protected]
Subject: [java programming] 1021_HW


Hi

 // This method is synchronized
    synchronized void timeStamp(String name){
            for (int i = 0; i < 4; i++) {
                try {
                    Thread.sleep(1000);
                }//try
                catch (InterruptedException e) {
                }//catch
                System.out.println(name + " at " + i +
                        " has " + this.getCurrentTime());
            }//for
        }//print

Output is
Time3 at 0 has Saturday, October 17, 2009 1:56:49 PM EDT
Time1 at 0 has Saturday, October 17, 2009 1:56:49 PM EDT
Time2 at 0 has Saturday, October 17, 2009 1:56:49 PM EDT
Time3 at 1 has Saturday, October 17, 2009 1:56:50 PM EDT
Time2 at 1 has Saturday, October 17, 2009 1:56:50 PM EDT
Time1 at 1 has Saturday, October 17, 2009 1:56:50 PM EDT
Time3 at 2 has Saturday, October 17, 2009 1:56:51 PM EDT
Time1 at 2 has Saturday, October 17, 2009 1:56:51 PM EDT
Time2 at 2 has Saturday, October 17, 2009 1:56:51 PM EDT
Time3 at 3 has Saturday, October 17, 2009 1:56:52 PM EDT
Time1 at 3 has Saturday, October 17, 2009 1:56:52 PM EDT
Time2 at 3 has Saturday, October 17, 2009 1:56:52 PM EDT

Time1 is not first to start.  Does this method have to be synchronized.
Homework is not explicit?


      




--~--~---------~--~----~------------~-------~--~----~
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