File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

Either have object per thread OR synchronized over a writer block, a block which must be accessed synchronously.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Oct 27, 2005
Posts: 19544
    
  16

I'm moving this to Threads and Synchronization.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ...


BEE MBA PMP SCJP-6
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
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

Joined: Jan 09, 2008
Posts: 525
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

Joined: Feb 12, 2010
Posts: 266
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

Joined: Feb 12, 2010
Posts: 266
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

Joined: Feb 12, 2010
Posts: 266
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

Joined: Jan 24, 2010
Posts: 19

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

Joined: Jan 09, 2008
Posts: 525
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

Joined: Jan 24, 2010
Posts: 19

Thanks Jim for supporting my point
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
encapsulation
my thread and synchronized
Is this method thread safe?
StringBuffer Instance Variable
Thread-safe Action class ?