"static synchronized" always required?

let's say i am writing a Class . and i want this Class to be bullet-proof.
one that will never cause users of the Class to fail.
would not i be required to define all static methods as synchronized ?
consider this method:
public static void smoothData(Map<Date, Float> data) {
}i can have an object happily using this method. then, another object starts using the
same method. at some point in the future, if the timing is just wrong, is not there a chance that
both unfortunate invokers of that method blow-up?
synchronizing instance methods are certainly different. create that object, and its yours to
manage. but static methods break encapsulation .
i, clearly, am just getting started with programming.
so, please be patient.

kappa9h wrote:
let's say i am writing a Class . and i want this Class to be bullet-proof.That's a tall order! ;-)
one that will never cause users of the Class to fail.
would not i be required to define all static methods as synchronized ?
consider this method:of course not
>
public static void smoothData(Map<Date, Float> data) {
}i can have an object happily using this method. then, another object starts using the
same method. at some point in the future, if the timing is just wrong, is not there a chance that
both unfortunate invokers of that method blow-up?may not blow up, but you could certainly get dirty/phantom reads depending on what you're doing. but a static method might just get the time of day or something and doesn't need to be synchronized...

Similar Messages

  • Static synchronized methods VS non-static synchronized methods ??

    what is the difference between static synchronized methods and non-static synchronized methods as far as the behavior of the threads is concerned? if a thread is in static synchronized method can another thread access simple (ie. non static) synchronized methods?

    javanewbie80 wrote:
    Great. Thanks. This whole explanation made a lot of sense to me.Cool, glad I was able to help!
    Probably I was just trying to complicate things unnecessarily.It's a classic case of complexity inversion. It seems simpler to say something like "synchronization locks the class" or "...locks the method" than to give my explanation and then extrapolate the implications. Just like the seemingly simpler, but incorrect, "Java passes objects by reference," vs. the correct "Java passes references by value," or Java's seemingly complex I/O vs. other languages' int x = readInt(); or whatever.
    In the seemingly complex case, the primitive construct is simpler, but the higher level construct requires more assembly or derivation of the primitive constructs, making that case seem more complicated.
    Okay, I just re-read that, and it seems like I'm making no sense, but I'll leave it, just in case somebody can get some meaning out of it. :-)

  • Multiple static synchronized methods locking the same object ?

    If I have multiple static synchronized methods in a class will all the methods lock on the same (Class) object ? I guess the answer to this would be yes. In that case is it possible achieve synchronization without an object ie code level synchronization ? If yes, how ?

    If I have multiple static synchronized methods in a
    class will all the methods lock on the same (Class)
    object ? I guess the answer to this would be yes. In
    that case is it possible achieve synchronization
    without an object ie code level synchronization ? If
    yes, how ?There is nothing special about static synchronized methods vs. any other synchronization. It's just shorthand for syncing on the Class object for that class. The effects of synchronization are identical no matter what you sync on.
    There's no way to achieve synchronization without an object. Synchronizing always obtains an object's lock. In the case of static synced methods, the object whose lock we obtain is the Class object for that class. As far as syncing goes, it's just another object.
    There is no such thing as "code level synchronization" vs. any other kind. All syncing occurs in your Java code over blocks of code. For synced methods, those blocks are the entire respective method bodies.
    Having said all that, I have no idea what you're really after or what you're trying to achieve.

  • Calling static synchronized method in the constructor

    Is it ok to do so ?
    (calling a static synchronized method from the constructor of the same class)
    Please advise vis-a-vis pros and cons.
    regards,
    s.giri

    I would take a different take here. Sure you can do it but there are some ramifications from a best practices perspective. f you think of a class as a proper object, then making a method static or not is simple. the static variables and methods belong to the class while the non-static variables and methods belong to the instance.
    a method should be bound to the class (made static) if the method operates on the class's static variables (modifies the class's state). an instance object should not directly modify the state of the class.
    a method should be bound to the instance (made non-static) if it operates on the instance's (non-static) variables.
    you should not modify the state of the class object (the static variables) within the instance (non-static) method - rather, relegate that to the class's methods.
    although it is permitted, i do not access the static methods through an instance object. i only access static methods through the class object for clarity.
    and since the instance variables are not part of the class object itself, the language cannot and does not allow access to non-static variables and methods through the class object's methods.

  • After upgrading to Mavericks on my Mac Pro, mail always requires that I type in my password when launching. How do I change that?

    After upgrading to Mavericks on my Mac Pro, mail always requires that I type in my password when launching. How do I change that?

    If your password is accepted, and Mail connects to the account, but you're prompted for the password repeatedly, take each of the following steps that you haven't already tried. Note: these steps don't apply if the server rejects the password. That's a different problem.
    Step 1
    When prompted for the password, check the box marked Remember this password in my keychain.
    Step 2
    Check that the keychain is working.
    Step 3
    Check that the default keychain is unlocked.

  • Premiere CS4 - Multiple tracks always require rendering regardless

    It used to be if you had for instance mutiple tracks and the top track was a DV-AVI file with 100% opacity and no filters, no rendering was required. Now it seems that if you have anything on the lower tracks, even if completely hidden by the top layer, rendering is always required and transcoding takes longer - sometimes considerably depending on what's on the lower layers though not seen. I actually have to completely remove sections of clips on the lower tracks to speed up transcoding. Just more unnecessary work and time.
    Forgive me if this has been discussed before, but I can not seem to find this precise issue among the slow rendering discussions.
    Bruce B

    I hope you've found the answer already, but it sound exactly what I've run into.
    My problem was that I upgraded from CS2 and then removed the suite once CS4 was running.
    If you have done the same thing, simply reinstall Premiere CS4 and it will work as it did for me.
    If not, I don't know what to tell you.
    Good luck,
    Dan

  • Can location services get disabled automatically? Or does it always require a user to disable it?

    Can location services get disabled automatically? Or does it always require a user to disable it?
    I was repeatedly trying to Find my iPhone  when it came up with "Location Services Disabled".
    Later I was able to find my iPhone again.  This has happend severally times lately?
    Cold this happen automatically, like a "time out" function?

    Can location services get disabled automatically? Or does it always require a user to disable it?
    I was repeatedly trying to Find my iPhone  when it came up with "Location Services Disabled".
    Later I was able to find my iPhone again.  This has happend severally times lately?
    Cold this happen automatically, like a "time out" function?

  • Static synchronized method

    hi,
    I am unable to understand the concept of static synchronized method.
    A non-static synchronized method gets lock on this object before entering into the method. But how this is valid for static method.
    thanks in advance

    hi,
    I am unable to understand the concept of static
    atic synchronized method.
    A non-static synchronized method gets lock on
    this object before entering into the method.
    But how this is valid for static method.
    thanks in advanceIt locks the YourClassName.class object.

  • Static synchronized

    i have a test file where a running sequnce number is stored and
    various classes from different packages are accessing it.
    Sometimes the contents of the file becomes null!
    here is an excerpt of the code :
    package com.general
    public class NumGenerator {
    public static synchronized getNum() throws exception {
    - open file
    - get number
    - increment number by one
    - write to file and close stream
    return num;
    package com.counter
    public class CounterManager extends NumGenerator {
    public void add() {
    //do someting
    getNum();
    //do something
    package com.adder
    public class AdderManager extends NumGenerator {
    public void add() {
    //do someting
    getNum();
    //do something
    I do hit exceptions when i would understand only one thread is
    supposed to access the method getNum().Any insight to to how the
    exception could be avoided ?
    And as classes from different packages are access the getNum(), does that mean there are two instances of the method for each package?
    Thank you for your time.
    - cheers

    When you synchronize a static method, the lock is the Class object. I'm in the case of a child class implicitly using its parent's static method (that is, what you'v got here), I'm not sure if the lock is the Class object for the child class--that is the class whose method is calling the synchronized method--or the parent class--that is, the class on which the method is declared.
    I would have guessed that it was the parent class, but the behavior you're getting suggests that this is not the case. If it is in fact syncing on a different lock for each child class, then malcommc's suggestion may end up giving you the same behavior. Try it, but if it doesn't work, you can guarantee you'll sync on the right object by synchronizing the body of getNum on NumGenerator.class (and taking "syncrhonized" out of the method declaration).

  • Planning/Budget Profile always required for Project Profile?

    Hi,
    Are planning and budget profiles always required fields for Project Profile?
    Thanks!
    Regards,
    Vivian

    Hi,
    Thanks for your reply.  May I know if you could advise relevance of the planning and budget profile?  what if I do not specify them in my Project Profile? What will be the impact or default values if they are not specified in the project profile.
    Thanks!
    Regards,
    Vivian

  • Intrinsic locks - static synchronized method

    I am trying to understand the "static synchronized threads" - by theory when such a thread is invoked, it has to obtain a intrinsic lock on all the static variables. I wrote a sample program, but it is not giving me the desired results.
    I have 3 threads, t1, t2, t3. t1 calls a static synchronized method crazy(), where i am using static int i. t2 and t3 calls a void function f2() and f3() which just prints i. Now i put a sleep in synchronized method crazy. I am expecting t1 to start and print i and go to sleep for 10 secs, release i and then t2 and t3 starts since crazy() holds an intrinsic lock on i. But the program calls t2 and t3 even if crazy puts the thread to sleep. What happend to the intrinsic lock on i ??
    class RunnableThread implements Runnable{
    static String i;
    void f2() {
    RunnableThread.i = "Two";
    System.out.println(RunnableThread.i);
    void f3() {
    this.i = "three";
    System.out.println(this.i);
    static synchronized void crazy() {
    try {
    i = "One";
    System.out.println(i);
    Thread.sleep(10000);
    System.out.println("Sleep done");
    catch (Exception e ) {
    e.printStackTrace();
    public void run() {
    System.out.println("Thread Name: " + Thread.currentThread().getName());
    if (Thread.currentThread().getName().equals("two"))
    f2();
    } else if (Thread.currentThread().getName().equals("three"))
    f3();
    else if (Thread.currentThread().getName().equals("one"))
    RunnableThread.crazy();
    public static void main(String args[]) {
    System.out.println("SOP from main");
    RunnableThread rt1 = new RunnableThread();
    RunnableThread rt2 = new RunnableThread();
    RunnableThread rt3 = new RunnableThread();
    Thread t1 = new Thread(rt1, "one");
    t1.start();
    Thread t2 = new Thread(rt2, "two");
    t2.start();
    Thread t3 = new Thread(rt3, "three");
    t3.start();

    lavanya.km wrote:
    I am trying to understand the "static synchronized threads"Never heard of it. You might want to clarify your terminology.
    - by theory when such a thread is invoked, it has to obtain a intrinsic lock on all the static variables. Nope. Doesn't happen.
    I wrote a sample program,Ah, I see. You're creating synchronized static methods. Those do not even come close to "obtaining an intrinsic lock on all the static variables," even if there were such a thing as an "intrinsic lock," which there isn't. A synchronized method is just shorthand for enclosing the entire body in a sync block. In the case of a non-static method, it syncs on the "this" object. In the case of a static method, it syncs on the Class object for the class where the method is declared.
    In no case does anything sync on "all the variables," static or not.

  • What does static synchronized do ?

    Hi,
    I just heard someone talking about this.
    I never really have used static synchronized block of code.
    Any ideas on what it could be useful ?
    I am thinking it will make the instance of the class synchronized irrespective of how it is called... or may be just synchronizing a static method. Which one could be correct rather.. what is correct?
    Rgds

    Boom. Thanks for the answer. Just like a static class, method, block.
    I would probably then specify something like static synchronized., if I wanted em to be synchronised. All of them can be synchronized.. should have thought about it.
    thanks again

  • 2008 r2 hyper-v guest with static IP always looses network connectivity after every restart - no problem with DHCP

    Hello,
    We are running 2008 R2 domain with one physical DC and other running in VM on Hyper-V host (2008 R2 Standard). The host has 4 NICs and is configured to use one physical NIC for itself (management) and the hyper-v guest is configured to use another dedicated/physical
    NIC (through microsoft virtual switch) just for itself.
    I noticed that after setting the hyper-v guest with a static IP address all works fine only until guest restart. When the guest boots up the IP address is still configured correctly in IPv4 properties, but there is no network connectivity at all and in fact
    the guest shows running APIPA config in ipconfig /all output. That situation continues until I remove the virtual NIC from hyper-v guest, remove the virtual switch from dedicated NIC on host and then reconfigure it (using same settings as they were). very
    annoying.
    For time being I switched the virtual DC (problematic hyper-v guest) to a DHCP IP and configured DHCP server (running on physical DC machine, not on hyper-v host) to store a reservation for the hyper-v guest so it always gets the same "static"
    IP configuration.
    Is there some kind of a problem/bug with using static IP on (2008 R2) hyper-v guests? is there a hotfix for static IP config in hyper-v guest environment?
    both 2008 R2 OSes (host and guest) are up to date with all updates (synced with Microsoft, not WSUS).

    OK, I'm not at the office now, but took my time to test out the restart scenarios on problematic virtual guest remotely.
    No dice, same as it was, everything works fine after guest has IP configured in DHCP mode (IP reservation of 192.168.1.5 for specific MAC address) and it doesn't work after restart in static IP mode (same address, works before restart of guest).
    I also took "arp -a" outputs at each step from host server and that was always saying there is only a single host (192.168.1.5 = VDC = problematic virtual guest) assigned to that IP address and always with same MAC, so that pretty much rules out
    ARP/MAC troubles and no issues with switches/routers getting spoofed. Problem is most likely with the virtual guest (WS2008R2) or within the host running same OS.
    Here are outputs:
    A) VDC has IP configured in DHCP mode - always same, survives through restart (all works)
    Ethernet adapter Local Area Connection:
    Connection-specific DNS Suffix . : CD.lan
    Description . . . . . . . . . . . : Microsoft Virtual Machine Bus Network Adapter
    Physical Address. . . . . . . . . : 00-15-5D-01-D3-00
    DHCP Enabled. . . . . . . . . . . : Yes
    Autoconfiguration Enabled . . . . : Yes
    Link-local IPv6 Address . . . . . : fe80::b9af:6679:3142:8799%13(Preferred)
    IPv4 Address. . . . . . . . . . . : 192.168.1.5(Preferred)
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Lease Obtained. . . . . . . . . . : Thursday, January 30, 2014 5:34:48 PM
    Lease Expires . . . . . . . . . . : Friday, February 07, 2014 5:35:26 PM
    Default Gateway . . . . . . . . . : 192.168.1.254
    DHCP Server . . . . . . . . . . . : 192.168.4.5
    DHCPv6 IAID . . . . . . . . . . . : 268440925
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1A-6F-5F-C2-00-15-5D-01-D3-00
    DNS Servers . . . . . . . . . . . : 192.168.1.5
    192.168.4.5
    NetBIOS over Tcpip. . . . . . . . : Enabled
    ARP -a output from host server at that time:
    Interface: 192.168.1.4 --- 0xc
    Internet Address Physical Address Type
    192.168.1.5 00-15-5d-01-d3-00 dynamic
    B) VDC has IP configured in static mode - BEFORE RESTART (all works)
    Ethernet adapter Local Area Connection:
    Connection-specific DNS Suffix . :
    Description . . . . . . . . . . . : Microsoft Virtual Machine Bus Network Adapter
    Physical Address. . . . . . . . . : 00-15-5D-01-D3-00
    DHCP Enabled. . . . . . . . . . . : No
    Autoconfiguration Enabled . . . . : Yes
    Link-local IPv6 Address . . . . . : fe80::b9af:6679:3142:8799%13(Preferred)
    IPv4 Address. . . . . . . . . . . : 192.168.1.5(Preferred)
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 192.168.1.254
    DHCPv6 IAID . . . . . . . . . . . : 268440925
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1A-6F-5F-C2-00-15-5D-01-D3-00
    DNS Servers . . . . . . . . . . . : 192.168.1.5
    192.168.4.5
    NetBIOS over Tcpip. . . . . . . . : Enabled
    ARP -a output from host server at that time:
    Interface: 192.168.1.4 --- 0xc
    Internet Address Physical Address Type
    192.168.1.5 00-15-5d-01-d3-00 dynamic
    C) VDC has the same IP configured in static mode - AFTER RESTART (no more network connectivity at all, LAN in Public zone)
    Windows IP Configuration
    Host Name . . . . . . . . . . . . : VDC
    Primary Dns Suffix . . . . . . . : CD.lan
    Node Type . . . . . . . . . . . . : Hybrid
    IP Routing Enabled. . . . . . . . : No
    WINS Proxy Enabled. . . . . . . . : No
    DNS Suffix Search List. . . . . . : CD.lan
    Ethernet adapter Local Area Connection:
    Connection-specific DNS Suffix . :
    Description . . . . . . . . . . . : Microsoft Virtual Machine Bus Network Adapter
    Physical Address. . . . . . . . . : 00-15-5D-01-D3-00
    DHCP Enabled. . . . . . . . . . . : No
    Autoconfiguration Enabled . . . . : Yes
    Link-local IPv6 Address . . . . . : fe80::b9af:6679:3142:8799%13(Preferred)
    Autoconfiguration IPv4 Address. . : 169.254.135.153(Preferred)
    Subnet Mask . . . . . . . . . . . : 255.255.0.0
    Default Gateway . . . . . . . . . : 192.168.1.254
    DHCPv6 IAID . . . . . . . . . . . : 268440925
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1A-6F-5F-C2-00-15-5D-01-D3-00
    DNS Servers . . . . . . . . . . . : 192.168.1.5
    192.168.4.5
    NetBIOS over Tcpip. . . . . . . . : Enabled
    ARP -a output from host server at that time:
    Interface: 192.168.1.4 --- 0xc
    Internet Address Physical Address Type
    192.168.1.5 00-15-5d-01-d3-00 dynamic
    Throughout the testing, the hyper-v host IP configuration and IPconfig output was always staying same.
    The Network Connection #2 is the only one the host uses (not shared with hyper-v guests).
    The Network Connection #4 is assigned to Microsoft Virtual Switch hence why it doesn't show up in results, like below:
    Windows IP Configuration
    Host Name . . . . . . . . . . . . : HYPER-V
    Primary Dns Suffix . . . . . . . : CD.lan
    Node Type . . . . . . . . . . . . : Hybrid
    IP Routing Enabled. . . . . . . . : No
    WINS Proxy Enabled. . . . . . . . : No
    DNS Suffix Search List. . . . . . : CD.lan
    Ethernet adapter Local Area Connection 3:
    Media State . . . . . . . . . . . : Media disconnected
    Connection-specific DNS Suffix . :
    Description . . . . . . . . . . . : HP Ethernet 1Gb 4-port 331i Adapter #3
    Physical Address. . . . . . . . . : 9C-8E-99-52-15-91
    DHCP Enabled. . . . . . . . . . . : Yes
    Autoconfiguration Enabled . . . . : Yes
    Ethernet adapter Local Area Connection 2:
    Connection-specific DNS Suffix . :
    Description . . . . . . . . . . . : HP Ethernet 1Gb 4-port 331i Adapter #2
    Physical Address. . . . . . . . . : 9C-8E-99-52-15-90
    DHCP Enabled. . . . . . . . . . . : No
    Autoconfiguration Enabled . . . . : Yes
    Link-local IPv6 Address . . . . . : fe80::dc78:8a3b:38a5:7af3%12(Preferred)
    IPv4 Address. . . . . . . . . . . : 192.168.1.4(Preferred)
    Subnet Mask . . . . . . . . . . . : 255.255.255.0
    Default Gateway . . . . . . . . . : 192.168.1.254
    DHCPv6 IAID . . . . . . . . . . . : 312250009
    DHCPv6 Client DUID. . . . . . . . : 00-01-00-01-1A-67-52-8F-9C-8E-99-52-15-93
    DNS Servers . . . . . . . . . . . : 192.168.4.5
    192.168.1.5
    NetBIOS over Tcpip. . . . . . . . : Enabled
    Ethernet adapter Local Area Connection:
    Media State . . . . . . . . . . . : Media disconnected
    Connection-specific DNS Suffix . :
    Description . . . . . . . . . . . : HP Ethernet 1Gb 4-port 331i Adapter
    Physical Address. . . . . . . . . : 9C-8E-99-52-15-93
    DHCP Enabled. . . . . . . . . . . : Yes
    Autoconfiguration Enabled . . . . : Yes
    On Monday I will install more test guests in Hyper-V host (WS2008R2), in variety of flavors like 7x64, 8.1x64, ws2012r2, and see if they show similar problems with static IP configuration when utilizing a dedicated NIC from host server.
    Don't get me wrong, I can live with virtual DC running on DHCP IP reservation (which is based on MAC), because the virtual DC pretty much requires a physical PDC (hosting also DHCP in my network) to be present for safety reasons ... however I prefer a static
    IP configuration on all servers, hence my question and surprise why it doesn't work.

  • Is an 'external' static IP address required to set up DNS

    Hi there
    I'm going to be setting up a OSX Server 10.3 machine as an open directory master, to take advantage of single sign-on capabilities.
    From what I have read in manuals and in this topic: http://discussions.apple.com/thread.jspa?threadID=977178&tstart=15 , it is necessary to configure the DNS service first. However, the topic talks about having an external IP address for the server, as well as for the internet router.
    Is this the only method that will work? The network only has one external IP address as far as I am aware.
    Any thoughts greatly appreciated.
    Matt

    The answer is, it depends.
    If you want your server to provide any external services then it will need an external IP address. Now, depending on your network, you may be able to use a single IP address on your router, having it NAT incoming connections via port forwarding - that's a common setup.
    The bigger issue is whether you need a static IP address or can live with a dynamic one. A static IP address is required if you're running certain services such as DNS or Mail. Other services might be able to get by with a dynamic IP, depending on what you're doing with them (for example, it's OK to run a personal web site on a dynamic IP address, but you wouldn't want to do that for a corporate web site that gets a lot of traffic.

  • Static synchronized methods

    Hi
    Why dont we declare static methods synchronized especially if they are doing some complex calculation on some passed arguments ?
    thanks
    ali

    lucky_ali80 wrote:
    Hi guys
    Thanks for your quick replies. But i am still a bit confused.Then you're making it more complicated than it is.
    You synchronize a block of code when you want to makes sure only one thread at a time can obtain a given lock for that block of code. You declare an instance (non-static) method synchronized as a shorthand for synchronizing the block that is its body on this, and you declare a class (static) method synchronized as a shorthand for synchronizing its body on the Class object for that class.
    >
    For e.g my static function looks some thing like the one below:
    public class MyClass
    public static int computeSomething ( int a , int b)
    // a number of lines of complex computation on a and b
    return result.
    Now Say its a webserver running with object1, object2, 3...10 of the same type being maintained by the server. (Not threads but objects)
    say all these objects simultaneously call my static function with their respective arguments.
    Would this cause any synchronization issues??No. As I said in my first reply, every thread has its own copy of local variables. This includes method parameters. Each thread has its own a and b here.
    Now, if you're using some other data besides a and b, and if that data is shared among multiple threads, then you may have an issue. We can't say for sure because there are many different possible scenarios.
    Edited by: jverd on Feb 10, 2009 11:58 AM

Maybe you are looking for

  • CANNOT record in plain STEREO!!! Everything outputs to MONO inste

    system specs: e6750 C2D 2 GIG DDR2 RAM X-fi xtreme gamer Abit IP35 Pro Drivers 2.00.09 Under audio creation mode I am trying to record with "What you hear" selected, and either WMA or WAV stereo. While playing a stereo mp3 with two discrete channels,

  • Problems skipping on dvd player

    Hello, I made a movie with imovie &  burned 30+ dvds for clients and tested just the first few min of each, figuring they would be good to go. Turns out they ALL skip in the same spots 16+ min into the movie. Arggg! Why is this doing this and how can

  • Dsmain Error in Windows server 2012

    HI, I created a snapshot with ntdsutil and then mounted the snapshot. when i tried to use dsamain.exe to assign the ldapport on the mounted database i get the following error. a)executed the dsamain command in CMD, I got the quota-tracking table prob

  • Item renderer problem

    I don't seem to have the correct syntax for adding a check box to a datagrid.  Here is what I have: <cust:MyAdvancedDataGrid id="dgData" width="100%" height="85%" textAlign="center" fontWeight="normal" dataProvider="{custVO}" selectionMode="singleCel

  • Servicegen and inner class

    I try to generate web service using servicegen. I got the error message: No defualt constructor was found for class ClassName$InnerClassName. I checked the inner class it does have a default constructor taking no parameter. Anyone knows why? Thanks