SYNCHRONIZED !!!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: SYNCHRONIZED !!!

  1. #1
    Ashish patel Guest

    SYNCHRONIZED !!!


    look at this program :

    import java.io.*;
    class TEST extends Thread
    {
    public synchronized void run()
    {
    for(int i=0;i<10;++i)
    System.out.println(i);
    }
    static void main(String saa[])
    {
    TEST t1=new TEST();
    TEST t2=new TEST();
    t1.start();
    t2.start();
    }
    }

    This progra, prints 1 1 2 2 3 3 ... 10 10
    though i have made run mathod Synchronized !??
    instead it should print 1 2 3 4 ... 10 then 1 2 3 .. 10
    Any help?
    Thanks in advance
    -Ashish



  2. #2
    Vaibhav Jain Guest

    Re: SYNCHRONIZED !!!


    "Ashish patel" <patel_ashishr@hotmail.com> wrote:
    >
    >look at this program :
    >
    >import java.io.*;
    >class TEST extends Thread
    >{
    > public synchronized void run()
    > {
    > for(int i=0;i<10;++i)
    > System.out.println(i);
    > }
    > static void main(String saa[])
    > {
    > TEST t1=new TEST();
    > TEST t2=new TEST();
    > t1.start();
    > t2.start();
    > }
    >}
    >
    >This progra, prints 1 1 2 2 3 3 ... 10 10
    >though i have made run mathod Synchronized !??
    >instead it should print 1 2 3 4 ... 10 then 1 2 3 .. 10
    >Any help?
    >Thanks in advance
    >-Ashish
    >
    >


    By synchronised keyword the lock is acquired on the object
    of class Test. After acquiring lock instance variables
    couldn't be changed by other thread. i is the local variable
    so it couldn't be locked.




  3. #3
    Aman Guest

    Re: SYNCHRONIZED !!!



    hi friends,
    look at this code with and without the synchronized keyword

  4. #4
    Ashish patel Guest

    Re: SYNCHRONIZED !!!


    "Vaibhav Jain" <j_vaibhav76@yahoo.com> wrote:
    >
    >"Ashish patel" <patel_ashishr@hotmail.com> wrote:
    >>
    >>look at this program :
    >>
    >>import java.io.*;
    >>class TEST extends Thread
    >>{
    >> public synchronized void run()
    >> {
    >> for(int i=0;i<10;++i)
    >> System.out.println(i);
    >> }
    >> static void main(String saa[])
    >> {
    >> TEST t1=new TEST();
    >> TEST t2=new TEST();
    >> t1.start();
    >> t2.start();
    >> }
    >>}
    >>
    >>This progra, prints 1 1 2 2 3 3 ... 10 10
    >>though i have made run mathod Synchronized !??
    >>instead it should print 1 2 3 4 ... 10 then 1 2 3 .. 10
    >>Any help?
    >>Thanks in advance
    >>-Ashish
    >>
    >>

    >
    >By synchronised keyword the lock is acquired on the object
    >of class Test. After acquiring lock instance variables
    >couldn't be changed by other thread. i is the local variable
    >so it couldn't be locked.


    there is nothing to lock here.
    Since i am using synchronized mathod()
    not synchronized (some Object).
    When method is made synchronized, no other thread should
    be able to access this method when one method is used by one thread. If this
    theory is true then given program should print
    1234.....10 then 1234... 10
    not 11223344 .... 10 10




  5. #5
    Paul Clapham Guest

    Re: SYNCHRONIZED !!!

    When you run t1.start(), it calls the run() method of object t1. This is
    synchronized so it acquires a lock on t1. Likewise when you run t2.start,
    it acquires a lock on t2. There is no conflict between these locks, so the
    threads are free to run and can produce output in any sequence.

    Ashish patel <patel_ashishr@hotmail.com> wrote in message
    news:39abfd80$2@news.devx.com...
    >
    > look at this program :
    >
    > import java.io.*;
    > class TEST extends Thread
    >


    > public synchronized void run()
    > {
    > for(int i=0;i<10;++i)
    > System.out.println(i);
    > }
    > static void main(String saa[])
    > {
    > TEST t1=new TEST();
    > TEST t2=new TEST();
    > t1.start();
    > t2.start();
    > }
    > }
    >
    > This progra, prints 1 1 2 2 3 3 ... 10 10
    > though i have made run mathod Synchronized !??
    > instead it should print 1 2 3 4 ... 10 then 1 2 3 .. 10
    > Any help?
    > Thanks in advance
    > -Ashish
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center