Lame Concurrency Question

I am seriously thread retarded. I can always get my threaded code to work without
any problems but that doesnt mean I have a clue about what is going on.
What is the best way to code the following common functionality:
class Runner implement Runnable{
public void run(){
while(run.booleanValue() == true){
do something();
Thread.sleep(some time);
public void stopRunning(){
run = new Boolean(false);
Boolean run = new Boolean(true);
}Do you just make stopRunning() synchronized?
Or do you lock on the "run" object?
If so, do you have to use a "synchronized(run)" block in both methods?
If you synchronize on an object should you use "synchronized(object)" every
place the object is used?
public void run(){
while(true){
synchronized(run){
if(run.booleanValue() == false){
break;
do something();
Thread.sleep(some time);
public void stopRunning(){
synchronized(run){
run = new Boolean(false);
}(By the way, ive never done the above. I usually just make stopRunning() synchronized.)

TuringPest wrote:
ignore my concurrency ignorance, lol, but i thought that whenever you had one
thread changing the values of something in another thread it had to be synchronized -
even if its a primitive?Yes. Or the variable has to be volatile. But, as already discussed, you don't need the running flag here.
while (!Thread.interrupted()) {
  doStuff();
  try {
    Thread.sleep(millis);
  catch (InterruptedException exc) {
    log it?
    Thread.currentThread().interrupt();
}

Similar Messages

  • Concurrency questions, Secondary Databases, etc.

    Hi,
    i have the following requirements:
    - Multiple threads, every thread opens one ore more database
    - Databases have RefCounting if they are used in more than one thread
    - Every database has a SecondaryDatabase associated
    - All Threads are performing only put operations one the databases
    - Keys and SecondaryKeys are unique (no duplicates)
    I tested with normal Databases and SecondaryDatabases:
    - no Transactions used
    - deferredWrite is on true
    Everything worked and the performance is within our expectations.
    my Questions now:
    - Does this setup work as long as all threads are only writing (put)
    (i read in another post that SecondaryDatabases work only with transactions...
    i think thats true only if you read/write ??)
    - Is there anything i should take care of? I already checked my SecondaryKeyCreator
    for concurrency issues...
    - Does it help (in this setup) to disable the CheckpointerThread? The Databases are
    synced and closed after all writes are finished. We don't need recovery...
    - Are there any penalties if i increase the LogFileSize? We are writing around 80 to 150GB
    of data and with the default size (10MB) we get a lot of files...
    - Caching is a non-issue as long as we are only writing... is this correct?
    Sorry for the amount of questions & thanks in advance for any answers!
    Greets,
    Chris

    Hi Chris,
    - Does this setup work as long as all threads are only writing (put)(i read in another post that SecondaryDatabases work only with transactions...
    i think thats true only if you read/write ??)>
    When using secondaries, if you don't configure transactions and there is an exception during the write operation, corruption can result. If you are reading and writing, lock conflict exceptions are likely to occur -- with transactions you can simply retry, but without transactions you can't. Since you are not reading, it is unlikely that this type of exception will occur. See below for more.
    - Is there anything i should take care of? I already checked my SecondaryKeyCreatorfor concurrency issues...>
    Since your secondary keys are unique, you'll get an exception if you attempt to write a primary record containing a secondary key that already exists. To avoid corruption, you'll have to prevent this from happening. If you are assigning secondary keys from a sequence, or something similar, then you'll be fine. Another way is to check the keys for existence in the secondary before the write. To do this, open the secondary as a regular database (openDatabase not openSecondaryDatabase). You don't want to read the primary (that could cause lock conflicts), which is what happens when you use openSecondaryDatabase and read via the secondary.
    - Does it help (in this setup) to disable the CheckpointerThread? The Databases aresynced and closed after all writes are finished. We don't need recovery...>
    Yes, if you don't care about recovery time, then disabling the checkpointer during the write operations will reduce the amount of disk space used and overall overhead.
    When you say you don't need recovery, what do you mean? In general, this means that if there is a crash, you can either 1) revert to a backup or 2) recreate the data from scratch.
    - Are there any penalties if i increase the LogFileSize? We are writing around 80 to 150GBof data and with the default size (10MB) we get a lot of files...>
    The log cleaner may become inefficient if the log files are too large, so I don't recommend a file size larger than 50 MB.
    - Caching is a non-issue as long as we are only writing... is this correct?The JE cache is not just a cache, it's the memory space for internal information and the Btree. For good performance during the write operations you should configure the cache large enough to hold all internal Btree nodes. The DbCacheSize program (in com.sleepycat.je.util) can be used to calculate this size.
    An exception to this rule is when you are inserting keys sequentially. If both the primary keys and secondary keys are assigned and written sequentially, then the cache size can normally be much smaller, perhaps only 10 MB. But this is an unusual use case, especially with secondaries.
    --mark                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Lame noob question that bothers me...

    Ok, this is my truly lame question: Typically, how long does the cmos battery on a mobo last? I noticed that my board came with an extra (I built this puter a few months ago but I decided to ask this now). Thanks, I'm sure this will only take one reply from one of you nice people....  

    If your system is on 24/7, then the batt can last up to 4 and half years or more.
    If you only only your system for 4 to 6 hours a day, max is around 4 years or less.
    If you only have your system up for 2 to 3 hours a day, max is around 3years.
    The battery is there to keep the system clock and BIOS settings in place when the system is not on, when the sysem is on, all these power to keep the settings will be through your PSU.

  • Lame GRUB question

    Hey all,
    Let me first say that this question will be lame (as the subject indicates).  The backstory of this post is as follows:
    I installed arch last night for the umpteenth time.  I decided, this time, to use GRUB, as I haven't used it in ages.  The configuration file looked fine and dandy so I let it sit with the defaults (autoprepared the HD as well).  When I rebooted, GRUB failed, saying something was too long for the bootrecord, or something to that effect.  It was late, so I shutdown and went to bed.
    Now my question is this:  what did I do wrong?  Let me elaborate.  I am at work and have alot to do before I get home, and I can fix this if I had the information at my finger tips.  So I am relying on the arch community for information.  The autoprepare sets up /boot (hd0,0), swap (hd0,1) and / (hd0,2), correct?  Therefore (hd0,0) should be my boot partition, and (hd0,2) my root, correct?  BTW I installed GRUB to my MBR...
    If anyone could let me know what the proper GRUB configuration should look like for an Arch auto-prepared disk, please let me know so I can get this fixed when I get home.
    Thanks in advance

    I'm here (physically, what a thought) with beniro.  His solution:
    root (hd0)
    kernel (hd0,0) root=/dev/hda3 ro devfs=nomount
    My system still boots with
    root (hd0,2)
    kernel (hd0,0)/vmlinuz-2.6.6-usb root=/dev/hde3 devfs=nomount ro hdc=ide-scsi
    I guess it's a new version of grub?  Is this not the gayest problem in the world?

  • General concurrency question

    Hello,
              We are in the process of writing an EJB-based application
              running on top of WLS in a clustered environment and wonder
              about the following:
              1. We have transactional control in a session bean to make sure
              that proper synchronization is used when concurent clients (JSPs)
              invoke services on instances of the same session bean.
              Would this work fine in the clustered environment? i.e. is any
              additional code/care needed so that the same
              transaction/synchronization control work equally well in the
              clustered environment?
              2. Some entity EJBs will certainly be replicated. Does the
              clustering automatically take care of data validity? i.e. in
              cluster 1 the database info the entity bean gets updated while
              in cluster 2 a client (say a session bean) accesses the entity bean,
              do we have guarantee that cluster 2's client gets the data as
              was updated by cluster 1's client?
              Any information would be greatly appreciated. Many thanks
              in advance!
              Please Cc your replies to [email protected], if
              not a burden.
              -Mourad
              

    The dbIsShared property of the deployment descriptor will also address the
              concerns in your second question.
              --Joe Sherwin
              Senthil Kumar S wrote:
              > Mourad Zerroug wrote:
              >
              > > Hello,
              > >
              > > We are in the process of writing an EJB-based application
              > > running on top of WLS in a clustered environment and wonder
              > > about the following:
              > >
              > > 1. We have transactional control in a session bean to make sure
              > > that proper synchronization is used when concurent clients (JSPs)
              > > invoke services on instances of the same session bean.
              > >
              > > Would this work fine in the clustered environment? i.e. is any
              > > additional code/care needed so that the same
              > > transaction/synchronization control work equally well in the
              > > clustered environment?
              >
              > Statefull session bean`s Home can be clusterable not the remote objects.
              > So if the client connects to a session bean then it will be pinned
              >
              > Stateless session beans:
              > In this case u have ot make sure the services are idempotent. The
              > implementation should take care of those duplicate calls
              >
              > >
              > > 2. Some entity EJBs will certainly be replicated. Does the
              > > clustering automatically take care of data validity? i.e. in
              > > cluster 1 the database info the entity bean gets updated while
              > > in cluster 2 a client (say a session bean) accesses the entity bean,
              > > do we have guarantee that cluster 2's client gets the data as
              > > was updated by cluster 1's client?
              >
              > It depends on the isolation level u set.
              > weblogic.com/docs51/classdocs/API_ejb/EJB_reference.html#1056190
              >
              > >
              > >
              > > Any information would be greatly appreciated. Many thanks
              > > in advance!
              > >
              > > Please Cc your replies to [email protected], if
              > > not a burden.
              > >
              > > -Mourad
              

  • ConcurrentSkipListMap concurrency question

    I am not much familiar with the internals of that class...
    Is it thread-safe to add entries to that map and in another thread to clone the map ?
    What about the ConcurrentHashMap ?
    Thanks.

    yes. You will get a consistent copy of the data.
    However if you add multiple entries as a group e.g. with addAll or putAll, there is no guarentee you will get all or none. You could get some of them but not all.
    Because these classes are concurrent, you may find you don't need to clone them. If you want to snapshot of the data, you are unlikely to need to update the snapshot in a multi-threaded manner so you can just copy the data to a plain TreeMap or HashMap.

  • Lame imapfilter question

    Hi, Everybody,
    I am hoping somebody that uses Imapfilter can help me with a seriously stupid question.  I'm trying to filter mail from the address "Certificate Services Manager <[email protected]>".  The address appears like this when I view full headers in Mutt.  My problem is that I can't seem to select by the address itself using imapfilter.  When I execute the following two queries:
    msgs = bsdad.INBOX:contain_from('Certificate Services Manager')
    print("resultant size: " .. table.getn(msgs))
    msgs = bsdad.INBOX:contain_from('[email protected]')
    print("resultant size: " .. table.getn(msgs))
    Only the first, not the second, has any rows in the table.  Has anybody seen this before?  Could anybody explain to me why this is happening? 
    Thank-you so much for your patience with my inexperience, and for your time and expertise.
    Dan

    I'm here (physically, what a thought) with beniro.  His solution:
    root (hd0)
    kernel (hd0,0) root=/dev/hda3 ro devfs=nomount
    My system still boots with
    root (hd0,2)
    kernel (hd0,0)/vmlinuz-2.6.6-usb root=/dev/hde3 devfs=nomount ro hdc=ide-scsi
    I guess it's a new version of grub?  Is this not the gayest problem in the world?

  • *.la files (possibly a lame newbie question)

    I'm a newbie packager. I have successfully installed a lot of software from source, but I have never made my own Arch packages so far. Now, I have decided to try.
    I have read the guide, and my package is correctly built. Now, it's time to make it well. When I was installing software from sources, I had many .la files. There are some files of this type in my package too. The problem is, there are no .la files in /usr/lib, on my Arch system. I have also found this news saying that I should get rid of all .la files from my package. The news is outdated though...
    The question is: should I get rid of .la files from my package? Will it work correctly after that?

    Profjim wrote:
    Snowman wrote:There is a minority of packages that needs the .la files to work properly.  You should remove the files and see first  if your app works correctly without them.  For that reason, I wouldn't make it the default setting in /etc/makepkg.conf. The default makepkg.conf has the option set to keep these files. They should be removed on a PKGBUILD-by-PKGBUILD basis with the !libtool option.
    What's wrong with making !libtool the default and having the packages that need them specify options=(libtool). Won't that also work? In fact, shouldn't that be the preferred way to write PKGBUILDs for packages that require the .la files, so that it's not dependent on how the user has configured makepkg's defaults?
    That would also work. Probably, the default is to keep the the libtool files because when we started the libtool-slay, we proceded by removing the libtool files from the packages where they weren't necessary. If you don't want it to be dependant on the makepkg.conf default, then ideally each PKGBUILD should specify if it needs the libtool files or not.
    Profjim wrote:
    Searching my install, I see:
    gnutls, libarchive, subversion have .la files and have options=(libtool...) in their PKGBUILDs
    fam has .la files but doesn't have libtool in its options. But it does have a comment saying 'Don't !libtool'. Why then doesn't it list libtool in the options?
    It wouldn't hurt to have libtool in its options. You can submit a bug report.
    Profjim wrote:libdmx, libsasl, libvorbis, and neon have .la files, but either don't list libtool in their options line, or lack an options line altogether. Shouldn't we be able to construe that as a statement that they don't require the .la files? Does anyone reading this thread have knowledge of whether any of these specific packages does require their .la files?
    The default /etc/makepkg.conf keeps the libtool file.  Either they need the libtool file (I think neon needs it) or
    the maintainer forgot to libtool-slay it.
    libvorbis doesn't have a libtool file on x86_64 so you might want to submit a bug report to have the i686 package libtool-slayed. For libdmx and libsasl: you can check if they work without libtool files and bug report if they do.

  • Concurrency question

    interface messageInterface{
       getMessage( message )
    class B implements messageInterface{
       private int A;
       private int B;
       public synchronized it(parmA, parB){
          A=parmA;
          B=parmB;
          sendMessage();
      // get message from server
       public void getMessage(message){
            methodZ( A )
       private void methodZ (int  ){
    The potential problem I see is that A and B could change, if method it() is called twice before getMessage() gets a response from the server and has a chance to use it. What are some possible ways to synchronize this better.

    It doesn't matter what the actual method calls do.
    The point is that A and B are changing on one thread when the it() method is called.
    It then sends a message to the server and wait for a response which it receives in getMessage().
    getMessage needs to use A and B for some calculation with the message it receives.
    What it really needs to do is:
    Get A and B( it() )
    send a message to server to get some more information
    Use A,B, and message from server to perform a calculation.
    But A, B could change if it() is called before a message is received from the server.
    One way it could work is that it() could wait unitl getMessage() receives it's message from server and performs it's
    calculation before returning. I imagine it could some kind of wait() notify() or thread.sleep periodically waiting for getMessage to be done.
    Just looking for some suggestions. How would the wait/notify work?

  • Newbie lamer restore question

    I have a .DMP file from an Oracle 8.1.7 server, which I need to restore into a different server runn 8.1.6. How do I do it, and do I need any additional information, control files etc?
    I'm a SQL Server DBA, and don't know where to start... :-(
    TIA,
    Alasdair.
    MCSE MCSD MCDBA

    I tried. however it said input/output error and can't create image. Should I choose 'compressed' rather than DVD/CD master. I don't understand what compressed means, but it was chosen already when i first click NEW IMAGE.

  • Java concurrency question

    I read few articles about Java Threads but I am confused over one issue.
    If I spawn a new thread (say "user thread") from my Main thread in which conditions will the Main thread wait for the user thread to complete the execution to exit the system?
    My understanding was, once the user thread is spawned, Main thread will go ahead with its execution and if it reaches end point before the user thread finishes its task, it will simply exit causing premature end of the user thread.
    I wrote a code to test it and realized that only if I exit Main thread with System.exit() function, the user thread ends prematurely. Otherwise the Main thread waits for the user thread to complete the execution (even without the join() method). In all the articles I read, this concept is not really clearly described or at least I have not picked it up properly. Could you guys please care to explain?

    A thread can either be a daemon thread or a 'regular' thread. You control this through a setter method on Thread. By default threads are 'regular' threads.
    When the main thread finishes execution, it will wait for all 'regular' threads to finish their execution first before the program exits. If these threads are not designed to finish, your program will hang waiting for them. If you only have daemon threads left running, the program will exit immediately.

  • Function Concurrency Question

    Hello,
      I have two functions which I desire to be run in paralell, but are currently not. They both read a lot of the same values (scalars), but write to different memories. However they also both access a common memory. This memory is a true dual port block RAM. Neither function writes to this memory, they only read from it. However both functions utilize both memory ports, which I believe prevents them functions from being scheduled for parallel execution. How do I verify that this is the case and if I'm correct how would I address it?
    Thanks!
    Edit: Fixed a couple of typos.

    hi
    I thought about one way of doing that.
    At the moment, it's much more cheating than a warkaround - so especially this time, verify, verify again, use at your own and verify some more.
    using similar topology as my previous examples, the trick here is to make the shared memory external - as you had tried.
    here you need an explicit synchronize the functions, otherwise from a dependency point of view the 3 sub function are totally independant and will all execute in parallel.
    once alpha() completes, foo() and bar() can start. Notice that the tool should report min latency == latency of alpha and max latency == latency of alpha + max ( latency of foo , latency of bar )
    bool alpha( ... ) {
    return 1;
    void top(
    data_t array_input[array_dim],
    data_t array_common_write_port[array_dim],
    data_t array_common_read_port_1[array_dim],
    data_t array_common_read_port_2[array_dim],
    data_t array_output_1[array_dim],
    data_t array_output_2[array_dim]
    #pragma HLS interface bram port=array_common_write_port
    #pragma HLS interface bram port=array_common_read_port_1
    #pragma HLS interface bram port=array_common_read_port_2
    hls::stream<bool> sync_stream;
    sync_stream << alpha( array_input, array_common_write_port );
    if( sync_stream.read() ) {
    foo( array_common_read_port_1, array_output_1 );
    bar( array_common_read_port_2, array_output_2 );
    Once again verify the behavior of this creative approach.
    I hope this helps!

  • Lame JCheckbox Question

    Hi All,
    My check box that is selected by default does not deselect physically but it does execute the code to do that.
    Here is the code:
    private final class CheckBoxListener implements ActionListener,
      ItemListener, ChangeListener {
    public void actionPerformed(ActionEvent e) {
    // the getIsLanguageCBx is a function to sets up the Checkbox:
    //create, add tips and other functionaly related stuff
       boolean state = getIsLanguageCBx().isSelected();
      if (e.getSource() == isLanguageCBx) {
       if (state) {
         //hide the label and combo
         isLanguageCBx.setSelected(false);
         showCombo(false);
    } else {
          //show the label and combo
          isLanguageCBx.setSelected(true);
           showCombo(true);
        public void itemStateChanged(ItemEvent e) {
           if (e.getItemSelectable() == getIsLanguageCBx()) {
               System.out
                      .println("Checkbox sent the item changed event event");
              if (e.getStateChange() == ItemEvent.DESELECTED) {
               //hide the label and combo
                 isLanguageCBx. setSelected(false);
                 showCombo(false);
                  System.out.println("In state deselected");
                 } else {
                     //show the label and combo
                     isLanguageCBx.setSelected(true);
                     showCombo(true);
                      System.out.println("In state selected");
                 }//if
             }//if
    The above is a inner class to my other class. What I want the code to do is to show certain fields if the checkbox is selected and hide them if deselected.
    About the repeated code: if you can advice on a beter way to achieve this please you are more than welcome.
    Yours,
    Me

    cuz you are reseting the state. take out the
    isLanguageCBx.setSelected(false);
    and
    isLanguageCBx.setSelected(true);
    lines. Checking it by clicking it changes the state, then fires the event, so you are getting that event and reseting it to the opposite of the new state, which is the old state.

  • Built-in Input question

    I know this might sound like I'm new to iMac, but I am so I'm sorry for such a lame silly question.
    I have a 27" iMac I use at work and I have an iPhone. I plug in my iPhone to my iMac input on the back of the computer to listen to music from my iPhone.
    Oddly, I see the Input Level indicator showing sound input but I cant get it to actually play on the internal speakers on my iMac.
    OUTPUT SETTING is set to Internal Speakers
    INPUT SETTING is set to Line In
    ...and nothing...

    Mac OS X does not automatically play audio directly from the line input to the speakers. Try the free app LineIn:
    http://www.rogueamoeba.com/freebies/
    Regards.

  • I'm trying watch videos, but the flash program is blocked by Apple.

    I'm trying to watch some instructional videos, but they say I need to download "flash". When I try I'm told that Apple blocked this, but can get something to help. I have NO idea what it is talking about  Can someone help me?

    Who ever told you that Apple has blocked flash is incorrect. Adobe has blocked flash from working on EVERY iOS device. The only thing that Apple should block is all of these lame 'flash' questions cluttering up these forums. You'll never see it snow at the equator, you'll never see pigs fly, and you'll never see any of these people asking this same "flash question" ever using the search function...  
    OK, maybe you might see the first two happen...

Maybe you are looking for

  • Why can't i find a tv show I purchased on iTunes?

    I purchased Homeland Season 1 on iTunes in January.  Since then I have updated my Apple TV software (today) and now it no longer appears in my "Purchased" TV shows on the apple TV.  I can see the purchase history on my iTunes account on my Mac but ca

  • No longer able to drag a contacts group into mail address line

    With the latest update to OSX, I cannot drag a Contacts group to the address line in Mail. How can I restore this function?

  • SVG display problem

    There is a big bug in this version. When I import my SVG in a block into a menu. The images do not appear in "active" mode on the final page. I set an example. Just play with it to understand the problem. It really hurts !! https://www.dropbox.com/s/

  • Linking pages on dreamweaver

    in creating a website on dreamweaver for a project. it isnt going on line. how do i link up my pages?

  • Elimination of Duplicates

    How can I ELIMINATE DUPLICATES once I have them. So far my only solution is to drag them to the trash, which works, and may be the only solution, but what I would really like is how to best to search them out and make the disposing of them much more