How to use 'this' with synchronization?

Hi, I have the following program and the results are not synchronized as expected.
The TransThread class includes a synchronized block and a synchronized method. In my opinion, a non-static synchronized method will refer to 'this' as its objectidentifier so I use 'this' as the objectidentifier of synchronized block,too. But no synchronization happens. Would you like tell me why? Thank you in advance.
// SynTestDemo.java
class SynTestDemo
public static void main (String [] args)
FinTrans ft = new FinTrans ();
TransThread tt1 = new TransThread (ft, "Deposit Thread");
TransThread tt2 = new TransThread (ft, "Withdrawal Thread");
tt1.start ();
tt2.start ();
class FinTrans
public static String transName;
public static double amount;
class TransThread extends Thread
private FinTrans ft;
TransThread (FinTrans ft, String name)
super (name); // Save thread's name
this.ft = ft; // Save reference to financial transaction object
public synchronized void WithdrawOp()
ft.transName = "Withdrawal";
try
Thread.sleep ((int) (Math.random () * 1000));
catch (InterruptedException e)
ft.amount = 250.0;
System.out.println (ft.transName + " " + ft.amount);
public void run ()
for (int i = 0; i < 100; i++)
if (getName ().equals ("Deposit Thread"))
synchronized (this)
ft.transName = "Deposit";
try
Thread.sleep ((int) (Math.random () * 1000));
catch (InterruptedException e)
ft.amount = 2000.0;
System.out.println (ft.transName + " " + ft.amount);
else
WithdrawOp();

Thank you for your advice. I modified my code. But the outcome is incorrect if I use
if (thdName == "Deposit Thread")
the amount is always 100
while
the outcome is correct if I use
if (getName ().equals ("Deposit Thread"))
// SynTestDemo.java
class SynTestDemo
public static void main (String [] args)
FinTrans ft = new FinTrans ();
TransThread tt1 = new TransThread (ft, "Deposit Thread");
TransThread tt2 = new TransThread (ft, "Withdrawal Thread");
tt1.start ();
tt2.start ();
class FinTrans
public static String transName;
public static double amount;
public synchronized void update(String transName, double amount)
this.transName = transName;
this.amount = amount;
try
Thread.sleep ((int) (Math.random () * 1000));
catch (InterruptedException e)
System.out.println (transName + " " + amount);
class TransThread extends Thread
private FinTrans ft;
TransThread (FinTrans ft, String name)
super (name); // Save thread's name
this.ft = ft; // Save reference to financial transaction object
String thdName = getName();
public void run ()
for (int i = 0; i < 40; i++)
if (thdName == "Deposit Thread")
// if (getName ().equals ("Deposit Thread"))
ft.update(thdName,1000);
else
ft.update(thdName,100);

