StringBuffer when used with multi thread show output in UnSynchronize way.

below is my code :-
public class Two implements Runnable
     //thread safe
static StringBuffer builder=new StringBuffer("output:1");
Two(StringBuffer builder)
          this.builder=builder;
     public void run()
          System.out.println("Printed by "+Thread.currentThread().getName()+" : "+builder.append(" output:5"));
public static void main(String s[])
     builder.append(" output:2");
     Two d1=new Two(builder);
     Thread t1=new Thread(d1);
     Thread t2=new Thread(d1);
     Thread t3=new Thread(d1);
     t1.start();
     t2.start();
     t3.start();
     builder.append(" output:3");
     builder.append(" output:4");
     System.out.println("Printed by "+Thread.currentThread().getName()+" : "+builder);
when the program is executed it show different output in corresponds to the order of call and it is not showing thread saftey.

thanks tolls ,
below is my output : -
first time when i run the progra"_
Printed by Thread-1 : output:1 output:2 output:5 output:5 output:5
Printed by Thread-0 : output:1 output:2 output:5
Printed by main : output:1 output:2 output:5 output:5 output:5 output:3 output:4
Printed by Thread-2 : output:1 output:2 output:5 output:5
second time when i run the program_
Printed by Thread-0 : output:1 output:2 output:5
Printed by Thread-2 : output:1 output:2 output:5 output:5 output:3 output:4 output:5
Printed by Thread-1 : output:1 output:2 output:5 output:5
Printed by main : output:1 output:2 output:5 output:5 output:3 output:4
see the order of output. there is no synchronization among threads . Where as StringBuffer is thread safe but the order of output is different at each time. My question is just that
how to achieve synchronization among threads.

