Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: synchronised

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default synchronised

    class B 
    {    
        public static synchronized void printName()
        {
            try
            {
    	           System.out.println("printName");
    	           Thread.sleep(5*1000);
            }
            catch(InterruptedException e)
            {             
            }
        }
        public synchronized void printValue()
        {      
                   System.out.println("printValue");                
        }
    }
    public class test extends Thread
    {
        B b = new B();
        public static void main(String argv[]) throws Exception
        {
    	    test t = new test();
    	    test v=new test();
    	    Thread t1 = new Thread(t,"t1");
    	    Thread t2 = new Thread(t,"t2");
    	    t1.start();
    	    t2.start();
        }  
        public void run()
        {      
            if(Thread.currentThread().getName().equals("t1"))
            {
                b.printName();
            }
            else
            {
                b.printValue();
            }       
        }   
    }
    static synchronised means locked for the class so why it is printing without any delay of 5 sec


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: synchronised

    Here's how your program flow is going:
    1. main thread
    start thread 2 and thread 3
    2. t1 thread
    print out text, wait 5 seconds
    3. t2 thread
    print out text

    Because thread 2 and 3 are running concurently, thread 2's wait only affects thread two. Ex:

    // changed the run to do something in thread 2 after the wait
    if (Thread.currentThread().getName().equals("t1"))
    {
         b.printName();
         System.out.println("done waiting!");
    }...

    After running this, you'll see:

    printName
    printValue
    done waiting!

    Also, fyi if all you're running is a static method, you don't need to create a B object in your main class. Call the static method using the class name.

    B.printName();
    B.printValue();

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: synchronised

    no its not locking the object in case of static synchronised
    in case of static
    the output is
    printname
    printvalue
    donewaiting
    why

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: synchronised

    The static refers to a class state, not a specific instanced object. So, any static methods are locked to the static synchronized method, but all instance methods are left unlocked. See Instrinsic Locks and Synchronization (second section).

  5. #5
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: synchronised

    nice post!
    -------
    Greensboro Press Release Service