• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread doubt

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


can someone tell me why thread execute concurrently
the numbers will be same on same line(i=1,j=1)

What I thought was because the run method is synchronized one thread must complete its run method before another thread begins.
sample output:
Thread-0 i 1,j 1
Thread-0 i 2,j 2
Thread-0 i 3,j 3
Thread-0 i 4,j 4
Thread-0 i 5,j 5
Thread-0 i 6,j 6
Thread-0 i 7,j 7
Thread-0 i 8,j 8
Thread-1 i 1,j 1
Thread-1 i 2,j 2
Thread-1 i 3,j 3
Thread-1 i 4,j 4
Thread-1 i 5,j 5
Thread-1 i 6,j 6
Thread-1 i 7,j 7
Thread-1 i 8,j 8
Thread-1 i 9,j 9
Thread-1 i 10,j 10
Thread-1 i 11,j 11
Thread-1 i 12,j 12
Thread-0 i 9,j 9
Thread-0 i 10,j 10
Thread-0 i 11,j 11
Thread-0 i 12,j 12
[ May 15, 2007: Message edited by: shyam kumarK ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On what object are you holding the lock. Are they the same object.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

shyam


I think ,when ever a thread is in a running state,then that thread can enter into waiting state.And when a thread will enter into running state ,it depends on schedular.When ever schedular wants to start a thread ,the schedular calls run method.And you synchronized that method.I think synchronizing the run() method doesn't have any impact,i think.


Thanks
Anil Kumar
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You created 2 objects, and it is possible to acquire lock on both. synchronize keyword used in method declaration will always try to acquire lock on (this). And since you have 2 objects, and each is acquiring lock on (this), this is always possible. Threads can run at the same time.

Take a look at code below, which is a little bit modified.
Here I create new thread, which acquires lock on itself and start printing. Immediatly after I call run from main, which is also trying to acquire lock. Important is, that it is trying to acquire lock on the same object.




To solve your problem with 2 threads, remove synchronize from method declaration and insert synchronized block to run. And synchronize on (A.class).
This ways threads will "fight" for one lock, and only thread, which acquire lock will run. Other threds must wait.
 
shyam kumarK
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John,


Anil said
I think ,when ever a thread is in a running state,then that thread can enter into waiting state.

to correct it,
when ever a thread is in a running state,then that thread can enter into Runnable state(if scheduler prefers to run another thread) and waiting state(only if it tries to enter a method or block which is synchronized on an object whose lock is owned by another thread unless you called wait on it)
i think
[ May 15, 2007: Message edited by: shyam kumarK ]
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the modified version:

 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shyam kumarK:
once a thread starts run method any other thread cant enter that method because it is a synchronized method..



Any other thread trying to enter this method for _this_ object must wait.

in your first code example:
thread a1 is calling run, so it must acquire lock on object a1, OK, running
thread a2 is calling run, so it must acquire lock on object a2, OK (nobody owns lock on a2), running
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shyam kumarK:
Thanks John,

I think It is wrong that the thread goes to wait....Rather it is in runnable state when the other thread runs

[ May 15, 2007: Message edited by: shyam kumarK ]


Right, I meant it as can't enter,
I'm not sure what is thread doing at that time.
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from JLS3


I'm not sure how are they blocked. If they are in running/runnable state and are polling for that lock. Or if they go to waiting state (and are somehow notified)?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I tried Jhon's code with synchronized(A.class){ and it worked. What I am not able to understand is when you acquire a lock on a class then basically you are gaurding against it's static variables and methods. When an instance of A class is used how come the threads are synchronized. They do not require a lock on A.class rather a lock on the instance of A class. Am I missing something.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think I got it. When synchronized(A.class) { is encountered this tries to acquire a lock on A.class as there is only one A.class the lock is obtained and the threads are synchronized. So basically if we have an instance var let say Object o = new Object(); and we synchronize on o it would yield the same result.
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic