• Post Reply Bookmark Topic Watch Topic
  • New Topic

Based on Threads

 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have some work to do which uses Java Thread Feature.

It has mentioned 'Synchronized Methods', but I don't know what these mean, I have made some search but its quite vague..

Can someone tell me what it is , & maybe how it maybe used in this context of Threads.
 
Calum Ludwig
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Making a method synchronized will not allow 2 invocations of synchronized methods on the same object to run simultaneously. While one thread is executing a synchronized method, all other threads that wish to call other synchronized methods on the object are suspended until the first thread is done.

Also once the thread exits the method all changes to the state of the object are visible to all threads no matter where they were suspended in execution.

Please see this pageif you have not done so already.

Hope it helps.

Calum
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read the notes, but It is not easy to understand.

class obj
void synchronized g()
{}
void synchronized s()
{}

If I hav an instance obj obj1 = new obj()
e.g. 1: Can these 2 run at the same time?
Thread 1: obj1.g()
Thread 2: obj1.g()
e.g. 2: Can these 2 run at the same time?
Thread 1: obj1.g()
Thread 2: obj1.s()
[ November 08, 2007: Message edited by: Christian Long ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For both of your examples, the answer is no, they can't run at the same time.
 
Christian Long
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have wrongly asked my question infact.. I have made as much tests as I could, and I have learned a bit more on 'synchronization'.

class obj
void synchronized g(char i)
{Display "g: " + i}
void synchronized s(char i)
{Display "s: " + i}

If I have an instance obj obj1 = new obj()

Thread 1: for(i=0 to 7) obj1.g('1')
Thread 2: for(i=0 to 7) obj1.g('2')

Thread 1: for(i=0 to 7) obj1.g('1') : 2nd example here: synchronized or not, this does not change anything
Thread 2: for(i=0 to 7) obj1.s('2')

1st example:
when 'not synchronized': g: 1 g: 2 g: 2 g: 1 g: 2 g: 1 g: 1 g: 1 ....
when 'synchronized': g: 1 g: 1 g: 2 g: 2 g: 2 g: 2 g: 2 g: 2 g: 2 g: 1.... - here all the g: 2 are always grouped together, but what I don't understand is why the g: 1 are not..
When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

[ November 09, 2007: Message edited by: Christian Long ]
 
Calum Ludwig
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be the way the JVM does the thread prioritizing, there�s no much control in this. Try the experiment with both non-synchronized and synchronized methods but with each thread calling sleep().

Might show some different results.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!