Similar Messages

  • How to use annotation with GUI in swing

    hi,
    i am new to annotation. I know how to use this with classes, methods and java elements. I am developing a tool in applet to post comment on my notepad, but unable to find that how to use this with GUI of swing. I have gone through most of forums and tutorials but got no idea. Anyone could give me little idea.
    Thanks

    >
    i need to use the applet mousedown function in swing..>There is no such class as 'applet' in the JRE, Swing has an upper case 1st letter, functions in Java are generally referred to as methods, and there is no 'mousedown' method in the JRE.
    OTOH a Swing JApplet inherits the mouseDown method from Component, and it was deprecated in Java 1.1. The documentation (JavaDocs) provides instructions on what to use instead.
    >
    is there any way to use it or any other alternative is available for this?
    if can provide me a sample code.>If can provide me cash. Short of that, you might actually need to do some research/coding of your own.
    As an aside. What does any of this have to do with Java 2D?

  • I used to drag and drop newly loaded CD's from Recently Added to one of my iPod Classic playlists. I have no idea how to do this with the new version of iTunes. I thought perhaps checking Sync might give me a clue but it warned me Syncing would wipe conte

    I used to drag and drop music loaded from CD's from the Recently Added folder to a Playlist on my iPods Classic. Can't see how to do this with new iTunes version. Probably need to sync somehow but when I checked on Sync it warned me it would wipe out my iPod contents. Don 't know why Apple makes something that used to be simple so difficult. Can't even figure out how to display both my library and iPod contents on same screen.

    Hey joshuafromisr,
    If you resintall iTunes, it should fix the issue. The following document will go over how to remove iTunes fully and then reinstall. Depending on what version of Windows you're running you'll either follow the directions here:
    Removing and Reinstalling iTunes, QuickTime, and other software components for Windows XP
    http://support.apple.com/kb/HT1925
    or here:
    Removing and reinstalling iTunes, QuickTime, and other software components for Windows Vista or Windows 7
    http://support.apple.com/kb/HT1923
    Best,
    David

  • I'm trying to use a madcatz controller on the mac but it turns on for 2 seconds and turns off. the system profiler picks it up as a xbox 360 gamepad but says its not been configured. how can i allow my self use this with my games.

    i'm trying to use a madcatz controller on the mac but it turns on for 2 seconds and turns off. the system profiler picks it up as a xbox 360 gamepad but says its not been configured. how can i allow my self use this with my games!

    I have exactly the same problem.
    I'm a little peed-off with Microsoft on this. The original wireless controller I have doesn't work either since the cable I'm using is 'just' the charge and play cable, so a direct connection to my Mac won't work with this cable. It's a cable for god's sake. Why on EARTH put any limitations on this!?!?
    Next I learn I need an additional USB wireless receiver from Microsoft to get my original 360 controller to talk to my Mac. As I certainly don't want to fork any more microsoft dollars on this, this is not an option.
    Next I think, yes! I have a MadCatz 360 wired USB controller. This should surely do the trick just plugging it in and the way I go. But oh no, for some reason I plug my MadCatz Xbox360 controller in, it flashes a few times then switches off and can't be turned on again.
    The System Profiles recognizes it as a MadCatz 360 device controller but that is about it, but the ontroller software I download for the System Prefs (360Controller, USBOverdrive etc) don't even recognize the controller as being plugged in.
    What the **** Is The Microsoft Deal with all of this!?!?
    PS: Oh, and I forgot to mention, there are No drivers for device 4716 that I can find on the MadCatz website, or am I going blind?
    If anyone has a link, it would be appreciated.
    Message was edited by: nostrawaggus

  • HT201303 last time i bought apps by using visa gift card so iam trying to use the kind of visa gift card and the system is denying the method payment ....any idea how to solve this with my itune account ?

    last time i bought apps by using visa gift card so iam trying to use the kind of visa gift card and the system is denying the method payment ....any idea how to solve this with my itune account ?

    A few reasons I can think of, but instead of shooting in the dark, lets check with experts who can look at your account.
    iTunes Store Support
    http://www.apple.com/emea/support/itunes/contact.html

  • HT2361 I have a Sony DCR-HC20. Can I use this with iMovie? How do I down load my videos?

    I have a Sony DCR-HC20. Can I use this with iMovie? If not, How do I transfer to My computer, and what would I use?

    http://esupport.sony.com/US/p/support-info.pl?info_id=1192&mdl=DCRHC20

  • Javascript error on - Help - Learn how to use Numbers with this online...

    I am trying to find out more about Numbers. I am using iTunes 9.1.1 on a Vista PC. I get a Javascript error in Internet Explorer 8 when I try to access:
    Help - Learn how to use Numbers with this online resource.
    http://help.apple.com/numbers/1.0
    The link redirects to the following page:
    http://help.apple.com/iwork/safari/interface/#tan727163ed
    The above page is blank except for boxes for Keynote Help, Pages Help, Numbers help, and a blank input field that seems to do nothing. Here is the Javascript error message from Internet Explorer 8:
    Webpage error details
    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
    Timestamp: Tue, 1 Jun 2010 20:55:54 UTC
    Message: This command is not supported.
    Line: 1
    Char: 41589
    Code: 0
    URI: http://help.apple.com/iwork/safari/interface/javascript.js

    Link also does not work for me in WIN 7 with IE8. However, it does work in Chrome in WIN 7. It also works in Safari on my G4. Suggest you try Chrome or it may work in another browser like FireFox or Safari in Vista.

  • How to use Terminal with KeySpan USB serial adaptor cable?

    I own a KeySpan model USA-19HS to use with our MacBookPro when working on clients networking equipment that require serial communication. I have used ZTerm with excellent luck, but it costs, and is yet another application that I dont really want. I would like to know if any one out there has exacing instructions on how to use Terminal with this adaptor. If I could write a file that had, say, the couple most common settings (9600-8-n-1, 57600-8-n-1, et cetera) that I could simply click on, to open a new Terminal window with those settings, then simply close that, and open a new window (apple+n) in order to get back to a default settings terminal window. All feedback is most appreciated.
    Thanks so much, Jason Sjobeck

    Hi, Jason. Welcome to the Discussions.
    You wrote: "All feedback is most appreciated."OK.
    1. Have you considered discussing this with Keyspan Support?
    2. Have you read the section "OSX has Terminal.app - why do I need ZTerm?" on the ZTerm Home Page? That seems to explain why one needs an app like ZTerm.
    3. A quick search of Keyspan's FAQ for your product and terminal doesn't yield much more than how to find the serial ports using Terminal.
    Likewise, this Google search also yields little, and variants of that search of such weren't particularly help either.
    It appears your inquiry seems to be a somewhat esoteric topic.
    4. Apparently, ZTerm X is a bit long in the tooth and there's no Universal Binary for it. It sounds, however, like you've tried it running on your MBP under Rosetta, yes?
    5. In researching alternatives to ZTerm for you, I note the followng:• The comment from "Reverb" on this page suggests C-Kermit or Kermit as a free alternative.
    • This tip on Mac OS X Hints cites Minicom as another option.6. You may want to try your question on the Unix Discussion.
    Good luck!
    Dr. Smoke
    Author: Troubleshooting Mac® OS X

  • How to use variables with

    I need to get the number of lines in internal table, which would
    be easy, using DESCRIBE TABLE itab LINES lines. But the problem
    is, that I get the name of this internal table in the field of
    another internal table, so I have to use the name of internal
    table as variable, but I don't know how to use variables with
    DESCRIBE TABLE (or if this is possible).

    Hi,
    REPORT ZPRUEBA782 .
    define two tables with diferent structures.
    data: begin of table1 occurs 0,
    registro type i,
    end of table1.
    data: begin of table2 occurs 0,
    registro type i,
    repid like sy-repid,
    end of table2.
    data: rows type i.
    start-of-selection.
    fill them with data
    do 1000 times.
    table1-registro = sy-tabix.
    append table1.
    enddo.
    do 1757 times.
    table2-registro = sy-tabix.
    table2-repid = sy-repid.
    append table2.
    enddo.
    call a form that receives as input the table and returns the number
    of rows as output.
    perform howmanyrows tables table2 changing rows.
    break-point. "evaluate the number of rows
    perform howmanyrows tables table1 changing rows.
    break-point. "evaluate the number of rows
    end-of-selection.
    form howmanyrows tables itable changing rows.
    rows = 0.
    loop at itable.
    add 1 to rows.
    endloop.
    endform.
    You can try it if you don't find a better solution.
    Cheers,
    Chaitanya.

  • How to Use Gmail With Apple's Mail?

    Answer:
    Now Itis not a Problem, Please go to this link and know how to solve this problem, Ihave configuir this in my mac pro. Have enjoy.
    http://picturemuseum.blogspot.com/2011/08/how-to-use-gmail-with-apple-mail.html
    http://mobilesuggestion.blogspot.com/2011/08/how-to-use-gmail-with-apples-mail.h tml

    I notice that it's possible to have sticky posts in these forums. See
    http://forum.java.sun.com/forum.jspa?forumID=534
    the Concurrency forum for an example. Posting a link to the FAQ in a sticky post might reduce the number of FAQs asked here by maybe 1%? Or posts like this one might do well as a sticky?

  • How to use this function in crm5.2 -   /CEM/ENT_IMPORT_DATAFROMEXCEL

    i am having a problem regarding how to use this function to import values from an excel sheet.
    in call funtion what to specify at place of ct_ent_action = ?
    CALL FUNCTION '/CEM/ENT_IMPORT_DATAFROMEXCEL'
      CHANGING
        CT_ENT_ACTION       =
    EXCEPTIONS
      FILE_ERROR          = 1
      IMPORT_ERROR        = 2
      OTHERS              = 3
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    Hi
    The use of Standard Parner functions are as follows:
    1. Ordering Address (OA): The PO will be sent this vendor and not the main vendor.
    2. Goods Supplier (WL): In case of return deliveries, Goods will be returned to thsi vendor's address
    3. Invoicing party (RS): The payment will be made to this vendor.
    Hope this clarifies.
    Thansk

  • How  to  use this HD

    Emicsoft HD Video Converter is the high definition video converter software develeped to convert HD video files with high quality. With this HD video converter you can also easily convert between HD video formats, convert standard definition videos to HD videos, or convert HD videos to SD (Standard-Definition) videos. The HD video converter supports most of HD video formats: MTS/M2TS, TS, HD MKV, HD AVI, HD MP4, HD WMV, HD MOV, HD ASF.
    How to use this HD Converter?
    1. Launch the software and click "Add File" button to import files into the software. You can watch them on the preview screen.
    2. Edit the added movie by clicking "Effect" "Trim" or "Crop" button which can help you create unique videos easily.
    3. Select output in Profile option. Here all popular formats and mobile devices are supported. Then make sure the destination folder.
    4. Click "Start" button and the program will automatically start conversion.
    Emicsoft AVCHD Converter can convert AVCHD video to all popular video formats, and convert other videos to AVCHD video. So many formats are supported such as HD video (TS, MTS, M2TS, HD MPEG-4, HD MOV, HD AVI, HD VOB, HD ASF, HD WMV, etc.), general video (AVI, MP4, WMV, MPEG-1, MPEG-2, VOB, MOV, 3GP, FLV, SWF, etc.) and audio track (MP3, WMA, WAV, AAC, AC3, OGG, etc.).
    Emicsoft MOD Converter is a powerful MOD video converter which can MOD to AVI, MOD to MPG, MOD to MPEG, MOD to WMV and to other video format, such as FLV, SWF, ASF, DivX, XviD, MP4, VCD, SVCD, DVD-Video, VOB and so on. Batch conversion supported, so you can add several .MOD files one time.
    More softwares may help you:
    Emicsoft Video Converter
    Emicsoft FLV Converter
    Emicsoft MTS Converter

    This is not a simple issue. You can set up a network, and set up sharing on the Mini, so that other computers on the same network can read and write to the external hard drive connected to the Mini.
    To do that you have to get both computers on the network first, then set up the external hard drive so others can read on and write to it.
    Is that what you want to do? If yes, and yu need help with this, please post back.
    If you want to store apps on the external, and run them from a networked Mac, that is a whole other issue. To do that you need to set up the Mini to be a network server:
    https://www.apple.com/ca/mac-mini/server/
    Please post back, because I'm not sure I'm answering your questions.

  • How to use Count with Date Parameters

    Hello,
    I am having issues using the Count() function in conjunction with date parameters.
    This is a Siebel report and in my report I have 2 date parameters(From Date, To Date). In a nutshell I am basically trying to count Opportunities that has a start date within the given date period. However I don't see a reasonable way to put my date parameters within the Count() function. The reason being is that I need to have a huge chunk of code to convert the dates into a common format that can be compared, and it won't even fit within the code block in my rtf template. I am not even sure how to put multiple conditional statements inside a Count() function since all the examples I have seen are very simple.
    Anyone have a suggestion on how to use Count() with date parameters?
    Thanks.

    Any chance you can get the date formats in the correct format from siebel?
    I don't know Siebel - so I can't help you with that. If you get the correct format it is just
    <?count(row[(FromDate>=date) and  (date<=ToDate))?>
    Otherwise the approach would probably need to use string function to get year/monthd/day from the date
    and store it into a varialbe and compare later the same way
    <?variable@incontext:from; ....?>
    <?variable@incontext:to; ...?>
    <?count(row[($from>=date) and  (date<=$to))?>
    Potentially you can use the date functions such as xdofx:to_date to do the conversion
    [http://download.oracle.com/docs/cd/E12844_01/doc/bip.1013/e12187/T421739T481158.htm]
    But I am not sure if they are available in your siebel implementation.
    Hope that helps

  • Help me!! How to use JavaScript with JSP ??

    I am using JDeveloper and I created a screen in JSP which uses a bean for database connectivity and retriving info onto the page.
    The page has a ListBox where list items are populated from the database.My requirement is
    whenever the list is changed the page shuold be refreshed with the selected item info.
    I tried to use 'JavaScript' for triggering the event with 'onChange' event of the ListBox.But the event is not getting invoked. I think JavaScript is not working with JSP.
    Please help me with how to Use javaScript with JSP or any other alternative where I can meet my requirement.
    I have one more question...I have gone through the JSP samples in OTN and I am trying do download the sample 'Travel servlet' which show list of countries...etc
    I have also gone through the 'readme' but I don't know how to extract .jar file.
    I would be great if you could help me in this.
    Thanks!!
    Geeta
    null

    We have a similar need. We have used Cold Fusion to display data from Our Oracle Database. We have a simple SElect Box in HTML populated with the oracle data. When someone selects say the State of Pennsylvania. then we have an On change event that runs a Javascript to go get all the cities in Pennsylvania.
    Proble we are having is that inorder for the Javascript to work , we currently have to send all the valid data.
    Do you know of any way to dynamically query the the Oracle database in Javascript

  • How to use setSoTimeOut() with socket ??

    HI All!
    I eagerly need to know how to use setSoTimeOut() with socket (on server side).
    Actually the scenario is that My Server accepts connection and waits for connected client to send data to it. I WANT TO KEEP, IF CLIENT DOENS'T SENDS ANY DATA TO SERVER WITHIN 5 min. THEN SERVER MUST CLOSE THE CONNECTION, BUT IF SERVER RECEIVES STILL A BYTE FROM THE CLIENT WITHIN 5 mins., THE SERVER MUST RESET CHECKING TIME OUT FOR NEXT 5 MINUTES.
    How can i do that??
    Any help with example will be appreciated!!
    Thanx in advance

    you'd have to do something like this:
    (this is all assuming you want to read Strings from the socket. setSoTimeout works for any read() call to the InputStream associated with the Socket)
    ServerSocket ss = new ServerSocket(myPort);
    Socket s = ss.accept();
    s.setSoTimeout(5*60*1000); // 5 mins * 60 secs * 1000ms
    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    boolean running = true;
    while(running)
    try
    String readIn = in.readLine();
    // do whatever you want with the data read
    catch(SocketTimeoutException)
    running = false;
    Now. That's what I think needs to be done. I don't have Java on the machine I'm currently on, so I can't test/verify it, but it goes something like that.
    Good luck,
    Radish21

Maybe you are looking for