Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java we need to use "synchronized" keyword in order to make that method thread safe.

A question : If there are Million threads accessing this method, as I used synchronized all the other threads has to wait, which makes the application to run slower. So how can I achieve thread safe with out using synchronization in this situation ?

Thanks
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Either have object per thread OR synchronized over a writer block, a block which must be accessed synchronously.
 
Henry Wong
author
Marshal
Pie
Posts: 21122
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jose chiramal wrote:In Java we need to use "synchronized" keyword in order to make that method thread safe.

A question : If there are Million threads accessing this method, as I used synchronized all the other threads has to wait, which makes the application to run slower. So how can I achieve thread safe with out using synchronization in this situation ?

Thanks


First, what do you mean by "run slower"? If you are thinking that waiting threads actually take up processor time, they actually don't. The running thread runs just as fast with one or many threads waiting for it.

Now, if you mean, that lots of waiting threads can bottleneck, and hence, feel slower as an application... and you are asking on how to code many threads without using synchronization, take a look at the classes in the java.util.concurrent.Atomic package. But be warned, optimistic synchronization is not for the faint of heart, if you think that getting pessimistic synchronization correct was hard...

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is related to your previous questions regarding Struts actions and servlets, it might be helpful to describe the problem you think you're having before questing towards a solution that actually works; as Henry said, this can get complicated, quickly.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to Threads and Synchronization.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have found the synchronized syntax to be somewhat misleading.
Since "synchronized" appears in method signatures, we talk in terms
of synchronizing on the method. The correct concept is that we are to
synchronize on the "this" object; my thread must secure its lock before
proceeding. Multiple object users must queue-up to get the lock. For
better speed, try to synchronize on just part of the method. A language
example is StringBuffer versus StringBuilder. The latter is recommended
as faster because it is not thread-safe (synchronized).
Jim ...
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just happened to think of this scenario , so thought of asking. "In synchronisation, once if a thread enters a synchronised method on an object, no other thread can call any other synchronised method on the same object." My questions :

1. Can we call synchronised on two different objects at the same time ?? or is it only restricted to one per thread per object ?

2. When there is a thread that calls a synchronised method on an object can we call any other methods of the same object in any other thread ?

Thanks. Just trying to understand the statement in ""
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Can we call synchronised on two different objects at the same time ...

The term thread refers to "a thread of execution," one instruction
at a time. So if a thread is waiting to secure an object's lock flag,
at the entrance to a synchronized method, it cannot do something
else also, like look for a second lock.
... can we call any other methods of the same object in any other thread?

Yes, other threads may call any method, and often they call the same method.
(The method is synchronized to protect object data from multiple-thread attacks.)
For example, thread-1 calls a synchronized method, the object containing the
method is locked and the method executes doing work for thread-1. Meanwhile,
thread-2 calls the same method. But it has to wait because the object is locked.
The method completes, the lock is released, and thread-1 continues on its way.
The lock is then given to thread-2 and the method executes again, working for
thread-2. Many threads can queue-up for the same method and each in turn
will secure the object's lock.

Remember that it's the object's lock flag that controls entry into synchronized
methods, or synchronized blocks of code. I hope this helps.

Jim...
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your response Jim Hoglund :

I have tried answering the questions that I asked in the previous post. Please correct me if am wrong.

1. Can we call synchronised on two different objects at the same time ?? YES (only in different threads, not the same thread) or is it only restricted to one per thread per object ? (Yes, at a time only one thread can access the methods of any object)

2. When there is a thread that calls a synchronised method on an object can we call any other methods of the same object in any other thread ? In any other thread , yes we can.
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In threading concept , do we say that the lock is acquired on the object or lock is acquired on the method ?

I think it is only the object that has a lock so the locking concept refers only to objects and not method of the object ?
 
jose chiramal
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In threading concept , do we say that the lock is acquired on the object or lock is acquired on the method ?


Or is it both ??

Please reply.
 
Ryann Ong
Greenhorn
Posts: 19
Fedora Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your question, basically if you have millions of thread accessing a same method with a "synchronized", what happens is only one thread can access it at a time, since only one prevails, you will face a problem here called bottleneck. After a thread exits to the method, you will not be able to keep track which thread will succeed after that thread.

There are many ways to implement a thread safe application,

- you can use synchronized block to control which part do you want to be exactly locked at the given moment, providing an object paremeter as the locking object
- you can use singletons to handle your object and lock only the object not the class
- use a library which supports concurrency issues properly

And i think if you're using synchronized methods, it locks on the class not on the object
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose : Are your questions answered? To clarify Ryann's comment a bit,
the class object is locked for static synchronized methods. Otherwise,
for non-static methods, an instance is locked; typically the 'this' object.
For a synchronized code block, however, you can lock any object.

Jim ... ...
 
Ryann Ong
Greenhorn
Posts: 19
Fedora Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim for supporting my point
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic