Synchronized keyword for simple addition

Hi, I have a function, as given below
{noformat}public static void increment() {{noformat}{noformat} a = a + 1; {noformat}{noformat}}
{noformat}
This function will be called from multiple threads (a is also a static variable).
My question is should I use "synchronized" keyword for this method to avoid unpredictable results? Is there a way to wirte this function without using "synchronized" keyword?
Please suggest.

Thanks, jwenting. I wanted to confirm that "synchronized" has to be used.
jwenting wrote:
yes. Depending of course on what you decide to be "unpredictable" :)you understand what "unpredictable" means here ;)
jwenting wrote:
yes, don't use a static variable but a method local one. Of course that will have a totally different effect.Variable "a" should be a class level variable. Yes, local variable has a totally different effect. ;)

Similar Messages

  • Use of synchronized keyword with portal services

    Hi,
    Can you confirm me if it is true that a portal service is a Singleton? I mean, when using an instance variable of a portal service I am able to set the value of the instance variable using one client app and get it afterwards using another client app. So we are talking about the same and only instance of the portal service, right?
    If this is true how can I synchronize the access to a portal's service method? I tried to mark
    the method syncronized (in the interface) but then I realized that this issues a compiler error because one can not mark an interface method synchronized. So can I mark the implementation class instead? That is, can I leave the interface without the synchronized keyword for the method and still mark the implementation of the method in the service class as syncronized? Does this work?
    Thanks in advance,
    Diz

    Hi,
    Portal service is not a Singleton, as the name says a service is just provider for services which does not save state between two requests/applications.
    So if you want to save state, then use some session variables to save it.
    In a cluster installation, each server node has its own portal services, so if you save state in service, then your application should save this state on all servers of the cluster.
    So you should change your approch.
    http://help.sap.com/saphelp_nw70/helpdata/en/e3/fab74247e2b611e10000000a155106/frameset.htm
    Greetings,
    Praveen Gudapati
    [Points are always welcome for helpful answers]

  • Simple addition using 7 series Gen2 integrated block for PCIe for ZYNQ7000

    Hi,
    I have been trying to perform a simple addition program using 7 Series Gen2 Integrated block for PCIe 3.0.
    Due to some issue in the design i am unable to proceed. Can anyone help me out  on how the interface has to be done what are the necessary IPcores needed and the necessary driver to execute on the host machine.
    Tool : Vivado 2014.4
    Regards
    Heramban

    Hi,
    Satish I have refered this document long time back. The issue is that this mode is making the FPGA standalone and its more complex. Am asking for a simple design where in i use 7 series integrated block and connect it to a custom IP core where it performs only addition. Generate bit stream, use a driver program (CPP file ). Execute in the host system. The CPP file will have the data to be executed. the FPGA reads the data from the HOST Machine (CPP file), executes and gives back the result in the terminal window (Host machine).
    Regards
    Heramban

  • ER Contribution for 401K additional medicare

    Hi Experts,
    How to update ER contribution for 401K Additional medicare and Pension plans?
    Will it be the same as for simple 401K ?
    Thanks in advance
    Jane

    Hi,
    Any idea how to write above query in user-exit in 'PBEN0014' SAP enhancement (SAP function 'EXIT_SAPLHRBEN00GENERAL_014')? will the limits check wil be taken care in that?
    thanks,
    vinoli Jacob

  • HT4847 If you have several Apple devices feeding to the same iCloud account, is there an amount of storage made per device available free?  So that you are rewarded for the additional devices?

    If you have several Apple devices feeding to the same iCloud account, is there an amount of storage made per device available free?  So that you are rewarded for the additional devices?
    If this multi purchase if devices is not rewarded with a larger cloud storage being available, would it then be best to have several separate cloud accounts?
    Would you lose the ability to mesh data between devices???  Of could you choose to share a smaller part of the data and yet save the rest of the device data to their own cloud?  So in other words, split a devices stored data between 2 different icloud accounts?   One community iCloud account for partial data and individual account for data that does not need to be shared with the other devices, however it needs to be backed up.  The simpler solutions would be If you have several Apple devices feeding to the same iCloud account, an amount of storage made available per device (ie: 5 units of storage free per device)
    ~ So that you are rewarded for the additional Apple devices by giving 5 more units of storage or backup space per device

    Nobody here is an Apple employee, and my Christmas Season has been great, thanks very much. Neither am I jealous, I just abhor the fact that so many people these days expect more and more for free, in addition to what they already get for free. First world problems...
    Obviously Apple do consider people with multiple devices, since the whole purpose of the service is to keep data in sync across multiple devices. Clearly, after consideration, Apple decided to give 5GB of storage for free and provide customers with the option to buy more storage if they want/need more, in line with competing services.
    Everyone has their own set of unique circumstances but does that mean everyone should be rewarded with extras for free? At what point should it be considered acceptable to actually pay for additional extras you want?
    And by the way, somebody expressing a different opinion, and discussing people's expectations is not being rude. Aren't you from the country that supposedly takes 'freedom of speech' rather seriously?

  • Object or Statement Block locking using synchronized keyword in java

    Hi ,
    Can anyone tell me how java is implementing the Object locking and the code block locking mechanism using the 'synchronized' keyword internally??
    Thanks in advance
    Raj

    The JVM implements a concept known as a monitor using a combination of direct implementation and support by the operating system. For more detail, refer to the Java JVM and language specifications.
    Chuck

  • How can see the effect of synchronized keyword

    hi
    I read the follwoing code in some books:
    class W{
    static private int count=0;
    public String name;
    int no;
    private static synchronized int addW(){    return ++count; }
    public W(){ no= addW();  }
    }but I think that synchronized keyword in this code doesn't have an effect, i.e., if we remove it, the output would be ok, can anyone able to change this code such that the output becomes different depend on existance of synchronized keyword.
    thank you

    but I think that synchronized keyword in this code
    doesn't have an effect, i.e., if we remove it, the
    output would be ok,Well, you're mistaken.
    can anyone able to change this
    code such that the output becomes different depend on
    existance of synchronized keyword.No change is necessary. Synchronization is required to ensure proper behavior for the code presented.

  • Routine for simple formula

    hi everyone,
    is there anyway i could find a routing for simple mathematical functions like addition, subtraction, divison and mutliplication? please send me the routines people.
    thanx in advance

    Transfer  routine:
    Date key figure: Convert DATS format to DEC format
    Date Conversion in Flat File
    Transfer Routine ABAP
    conversion exit
    Date Conversion
    Problem with  conversion char to numc in transference rules
    conversion routine..?
    Start routine in Transfer rules:
    Look up to load master data
    excluding
    Start Routine in Transfer Rules
    Sample code in Update Rule to restrict data selection?
    Append Datapak - transfer start routine
    Excluding Blank field in Infopackage Filter
    Trans Routine ABAP help ..
    Update routine :
    How to call a function in Update Rules to reverse a key figure sign
    Need Sample Code for Update Rules
    Date calculation
    Difference between data in PSA and Data in Infocube
    No RETURNCODE <> 0 allowed in update routines in transfer mode
    the diffrerece between DEC and (yyyymmdd) 8 char in Time defination
    Access master data in update rules
    Date key figure: Convert DATS format to DEC format
    Is it possible to read a third ODS in update rules between two ODS?
    update rule coding while loading data from ODS to CUBE
    Start Routine in Update Rules to populate additional fields from ODS
    Coding in Update Rules accessing a Z-Table
    Start routine
    update routine
    update routine code
    Re: Difference for Start routines n update Routines.
    Re: Update rules vs Transfer rules
    Re: which is better transfer routine or update routine???
    Re: Start/Transfer/Update Routines
    http://help.sap.com/saphelp_nw2004s/helpdata/en/e3/732c42be6fde2ce10000000a1550b0/frameset.htm
    Update routines :
    http://help.sap.com/saphelp_nw04s/helpdata/en/80/1a64fae07211d2acb80000e829fbfe/frameset.htm
    Diff b/w Start & updated routine

  • Implementing method and add 'synchronized' keyword

    Basically, I have the following scenario:
    public interface ClassA {
      public void doSomething(String s);
    } Then, the class that implements ClassA:
    public class ClassB implements ClassA {
      public ClassB() {
        doSomething("Testing");
      public synchronized void doSomething(String s) {
        // Do something...
      public static void main(String[] args) {
        ClassB cb = new ClassB();
    }ClassB implements the doSomething(String s) method but adds the 'synchronized' keyword. I compile this and get no errors, but I wanted to make sure that adding the 'synchronized' keyword to a method that is implemented is a legit thing to do.
    The reason I am asking is that Rational Rose does not generate code for this situation correctly, and before I call tech support, I wanted to make sure that it is not a problem with what I am doing. When Rose generates the code, it gives a warning that the method doSomething was not implemented and it adds the method. I then have two methods called 'doSomething'.
    Thanks

    'synchronized' is not part of the method signature. But if you want to work around the problem, instead of declaring the method as synchronized, simply do this instead, which is equivalent (for non-static methods):
    public void doSomething(String s) {
      synchronized (this) {
        // your code here

  • A keyword for marking default protection

    Today java is missing a keywork for marking default (package) protection. This is marked just by not using private, protected or public.
    To me code with a explicit defined protection is easier to read than code where protection is defined by not using any keyword.
    I think java should have a keyword for explicitly declaring the protection level.
    I have searched bugs.sun.com for an issue about this, but didn't find any.
    Does anybody here has any thoughts about this topic?
    -Kaj :)
    Edited by: kajh on Apr 19, 2008 12:50 PM

    DrClap wrote:
    An optional package-private keyword would be just another source of confusionThen don't tell anyone about it :). Those who wanted it will find out, one way or another.
    I agree that I wish package-private had its own keyword, as I think it would make code more readable (a compiler preprocessor instruction forcing yourself to specify access level might be nice too). Personally, because there was no keyword for package-private, I spent the majority of my Java life thinking that the default (no keyword) was the same as "protected", because I assumed it had to be one of the three (public, protected or private). So from my experience anyway, not having a keyword is introducing a lot more confusion than adding one late in the game.
    The biggest problem I'd see with the addition would be that they haven't reserved a suitable keyword, and introducing a new keyword will either 1. run the risk of breaking existing code (identifiers using keywords added late) or 2. increase parser complexity.
    Could always use the default keyword in a separate context though:
    public class Clazz  {
       default int myData;
    }Or change the grammar rules for declarations:
    public class Clazz {
       package private int myData;
    }

  • Understanding 'synchronized' keyword

    Hello,
    I have a main class (ClsA.java) that extends JFrame, the frame contains one JButton and one JTextArea.
    When pressing the button x number of threads are created and each thread will write a couple of messages in the JTextArea by using a method in ClsA.java, the method looks like this:
    public void output(String output){
      this.taOutput.append(new java.util.Date(System.currentTimeMillis()) + ":" + output + System.getProperty("line.separator"));
    }everything works fine, but sometimes a few messages from different threads end up on the same line in the JTextArea. To fix this I changed the output method to this:
    public synchronized void output(String output){
      this.taOutput.append(new java.util.Date(System.currentTimeMillis()) + ":" + output + System.getProperty("line.separator"));
    }I have just added the synchronized keyword that will make the the system associates a unique lock with every instance of ClsA (there is only one). This works alomost fine, but when I increase the number of concurrent threads the application freezes(I�m not able to scroll down in the textarea or press the button)? Why does it freeze? I have tried adding notifyAll() at the end of the output method but there is no difference. What am I doing wrong here? The acquisition and release of a lock is done automatically and atomically by the Java runtime system.....but can it still end up in a freeze?!
    Greatfull for any suggestions!
    //Anders =)

    Hi,
    Use
    public void output(String output){
    SwingUtilities.invokeLater(new Runnable()
    public void run()
    taOutput.append(new java.util.Date(System.currentTimeMillis()) + ":" + output + System.getProperty("line.separator"));
    so as to update the text area only in the event thread!
    Roger

  • How to let add external apps for simple users?

    How to let add external apps for simple users? Simple users don't have login server administer rights, so how can they add external apps for their use?

    ...Edit External applications portlet sais:
    Error: An unexpected error occurred: User-Defined Exception (WWC-43000)
    :(((

  • How to use Value Mappings for simple translations?

    Hello,
    I want to use Value Mappings for simple translations in mappings, e.g. from IDoc to Inhouse structures.
    For example unit of quantity:
    IDOC    -->    INHOUSE
    PCE               P
    ABC               A
    How can I use Value Mapping for this? What should be used as Agency, what should be used for Scheme? What about groups? I tried the following: I created a new Value Mapping in Integration Directory:
    - Source Agency: DELVRY05
    - Source Scheme: MENEE (IDoc field name)
    - Target Acency: INHOUSE_DESADV (Name of structure)
    - Target scheme: UNIT (Name of field)
    Then, in the table, I added several lines for translating PCE to P and ABC to A and so on. But I have to define a group name for each line. I used INHOUSE. But than I get one INHOUSE group for each line.
    This seems very complicated for simple translations from A to B. I don't want to use FixValue in Message mappings.
    Any help appreciated.
    Thanks,
    Christoph

    Hello,
    @pavan kumar: Thanks, but I know all Blogs about Value Mappings. That does not help me. And I refer to PI 7.1.
    Lets get it very simple: I want to have exactly the same functionality of "FixValues" as ValueMappings. In 7.1, I need to define a Group for every row / line in the conversion table (e.g. for units of measurement conversion between IDoc and FlatFile). That does not make sense for me, as the Group is always the same, e.g. "Unit of measurement". So I will get dozens of same groups called "Unit of measurement".
    I don't really understand the concept of Groups. Maybe this is just not appropiate for my intenses? Maybe the Group has to be defined as one specific value of Unit of measurement, e.g. "pieces"?
    CHRISTOPH

  • HT201318 Can I pay for an additional iCloud storage with an Apple gift card?

    I was just wondering if you can pay for an additional iCloud space with and Apple gift card?

    From the bottom of that article:
    Note: You cannot use Apple Store gift cards as a form of payment to upgrade your iCloud storage (payment methods accepted include iTunes store credit, credit cards, and debit cards).
    (80324)

  • Mail help! Looking for an additional Mail App for Exchange!

    I have been desperately looking for an additional (not the built in) app so I can read my corporate email on my iphone. Does anyone know of such an app?
    Thanks...

    With the general Mailbox I got 4 mailboxes. 3 that I always have to be alerted when mail comes in, the general one I have to check in only once in a while. I always have all mailboxes open, however if I have the darn general mailbox in there i loose control over what's important. So I use safari right now for that busy mailbox which is cumbersome.
    I just wanted a separate mailbox that doesn't alert me every 2 minutes of incoming mail, yet easy enough to check what's in it when I need it.
    I'll give ibismail a try. Thanks.

Maybe you are looking for

  • Lack of VIP and Failover

    DB Version : 10.2.0.4 When VIP is functional , the CONNECTION REFUSED errors are quickly detected, and transparently the connection request will be transferred(fail over) to the next available IP. If something goes wrong with your VIP , the failover

  • Call Custom JSP from Screenflow JSP

    I need to call a static custom JSP(JSP2.jsp) from Screen flow JSP(JSP1.jsp) in Oracle BPM Studio while running Workspace. How do I do it? What will be the context path of JSP2.jsp while running workspace? JSP1.jsp has some context path created by Wor

  • Compiler Error Message: BC30451: Name 'UserEmail' is not declared.

    Have the message when I test the page. Anyone sugest a solution please? Compiler Error Message: BC30451: Name 'UserEmail' is not declared. Page code below. <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %> <%@ Register T

  • Can I Create and Sell  Commercial Scripts with ADDT?

    After seeing this utility in action, I would really like to use it to develop and sell scripts commercially. Will the ADDT licensing allow me to use the plugin to develop and sell scripts (like on hotscripts.com)? The files that ADDT created (include

  • MCE errors with kernel 2.6.32.x

    I'm running a dual Xeon machine, x86_64, 8G memory, Radeon HD 4350 graphics.  Back in February, I upgraded the system and went from the 2.6.31.6-1 kernel to the 2.6.32.7-1 kernel, at which point the machine started doing random reboots and reporting