LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2013, 02:02 AM   #1
ArnavKumar
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Rep: Reputation: Disabled
static synchronized method


Thread locks object or its method if it is synchronized. What will happen if the method is static type?
 
Old 11-08-2013, 04:08 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Why don't you try it? My guess is that any given time only one thread will be able to execute the method in question, the others will wait.
 
Old 11-11-2013, 04:54 AM   #3
ArnavKumar
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Why don't you try it? My guess is that any given time only one thread will be able to execute the method in question, the others will wait.
what will be there if a thread trying to access an static method whose object has not been initialized?
 
Old 11-11-2013, 05:04 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Why don't you try it? Probably an exception (or coredump).
 
Old 11-11-2013, 05:13 AM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> what will be there if a thread trying to access an static method whose object has not been initialized?

I don't know what is 'the object of a static method'
 
Old 11-11-2013, 07:38 AM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
"Static methods" work the way they always do, whether or not the calls to them are "synchronized."
 
Old 11-11-2013, 10:09 AM   #7
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
8.4.3.6 synchronized Methods:
Quote:
A synchronized method acquires a monitor (§17.1) before it executes.

For a class (static) method, the monitor associated with the Class object for the method's class is used.
 
Old 11-11-2013, 07:15 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Okay, okay, here's the skinny on both of these:

A [b]static method[/i] (what other languages call a "class method") is associated with the class itself ... not with any particular instance of the class. So, you do not have to possess a class-instance to call it.

Synchronized is a convenient, lazy man's way to wrap a mutual-exclusion mechanism around a method-call ... letting the language do the dirty-work so that you don't have to. (Furthermore, it hides the operating-system specific semantics ... "here's what I mean to do, so, mister language, now just go off and do it for me.")
 
Old 11-25-2013, 12:38 AM   #9
ArnavKumar
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Question

Quote:
Originally Posted by NevemTeve View Post
> what will be there if a thread trying to access an static method whose object has not been initialized?

I don't know what is 'the object of a static method'
Static methods load at the time of class loading but if object of that class is not initialized yet, then what?
 
Old 11-25-2013, 03:10 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> if object of that class is not initialized yet

We are talking about static methods, what object are you referring to?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Java Woes: A Java Runtime Environment (JRE) or Java Development Kit (JDK) must be available ... chytraeus Slackware 10 11-27-2010 10:04 AM
JAVA: Can an initialization block take parameters from the constructor? byteframe Programming 9 04-14-2006 10:51 PM
network initialization caverat311 Linux - Networking 3 07-01-2005 03:37 PM
Initialization Scripts Drunkalot Slackware 2 09-03-2004 06:47 AM
Anal retentive initialization for Java... JStew Programming 11 05-28-2003 03:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration