-
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
-
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.
-
Re: SYNCHRONIZED !!!
hi friends,
look at this code with and without the synchronized keyword
-
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
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks