• 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

Synchronized method

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'm kinda new to Threads, so I get a bit confused here.
Please consider the following code:

I'm creating two threads here, with both of them accessing the same sychronized method. AFAIK about threads, one thread will access the synchronized method, and the other thread will have to wait until the first thread finishes. (pls correct me if I'm wrong )
But, somehow that's not what happened. The output I'm getting is pretty much the same as a non-synchronized method.
Here's what I get on WindowsNT:

Shouldn't those two threads take turns in accessing a synchronized method?
please anyone enlightened me on this..
any help is highly appreciated
thanks in advance
- eric
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But each Thread has its own object - the Threads are not trying to access the same object.
Bill
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here 2 different objects p and o using the sync methods.If it is one object then that Thread has a lock for that object and no other Thread can have this lock until the first thread has finished the method.
Hope I was good.
Tahnx
Rajani
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all,
I've created ten threads of the same object and run as I expected. Please check the following code:

My next question would be: why do I get an IllegalThreadStateException if I use the commented line instead?
Please anyone let me know..
Thanks again.
- eric
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eric,
The exception is caused because you are trying to start the thread that has already been started.
new Thread(o).start() // you are starting a thread on o
o.start // here you are trying to start it again so you get IllegalStateException
Hope it helps,
Vanitha.
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Vanitha,
That clears my doubt.
- eric
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Vanitha,
If i comment out new Thread(o).start();
and then use o.start(); there then in runtime the code partially runs and reports the same IllegalThreadStateException. Why?

------------------
azaman
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Azaman,
Good Catch!


At (1) you create a thread out side the for method and start it at (2), (2) will not create a new thread . But (4) will create a new Thread so you didn't get runtime exception when you use (4).
But if you create a thread inside a method (3) it will create the o everytime and start it, so you won't get exception.
Correct me if I am wrong,
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited August 06, 2001).]
[This message has been edited by Vanitha Sugumaran (edited August 06, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic