my dog learned polymorphism*
The moose likes Threads and Synchronization and the fly likes Static methoods are not synchronised Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Static methoods are not synchronised" Watch "Static methoods are not synchronised" New topic
Author

Static methoods are not synchronised

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Am accessing a static method from the run method.

When i access the same method
1.with an object it goes synchronised with the thread.
2.accessing that as static method, the thread just gives a call to
that method keeps executing the next line.
inspite of that calledStaticMethod getting completed.

Do threads vary from the way it calls a static method.
and the way it calls the method with an object.

i need to make the static method synchronised to make it go along with
the thread.

Why this variation ?

What is the need ?

What advantage does it have ?

thanks for your replies.


Code :




Discussion - the powerfull way to excellence!
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Kindly PostRealCode.
I am not very clear about your question and the code sample does not really explain it any better.

As far as I understand, you *feel* that when you call an unsynchronized static method it runs parallely whereas an unsynchronized instance method runs sequentially.
This is not the case. Probably if you post the code which makes you feel like that, we can help you clarify your doubt.

The only time when a method is executed in parallel to the current thread is when the method is executed in a thread different than the current thread. There are no exceptions to this rule.


apigee, a better way to API!
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Nitesh Kant:
Kindly PostRealCode.
I am not very clear about your question and the code sample does not really explain it any better.

As far as I understand, you *feel* that when you call an unsynchronized static method it runs parallely whereas an unsynchronized instance method runs sequentially.
This is not the case. Probably if you post the code which makes you feel like that, we can help you clarify your doubt.

The only time when a method is executed in parallel to the current thread is when the method is executed in a thread different than the current thread. There are no exceptions to this rule.


Yae, what you meant is right !

As far as I understand, you *feel* that when you call an unsynchronized static method it runs parallely whereas an unsynchronized instance method runs sequentially.



This is the case.

I feel like that !

Am i speaking at right sense !
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

ram:
Am i speaking at right sense !


No!
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Ok ! Nitesh get my code : I realize what you said is right.

May be my code would do a cross confirmation.

If you could please alter this code that would explain me

how some thing can go unsynchronized(run parallely) within in the same thread.

Hope!There is no possibility as per my understanding from your advice.

















Save them seperately and execute the main class.ThreadRunner.java.

To Add Up Why i posted this question.

In the logs :

##################### T-1 - Started ############

##################### T-2 - Started ############

STATICClassAndMethod : staticMethod : 20

##################### T-1 - ENDED ############

STATICClassAndMethod : staticMethod : 20

##################### T-2 - ENDED ############

##################### T-1 - Started ############

STATICClassAndMethod : staticMethod : 20

##################### T-1 - ENDED ############

##################### T-2 - Started ############

STATICClassAndMethod : staticMethod : 20

##################### T-2 - ENDED ############

##################### T-1 - Started ############

STATICClassAndMethod : staticMethod : 20

##################### T-1 - ENDED ############

##################### T-2 - Started

############ STATICClassAndMethod : staticMethod : 20

##################### T-2 - ENDED ############

Watch the first few lines, i got confused for my self that this would be some problem with SYNChronization.(watch the lines T1 and T2 mixes)

Thanks for the Advice that you have given.

I understood;

#1. Every method call Runs Sequentially inside a thread. including those lines of the method that is called.

#2.Synchronized key word is specified to make any other thread wait, when one thread is already using it.

Am i right about the statements !

Please cross verify if am right ?
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Nitesh Kant:


No!




Also, please would some one just tell me

where can i add wait and notify to synchronize the - any static or instance method.in the above example
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methoods are not synchronised
 
Similar Threads
Threads and Synchronization examples
From Velmurugan's Notes
What is wrong with my code?
A question for beginners
Stopping and starting thread for multiple key clicks