Similar Messages

  • I have created a PDF with mp3 audio clips, the file works on PC's with audio playback (WIndows 7, 8 ) however the audio does not play when hosted on a website or an ipad/tablet computer or smart phone when used with PDF reader

    I have created a PDF with mp3 audio clips, the file works on PC's with audio playback (WIndows 7, 8 ) however the audio does not play when hosted on a website or an ipad/tablet computer or smart phone when used with PDF reader

    adobe42135678 wrote:
    the audio does not play when hosted on a website...
    When viewing in what browser?

  • Subscription cannot be used with multi-user device...

    I just received an email message stating "Subscription cannot be used with multi-user devices" and "Here's a quick reminder that Skype's subscriptions are for individual use only and cannot be used with multi-user devices such as PBXs"
    I'm not sure why i received it, I am the only user and I only call out via my Iphone wifi and, occasionally, my PC.
    Could anyone please explain this to me?
    Thanks - Steve

    Hi Steve,
    Basically as long as you don't make subscription calls from more than one device at the same time there shouldn't be an issue.
    I'll look into this messaging as it seems to be bit misleading.
    Could you check your email, I sent you an email asking bit more information around this notification.
    Andre
    If answer was helpful please mark it with Kudos and if issue is resolved mark it with solution. This will help other users find this answer more easily. Thanks in advance!

  • ExecuteNonQuery doesnt return number of rows affected when used with PL/SQL

    ExecuteNonQuery doesn't return number of rows affected when used with PL/SQL but returns -1. The documentation says ExecuteNonQuery method of OracleCommand returns the number of raws affected but when used with PL/SQL, it returns -1 even the update statement successfully executes and affects multiple records with no exeptions thrown.
    However, if ExecuteNonQuery is used with update sql statement in the asp.net code it returns number of rows affected correctly. Does anybody know why ExecuteNonQuery behaves like this?
    I am using ODP.NET that comes with Oracle Client 11g. The version is 2.111.6.20 of ODP.NET, Oracle database server is 10g.
    Marat

    You can get the rowcount in the stored procedure via SQL%ROWCOUNT and then pass it out as a parameter.
    For example:
    SQL> create or replace procedure updatesal(v_deptno in number, v_rowsaff out number) as
      2  begin
      3  update emp set sal = sal * 5 where deptno = v_deptno;
      4  v_rowsaff := SQL%ROWCOUNT;
      5  end;
      6  /
    Procedure created.Cheers

  • Test runner freezing when used with Dynamics CRM (and other apps) - Both applications freeze and never come back...

    We are trying to use test runner with dynamics CRM.
    MS Dynamics CRM will freeze and will not respond (even after 10 - 15 minutes). When this happens, the test manager will either freeze or a message will apear saying "waiting for application under test".
    We are not recording action recordings...This makes using test manager impossible. It happens consistently, starting with slowing the PC down (mouse moving slowly, slow to load in new screens, drop downs etc) eventually leading to the app freezing.
    Tried on windows xp, windows 7 etc on various specs of pcs.

    I am experiencing this same issue with
    Test runner freezing when used with Dynamics CRM. I notice this was first posted in June of 2010 and so far no
    resolution? I was wondering if it could be related to memory issues? Do you have any troubleshooting ideas? The
    issue for me seems to be sporadic and I can't
    reproduce it every time.
    The message I get is "waiting on application under test".
    DLM

  • Quicktime Player with the magic mouse, when used with a bug -

    On 10.7 Lion, Quicktime Player with the magic mouse, when used with a bug …
    Quicktime Player will not respond, but use the USB mouse with no problem

    Message was edited by: b j t

  • Msi x99 gaming 9 ack does not detect capture card when using with xsplit

    msi x99 gaming 9 ack does not detect capture card when using with xsplit
    i install every but still it does not work.

    Hi
    It wasn't hard to find your motherboard version:
    http://www.msi.com/support/mb/Z68AGD80_B3.html#down-bios
    Since your BIOS is v20.1, it couldn't be G3 
    Apart from checking BIOS, you can only check motherboard or moth...

  • Camera is hanging when using with flash

    Hi Lenovo Team,
    In lenovo vibe x2, camera is hanging when using with flash.Getting "Lenovo snapit isnt responding" error and screen hanged for sometime.after that when tried to open camera "Cannt cannot to camera " error is coming till when restart the phone.
    Even tried Factory reset and software update.
    Please provide the solution for the above issue.
    Thanks,

    Hi vijayprabhu,
    I just tested the X2 however am not having the issue. I set flash to always on then took a picture using SnapIt however its fine.
    Please check if you have the latest firmware S125.
    Check out the Community Knowledge Base for hints and tips.
    Did someone help you today? Press the star on the left to thank them with a Kudo!
    If you find a post helpful and it answers your question, please mark it as an "Accepted Solution"!
    X240 | 8GB RAM | 512GB Samsung SSD

  • HT5922 I have some devices that i no longer use with AirPlay. What's the best way to remove those devices from the AirPlay List?

    Bonjour,
    I have some devices that i no longer use with AirPlay. What's the best way to remove those devices from the AirPlay List?

    In article <e35sc3$ru1$[email protected]>,
    "-->dan mode" <[email protected]> wrote:
    > > All
    > > I have a website I would like to remove from the
    internet, However I am
    > > planning on keeping the IP for a development
    location and would like to
    > > put
    > > a page up that says this site is no longer
    available. Is this how you do
    > > it
    > > or are there other pages to display when a site is
    removed.
    > > Thanks
    > > Dave
    Depending on how long the site has been active, it may be
    already
    spidered by search engines and cached. However, for the most
    part, I
    usually use my FTP program OR even Dreamweaver itself (now
    that I can)
    to delete files on the server just as I do locally. Hit the
    delete and
    confirm

  • When using an external (ergonomic mouse) is there any way to get the wheel to scroll one full page at a time?

    When using an external (ergonomic mouse) is there any way to get the wheel to scroll one full page at a time? Much appreciated.

    I can't use the keyboard to scroll. I need to keep my hand on the mouse, otherwise I'd never get anything done. Since I have no problem getting Firefox to scroll one page at a time, and I'm constantly jumping between 3 browsers, it would be nice to be able to scroll a full page at a time in Safari, just like I can in Firefox. Thanks.

  • Proble with multi-threaded app using the same connection

    have a multi-threaded app. The threads all share the
    same connection. They all do some selects / updates.
    The OS is NT 4.0 and the application server is weblogic
    Some times I am getting the error "java.sql.SQLException: Io exception: not in streaming mode"
    and some times I am getting "java.sql.SQLException: Io exception: Protocal violation
    Any Ideas?
    Regards
    Suresh

    Don't create threads.... Your threads should if they are necessary be reusable, your threads should call the connections it needs from the cache service with let's say a max number of N pools (each pool a max number of M connections), if the connection domain asked exists on cache a connection is asked for corresponding pool, if the connection domain is not on cache the less used domain is thrown away (all inner connections closed first) and substituted by the new one requested. While the pool is not full keep creating connections as the request came (and all existing connections are being held and not released), when M connections exists, a request should wait till a connection is released.
    If you can have 20 threads asking for the same connection and the max number of connections on pool is M=10, then yopur threads will wait for a connection being released. All this must be synchronized. You should set M the closest as possible to the normal number of requests you can imagine to happen the same time. You should get connection use it and release it right away if you can have a lot of requests.
    I hope this helps you.

  • Trouble with DAQmxReadDigital Timeout (200284) when used in a threaded loop

    Hi,
    I'm using a NI6537 for fast Digital Acquisition. The board is installed on a NIPXIe-1062 crate controlled by a NI PXIe-8130 controller, where the software is running. I'm reading all the 32 lines as a single channel, at 50MS/s, and the clock is the one provided by the board itself. I put the timeout as infinite, but I also tried various other settings. As I'm reading a finite samples bunch of 524288 samples, at 50 MS/s, a timeout of 1 second should be by far sufficient.
    As far as I put the DAQmxReadDigitalU32(...) call in a simple loop I have no troubles, everything working fine.
    for (...) {
       DAQmxReadDigitalU32(...);
    } // That loop works perfectly
    while, if a have a loop inside a multi-thread process, or simply I give the system some freedom inside the loop, sometimes I get the timeout error. Note that the time cost of the Sleep() function is just of some millisecond, so there is no reason for the call to timeout.
    for (...) {
       DAQmxReadDigitalU32(...);
       Sleep(1); // To release system resources...
    }// That loop stops erratically
    With the last code, I can also see that actually the board did not complete the reading, i.e. is returning less samples than what requested. It seems to me that someting is stopping the acquisition, but I'm not able to figure what. The same happens even in LabView, using the VI read: sometimes the read randomly generates a timeout error and stops, even if LabView is the only running application.
    Could you point me what I'm doing wrong?
    Thanks, Piero.
    DAQmxReadDigitalU32

    Duplicate Post
    ColeR
    Field Engineer

  • I believe 3GS stereo jack damaged vehicle when used with cassette adapter

    I believe the 3GS damaged my automobile stereo through some sort of short in the headphone jack that is related to voice control. I've noticed other threads about headphones and voice control causing strange behaviors on the iPhone 3GS. I'm wondering if anyone else has noticed a problem with a cassette adapter as well. Here is my experience. First, I've owned both a 2G and a 3G prior to my 3GS. That is over 2 years experience with the iPhone platform prior to the issue. During that time I've used the same cassette adapter to output iTunes music to my car stereo system (same vehicle with original stereo during entire time as well…) - which is the OEM stereo that GM installs, as I own a 2K4 Chevy Silverado pickup truck. I’ve had zero issues with the 2G and 3G. Then, the first morning I go out to the truck with the 3GS and plug it in, there is a pop that comes out of the speakers, voice control comes on, then the phone locks up and has to be hard reset. I later notice that one of the backlight bulbs in the stereo is now blown (darkening half the display) and the vehicle speedometer is malfunctioning. The speedo issue might seem surprising, but given how GM wires everything back the stereo system for Onstar monitoring, while I don't know for sure, I assume the stereo received some sort of short from the 3GS and then passed that along to the electronics which control the instruments. The analog GM instrument cluster displays (such as speed) are all managed by electronics that drive small servos that actually move the needles.
    Anyhow, again, I feel it was the 3GS that caused this damage - as I'd been using other iPhone products for 2+ years and had no prior problems. Immediately after pluggin the 3GS in the first time the problem occurred. I think the voice control activating intermittently is a symptom of the problem. 8-9 times out of 10, when I plug the cassette adapter into the 3GS, voice control automatically starts, yet not all of the time. I am still using the same cassette adapter, but now plug it into the iPhone before it is inserted into stereo. While I’ve had no other issues with my stereo or instruments, I’m also very careful about not plugging the cassette adapter into the iPhone while the adapter is in the cassette player. I will also note that I'm on 2nd 3GS phone after an exchange for an unrelated issue and the new phone still intermittently activates voice control when the cassette adapter is plugged in. The issue also occurs with a 2nd cassette adapter (of different manufacture) which I use with a stereo in my home. In my mind, there something definitely wrong with the headphone jack on the 3GS and I believe it damaged my vehicle. Is anyone else seeing this issue?
    -Calvin

    You can try another cassette adapter or try another audio device with the same cassette adapter
    Have you considered getting  a BlackBerry VM-605?  Good prices on Amazon.
    Please click the Thumbs Up icon if this comment has helped you!
    If your issue is resolved, please click the solution button on the resolution!
    Every BlackBerry should have BlackBerry Protect, get it now! | Follow me on Twitter | Bring Back BBM Music!

  • IPad "misbehaves" when used with a MultiSIM - picks up/rejects phone calls

    Hello Cupertino,
    iPad 3G misbehaves when used e.g. with a MultiSIM card of T-Mobile Germany.
    It is said to pick up phone calls and actively rejects them. Thus the "Main" Mobile Phone (e.g. the iPhone) will ring ONCE then the iPad will pick up and reject the call. (On T-Mobile Network, one cannot disallow phone calls for a specific MultiSIM, all Phones of the same number will ring simultaneously).
    This is a really serious and embarassing bug, please fix it with next OS version.
    Cheers,
    Thomas

    No! this is a "bug", because the ipad is connecting via the sim-card in an incorrect way. From day one on Apple always is pressing the fact that the iPad is a "data"-device ONLY and NOT a "phone"-device.
    There is no intention of Apple to make the ipad do anything else as "connecting to the internet". That's why there is no textmessage app, no voicemail, no phonestuff on the the ipad. That's perfectly ok! But considering this the iPad misbehaves when connecting to a gprs/umts network because it is connecting showing itself as a PHONE AND DataDevice instead of DATA-ONLY.
    THAT IS WRONG, because this automatically triggers the telcom provider to sent voice and messaging connections to this device.
    Proof: On a jailbroken iPad it is possible to simply switch the device to the correct CGCLASS="CP" setting. With this setting the iPad only connects as a datadevice (e.g. like a usb-umts-stick) and doesn't answer any incoming non-data-connections.
    BUT: I don't want a jailbreak! If want a correct working iPad.
    And last but not least: the multi-sim-option (meaning more simcards under the same number) is totaly legitimate and is part of the usagespecs for telecom-networks. It is working perfectly well if (yeah, IF) every device connecting to the network identifies itself correctly with its capabilities.
    If a device identifies itself as a phone and it actually isn't, then this device has a serious bug and is not correctly configured to be used in a mixed telcom network. Just because "normaly" everybody uses only one simcard, it is no reason NOT to stick to specs.
    This is just sloppy work when porting the iPhone OS to the iPad. Simply forgotten! No big deal, but fix it, because Apple is "killing" a whole generation of german t-mobile users (all iphone contracts older than ~12 months can use their dataplan with free multisimcards from t-mobile on different devices!).
    Message was edited by: kinonet

  • WMI InvokeMethod for StartService does not work when used with ManagementOperationObserver on IIS

    Hi,
    I am using .net 4.5 and C# to remotely restart the services. I am using WMI to do this task.  This code works when called from windows application on the machine, but same code when used inside web application it gives exception of invalid parameter.
    We saw it log error in System Event log 
    The machine-default permission settings do not grant Local Activation permission for the COM Server application with CLSID
    {49BD2028-1523-11D1-AD79-00C04FD8FDFF}
     and APPID
    {49BD2028-1523-11D1-AD79-00C04FD8FDFF}
     to the user IIS APPPOOL\<app pool user> SID (S-1-5-82-634865507-143429395-1373251082-3464226226-3883016302) from address LocalHost (Using LRPC). This security
    permission can be modified using the Component Services administrative tool.
    I am pasting below code for reference.
    m.InvokeMethod(observer, "Startservice", null, null);
    //wait until invoke method is complete or 30 sec timeout
    int intCount = 0;
    while
    (!completionHandlerObj.IsComplete)
    if (intCount > 60)
    MessageBox.Show("Inside 30 seconds");
    break;
    //wait 1 sec.
    System.Threading.Thread.Sleep(1000);
    //increment counter
    intCount++;
    //see if call was successful.
    if (HandlerObj.ReturnObject.
    Properties["returnValue"].Value.ToString() == "0")
    isStarted = true;
    Dhanaji

    For web apps the context will be the context of the app pool.  Unless you set the app pool to run as a user account that has admin privileges on the remote machine then you won't be able to control the services on a remote machine.  If you have
    enabled Win auth such that the app runs in the context of the remote user then you might need to turn on delegation otherwise the hop won't work.
    Note that if you are using Win auth then consider using ServiceController instead which provides the same functionality as WMI but with a cleaner syntax.  This wouldn't work if you need to impersonate a user though.
    Michael Taylor
    http://blogs.msmvps.com/p3net

Maybe you are looking for