Threads behaving wierdly..Work of 5 months going for a toss.. plz help...?

Hi everyone,
I am stuck into this probel very badly..i have never seen such a problem before..Thread bottleneck somewhere is the issue i guess,,, plz have a look at the code patiently.. and do reply.. u guys are my last hope on this thing..Thank YOU...
The Bank method is called by another method which runs 50 threads , so
at a time 50 threads could enter this method to record Debit... class BankHelper {
// Some code of bank.. not relevant to my problem
     public Bank() {
                long time = System.nanoTime();
                abc.recordDebit();_ *// want to measure how much time this method takes*_
                abc.recordTimeForDebit(System.nanoTime() - time); *_// accumulating the total time the above method takes to calculate_*
    class abc {
// Some code of bank.. not relevant to my problem
    public synchronized void recordDebit(){
        debit++;      
    } _*// I had put timers aound the ++ and it took not more than 700 to 900 nanoseconds*_
    public synchronized void recordTimeForDebit(long time){
        record += time;
        Log.debug("Time taken to record drop for DEBIT " + record + " Millis " + time);
Answer:_
Record time is 9014 millis for 5000 increments or 5000 calls to recordDebit()_
One can see in the answer that its a huge number when one is expecting it to be somewhere around:
EXPECTED ANSWER:
*5000 * 800 nanos( it takes 800 nanos for very increment) = 4000000 = 4 millis and its like 9014 millis*
How is there such a huge difference ????????? I have bruised thorugh it for like 3 days... i have lost all my hope.. Plz help..Also look at the next code..
When i go to see in the log .. it shows me that every record took like from 2000 nanos to 6 to 7 millis i.e.7000000...
HOw is this even possibly possible ??? where did it get all this extra time from...it should not be more than some 700 to 900 nanos ..
Is there a bottleneck somewhere ????
Now part 2:
This thing has fazzed, dazzled , destroyed me.. I could not understand this and it has tossed all my concepts into the cupboard..
Same stuff: JUST HAVE A LOOK AND ENJOY AND TELL ME WHATS GOING ON...Same code.. different way of incrementing data i.e synchronization is at a different place..but results are very highly improved...
    class BankHelper {
// Some code of bank.. not relevant to my problem
     public Bank() {
                long time = System.nanoTime();
                abc.recordDebit(); // want to measure how much time this method takes
                abc.recordTimeForDebit(System.nanoTime() - time); // accumulating the total time the above method takes to calculate
}The Bank method is called by another method which runs 50 threads , so at a time 50 threads could enter this method to record Debit...
    class abc {
// Some code of bank.. not relevant to my problem
    public void recordDebit(){
        someotherclass.increment();      
    } // this is not synchronized nowwwwwwww
// I have put timers here too and it took like 1500 to 2500 nanos
    public synchronized void recordTimeForDebit(long time){
        record += time;
        Log.debug("Time taken to record drop for DEBIT " + record + " Millis " + time);
class someotherclass{
// Not relevant code
    public void increment(){
        someotherclass1.increment1();      
class someotherclass1{
// Not relevant code
    public void increment1(){
        someotherclass2.increment2();      
class someotherclass2{
// Not relevant code
    public synchronized void increment2(){
        someotherclass3.increment3();      
    } //now its synchronized
class someotherclass3{
// Not relevat code
    public synchronized void increment3(){
        debit++;      
    } //now its synchronized
ANSWER: Record is 135 millis for 5000 increments or 5000 calls to recordDebit()_
Expected time was : 5000 2500 = 125000000 = 125 millis (WOW .. AS EXPECTED)*
Please don't ask me why this code has been written this way..i know it goes and increment and does the same thing...but somehow when i measured it..
overall time or the accumulated time for both codes varied like in huge numbers...even though latter code is actually a superset of previous code..
HOW IS THERE SUCH A HUGE DIFFERENCE BETWEEN THE NUMBERS ???
COULD BE BECAUSE OF THE POINT OF SYNCHRONIZATION ???
Thank you..for going through all this..

Answered on JavaRanch. By synchronizing both the increment and the recording methods, you can only perform one of those functions at a time.Thanking you Steve, I shall read and inwardly digest.
My (humble) advise is:
1. WTF are you doing trying to measure the time taken to execute a [unirary operation|http://en.wikipedia.org/wiki/Unary_operation] (you putz!) which is just like so freeken small (on a 2Ghz CPU) that even a gazzillion invocations might take less time than it took you get your duds off in order to earn your reputation as a putz!
2. FFS, Don't write profiling code in your app... just download one. VisualVM is way cool!
3. As you would know, if you had bothered to actually RTFM at any stage in your (bugging;-) career... synchronized methods are slow(er) because of the farckin time taken to acquire and release the lock... as well as the inherent inefficiency of bottlenecking a whole method, as apposed to just the critical statements... which in the case of a simple incrementor is just about no near new never mind... so no never mind... but still the fact remains... you've turned an atomic operation (google it yourself) into a bottleneck... and then you're standing there whining about bottlenecks. Doh!
4. Seriously dude, read the effin manual.
Edited by: corlettk on 27/09/2008 14:19

Similar Messages

  • My computer iTools not working.how to this fix ?!!.plz help me

    My computer iTools not working.how to this fix ?!!.plz help me

    Hi We don't have a crytral ball more infomation about your problem if you need help . Cheers Brian

  • Hello my name is achuthranesh and my iPhone has in problem. When my iPhone was in vertical position the upper touch screen (up to 4mm) is not working....... Plz help me.....

    Hello my name is achuthranesh and my iPhone has in problem. When my iPhone was in vertical position the upper touch screen (up to 4mm) is not working....... Plz help me.....

    Are you talking about status bar or 4 mm ( screen) below it

  • Thread behaves wierdly.. takes more time for less code...

    Hi everyone,
    I am stuck into this probel very badly..i have never seen such a problem before..Thread bottleneck somewhere is the issue i guess,,, plz have a look at the code patiently.. and do reply.. u guys are my last hope on this thing..Thank YOU...
    The Bank method is called by another method which runs 50 threads , so at a time 50 threads could enter this method to record Debit...
    class BankHelper {
    // Some code of bank.. not relevant to my problem public Bank() {
    long time = System.nanoTime();
    abc.recordDebit(); // want to measure how much time this method takes
    abc.recordTimeForDebit(System.nanoTime() - time); // accumulating the total time the above method takes to calculate
    class abc {
    // Some code of bank.. not relevant to my problem
    public synchronized void recordDebit(){
    debit++;
    } // I had put timers aound the ++ and it took not more than 700 to 900 nanoseconds
    public synchronized void recordTimeForDebit(long time){
    record += time;
    Log.debug("Time taken to record drop for DEBIT " + record + " Millis " + time);
    }Answer:
    Record time is 9014 millis for 5000 increments or 5000 calls to recordDebit()
    One can see in the answer that its a huge number when one is expecting it to be somewhere around:
    EXPECTED ANSWER:
    5000 * 800 nanos( it takes 800 nanos for very increment) = 4000000 = 4 millis and its like 9014 millis
    How is there such a huge difference ????????? I have bruised thorugh it for like 3 days... i have lost all my hope.. Plz help..Also look at the next code..
    When i go to see in the log .. it shows me that every record took like from 2000 nanos to 6 to 7 millis i.e.7000000...
    HOw is this even possibly possible ??? where did it get all this extra time from...it should not be more than some 700 to 900 nanos ..
    Is there a bottleneck somewhere ????
    Now part 2:
    This thing has fazzed, dazzled , destroyed me.. I could not understand this and it has tossed all my concepts into the cupboard..
    Same stuff: JUST HAVE A LOOK AND ENJOY AND TELL ME WHATS GOING ON...Same code.. different way of incrementing data i.e synchronization is at a different place..but results are very highly improved...
    class BankHelper {
    // Some code of bank.. not relevant to my problem
    public Bank() {
    long time = System.nanoTime();
    abc.recordDebit(); // want to measure how much time this method takes
    abc.recordTimeForDebit(System.nanoTime() - time); // accumulating the total time the above method takes to calculate
    }The Bank method is called by another method which runs 50 threads , so at a time 50 threads could enter this method to record Debit...
    class abc {
    // Some code of bank.. not relevant to my problem
    public void recordDebit(){
    someotherclass.increment();
    } // this is not synchronized nowwwwwwww
    // I have put timers here too and it took like 1500 to 2500 nanos
    public synchronized void recordTimeForDebit(long time){
    record += time;
    Log.debug("Time taken to record drop for DEBIT " + record + " Millis " + time);
    class someotherclass{
    // Not relevant code
    public void increment(){
    someotherclass1.increment1();
    class someotherclass1{
    // Not relevant code
    public void increment1(){
    someotherclass2.increment2();
    class someotherclass2{
    // Not relevant code
    public synchronized void increment2(){
    someotherclass3.increment3();
    } //now its synchronized
    class someotherclass3{
    // Not relevat code
    public synchronized void increment3(){
    debit++;
    } //now its synchronized
    }ANSWER: Record is 135 millis for 5000 increments or 5000 calls to recordDebit()
    Expected time was : 5000 * 2500 = 125000000 = 125 millis (WOW .. AS EXPECTED)
    Please don't ask me why this code has been written this way..i know it goes and increment and does the same thing...but somehow when i measured it..
    overall time or the accumulated time for both codes varied like in huge numbers...even though latter code is actually a superset of previous code..
    HOW IS THERE SUCH A HUGE DIFFERENCE BETWEEN THE NUMBERS ???
    COULD BE BECAUSE OF THE POINT OF SYNCHRONIZATION ???
    Thank you..for going through all this..

    Triple post - http://forums.sun.com/thread.jspa?threadID=5334258&messageID=10438241#10438241

  • Please help , I have iPhone 4S and have updated to iso6, don't know if that has anything to do with the problem but I can't set the year on date and time to the correct year, it automatically goes to 2578 ? Plz help

    Subject:
    Please help , I have iPhone 4S and have updated to iso6, don't know if that has anything to do with the problem but I can't set the year on date and time to the correct year, it automatically goes to 2578 ? Plz help

    Settings>General>International.
    Set the calendar to "Gregorian". You have it set to Buddhist.

  • I'm unable connect my I pad mini to my laptop(window 7,64 bit) after formatting,my iTunes didn't find my device,before format everything working properly but this time lot of problem plz help

    I'm unable connect my I pad mini to my laptop(window 7,64 bit) after formatting,my iTunes didn't find my device,before format everything working properly but this time lot of problem plz help

    You formatted your computer's hard drive? If so, it's like starting with a new computer unless you restored all of your content from a backup but it does;t sound like that's the case.
    See wjosten's excellent how-to here: Syncing to a "New" Computer or replacing a "crashed" Hard Drive

  • HT5012 Ipad 3 speaker sound in not working.. Headphone is working ..no sound in any application..plz help

    Needs assistance guyz.. I tried so many tricks available in online but nothing works plz help me..

    Well, nobody here reads minds and that means that we don't know what you have already tried. Here is my boilerplate response, and unless you can share more information .......
    1. If you have no sound at all, even in the Music App, try rebooting the iPad.
    Reboot the iPad by holding down on the sleep and home buttons at the same time for about 10-15 seconds until the Apple Logo appears - ignore the red slider if it appears on the screen - let go of the buttons. Let the iPad start up.
    2. If you lost sounds for keyboard clicks, games or other apps, email notifications and other notifications, system sounds may have been muted.
    System sounds can be muted and controlled two different ways. The screen lock rotation can be controlled in the same manner as well.
    Settings>General>Use Side Switch to: Mute System sounds. If this option is selected, the switch on the side of the iPad above the volume rocker will mute system sounds.
    If you choose Lock Screen Rotation, then the switch locks the screen. If the screen is locked, you will see a lock icon in the upper right corner next to the battery indicator gauge.
    If you have the side switch set to lock screen rotation then the system sound control is in the control center if you are running iOS 7. Swipe up from the bottom of the screen to get to control center . Tap on the bell icon and system sounds will return.
    If you are running iOS 5 or iOS 6, the system sound control is in the task bar at the bottom. Double tap the home button and swipe all the way to the right in the task bar to get to the speaker icon on the left side. Tap on that and the sounds will return.
    If you have the side switch set to mute system sounds, then the screen lock can be accessed in the same manner as described above.
    This support article from Apple explains how the side switch works.
    http://support.apple.com/kb/HT4085
    3. Try resetting all settings. Settings>General>Reset>Reset all settings. No data will be lost but you will have to enter all of the device settings again.
    4. If you still have no sound, try cleaning the dock connector opening of the iPad with a soft brush and just a little bit of rubbing alcohol.

  • I have iPhone 4. and my left speaker is not working. Now what i do.. Plz help

    I have iPhone 4. and my left speaker is not working. Now what i do.. Plz help

    The speaker is only on the right. The holes on the left are for the mic.

  • Reverse SSH not working for Cisco 3750.Plz help.

    Hi,
    I have configured the reverse ssh mentioned on the cisco document:http://www.cisco.com/en/US/docs/ios/12_3t/12_3t11/feature/guide/gt_rssh.html#wp1051457.
    But now I am running into the issue that When I  issue command ssh -l alex:rotary1 192.38.6.8 from the cli of the router  I’m presented with a password prompt.
    I  enter my password and get connected. However at this point when I type, for  example, the atdt command I don’t see any output  displayed.
    So I  tried typing command atdt <tel no.> and at the same time ran a debug modem  on a separate session. It was from the separate session I could see the call  been made to the remote device and connected successfully. However almost  immediately the modem debug displays message 0236596: Dec  7 2010  12:18:18.236 GMT: TTY69: cleanup pending. Delaying DTR. At no point do I see  output displayed on the session where I connected to the  modem.
    When using telnet to  connect, i.e using the same modem and the same remote device, we have had no  problems it is just with SSH.
    Any suggestion on this will be highly appriciated.
    Thanks
    Alex.

    Philip,
    I first thank you for coming forward to help me on this.
    I use the 30DM(Digital Modem) Card on the router. I have configured on of this modem  on the PRI card(1/18) to make outbound calls. We use this dailup to cyclades the devices located on our remote office. Hence we loose the main internet connectivity we use this option as a backup.
    Show ver:Cisco IOS Software, 3700 Software (C3725-ADVSECURITYK9-M), Version 12.3(11)T5
    ine con 0
    transport output none
    line 72 92
    modem InOut
    modem autoconfigure type mica
    transport input all
    transport output all
    autoselect during-login
    autoselect ppp
    line 93
    absolute-timeout 60
    modem InOut
    modem autoconfigure type mica
    rotary 1
    no exec
    transport input ssh
    transport output none
    line aux 0
    no exec
    transport output none
    line vty 0 4
    logging synchronous
    transport input ssh
    transport output ssh
    Please let me know is this a bug or anything else. AS the same setup works fine when I use TElnet rather than ssh.
    Thanks in advance.
    REgards
    Alex.

  • Target="_blank" not working........plz help me..

    My requirement is to open a new (child) window when the user pushes
    a button on a (parent) window. I need to create a new instance
    of a bean, populate it, and then pass it to the child window for
    display.
    i tried this way
    To open JSP in new window just use target directive e.g.:
    <form action=myjsp.jsp method=post target=_blank>
    its not working...
    One additional question if I may:
    I don't want to open a new window for every action a user could
    take on the screen. Sometimes, I just want to post to the
    servlet and forward the jsp to the one window that is open.
    If I put the TARGET directive in, wouldn't I always open a new
    window?
    Is this still the best way to do this given this additional
    requirement? If yes, how do I alter it so that I don't always
    go to a new window?
    Thanks!!!!!!

    Thanks for the response...
    can i use some thing like this.....
    i mean document.form.action in window.open
    something like below...
    sb.append("input type=\"submit\"
    =\"submit\" Name="Submit"
    onClick="window.open(document.MyForm.action='/phmuni/fo
    m/combineDockets.jsp' , "myWindow");" value="Print " >
    ");YOu don't have to use action if you just want to go to another page. If you do need to do something before you open the new window, you may create a function (Javascript). Do some things (like the action) and then open a new window within that function. Then within your onClick tag, just call that function.

  • Error mapping doesn't work for me. Plz Help!

    I have created a .war using deploytool and added "404, /WEB-INF/NoSuchPage.html" in the error mapping area in file refs on the right tabbed panes.
    When I deploy the war and test it, the error mapping doesn't work. The server still display the default 404 page to me.
    What is the problem?
    I do have checked the descriptor. It contains:
    <error-page>
    ������<error-code>404</error-code>
    ������<location>/WEB-INF/NoSuchPage.html</location>
    </error-page>
    Any help will be greatly appreciated!

    <?xml version="1.0" encoding="UTF-8" ?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>WebApp</display-name>
    - <welcome-file-list>
    <welcome-file>login.html</welcome-file>
    </welcome-file-list>
    - <error-page>
    <error-code>404</error-code>
    <location>/Error.html</location>
    </error-page>
    <jsp-config />
    </web-app>
    This is a very simple war with no component and an error page is defined. But this doesn't work. When I type something like /loginnnn.htmllll, it doesn't give me the error page. That is my problem. Any idea???

  • I jst bought the imac 21.5 and i used kodak 5250 printer on my old pc  but the imac lion os wont work on my printer its shows not supported plz help me

    my question is how do my kodak 5250 printer work with my imac 21.5 os lion

    What steps have you take to install the printer and how are you connecting it? Please answer both questions!

  • My recirculating pump in sub vi simulation link doesnt work in the second iteration .It opens for maybe half a second whereas i gave the time delay for 5 secs..plz help very urgent

    Hi,
         I have attached my simulation loop.In the model attached i hav eone main pump with constant rpm which drives the 5 smaller pumps and fills the tank at the same time.As soon as the tanks reach their 90% level,the valves of the five pumps close(SP1,SP2,SP3,Sp4,Sp5).After that the recirculating pumps opens for 5 secs of the first tank.As soon as the recirculation finishes,the drain valve(SV1) for tank 1 open and the volume goes to interim storage.This happens for all the remaining tanks.
    My simulation works the first time,but when the second time the loop starts,it skips the recirculation pump even though i gave a time delay for 5 secs.Plz help ..I have attached the simulation.
    Thanks,
    Rami
    Attachments:
    Spatial Logic_2_Final.vi ‏223 KB

    Rami,
    I suspect that you have a race condition. The widespread use of local variables frequently leads to race conditions. Your subVI (Spatial Logic Sub_2.vi was not included) so I cannot run the VI. You have no way of knowing whether the subVI or the inner case structure will execute first, because there is no data dependency between them.
    I think a shift register or a few and some dataflow thinking would allow you to eliminate the inner case structure, the local variables, and, probably, most of your problems.
    Some of the SPi are indicators and some are controls. How are they used?
    The last case of the inner loop retursn to Case 1. Would case 0 be better?
    As for the second time through issue, it may be related to the Elapsed time function Auto Reset. From the help file: "Resets the start time to the value in Present (s) when the Express VI reaches the Time Target (s)." If more than 5 seconds elapses between the first time you use this and the next, it will exit immediately on the subsequent calls.
    Lynn

  • Continuous scrolling not working in Calendar month view

    Scrolling does not work in Calendar month view. I have toggled System Preferences >General > Show scroll bars to "Always on." Still nothing in Calendar month view.
    BUT, wait! Now every other app (except Calendar month view) now has the scroll bar "Always on" even though I toggled back to "Automatically based on...", then restarted my computer. I don't want to see the scroll bars all the time but now I can't hide them anywhere. It seems "Always on" amd "Automatically based on..." now do the same thing. Serious bug.
    Any hints? Thanks

    This is odd,  but the calendar scrolling in Month view is working on my machine.   It's been through various reboots - and I checked the disc (SSD) and permissions.
    Seems to me that the calendar application is sensitive to the scrolling. 
    Also,  I've been suffering from Firefox vertical scrolling stopping.  The "fix" is to quit & restart Firefox.  However... this scroll-crashing behaviour goes away for a few days then comes back with a vengence.  Again,  I've a feeling that a full reboot seems to help things.
    Yet another mouse-related problem I've been having is with VMWare.  These frequently interpret a single-click as a double-cliick or even triple-click.  Some chatter in the VMWare forums about this too.
    All of this behaviour *never* ocurred prior to the Mavericks upgrade.

  • Calling a delegate on the UI thread from a work thread inside a child class.

    Hi All,
    I've run into a snag developing a WPF multithreaded app where I need to call a method on the UI thread from my work thread, where the work thread is running a different class.
    Currently I am trying to use a delegate and an event in the 2nd class to call a method in the 1st class on the UI thread. This so far is not working as because the 2nd class is running in its own thread, it causes a runtime error when attempting to call
    the event.
    I've seen lots of solutions referring to using the dispatcher to solve this by invoking the code, however my work thread is running a different class than my UI thread, so it seems the dispatcher is not available?
    Below is as simplified an example as I can make of what I am trying to achieve. Currently the below code results in a "The calling thread cannot access this object because a different thread owns it." exception at runtime.
    The XAML side of this just produces a button connected to startThread2_Click() and a label which is then intended to be updated by the 2nd thread calling the updateLabelThreaded() function in the first thread when the button is clicked.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Threading;
    using System.Threading;
    namespace multithreadtest
    public delegate void runInParent();
    public partial class MainWindow : Window
    public MainWindow()
    InitializeComponent();
    threadUpdateLabel.Content = "Thread 1";
    private void startThread2_Click(object sender, RoutedEventArgs e)
    thread2Class _Thread2 = new thread2Class();
    _Thread2.runInParentEvent += new runInParent(updateLabelThreaded);
    Thread thread = new Thread(new ThreadStart(_Thread2.threadedTestFunction));
    thread.Start();
    public void updateLabelThreaded()
    threadUpdateLabel.Content = "Thread 2 called me!";
    public class thread2Class
    public event runInParent runInParentEvent;
    public void threadedTestFunction()
    if (runInParentEvent != null)
    runInParentEvent();
    I'm unfortunately not very experienced with c# so I may well be going the complete wrong way about what I'm trying to do. In the larger application I am writing, fundamentally I just need to be able to call a codeblock in the UI thread when I'm in a different
    class on another thread (I am updating many different items on the UI thread when the work thread has performed certain steps, so ideally I want to keep as much UI code as possible out of the work thread. The work threads logic is also rather complicated as
    I am working with both a webAPI and a MySQL server, so keeping it all in its own class would be ideal)
    If a more thorough explanation of what I am trying to achieve would help please let me know.
    Any help with either solving the above problem, or suggestions for alternative ways I could get the class in the UI thread to do something when prompted by the 2nd class in the 2nd thread would be appreciated.
    Thanks :)

    If I follow the explanation, I think you can use MVVM Light messenger.
    You can install it using NuGet.
    Search on mvvm light libraries only.
    You right click solution in solution explorer and choose manage nugget...
    So long as you're not accessing ui stuff on these other threads.
    using GalaSoft.MvvmLight.Messaging;
    namespace wpf_Tester
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    public MainWindow()
    InitializeComponent();
    Messenger.Default.Register<String>(this, (action) => ReceiveString(action));
    private void ReceiveString(string msg)
    MessageBox.Show(msg);
    Dispatcher.BeginInvoke((Action)delegate()
    tb.Text = msg;
    private void Button_Click(object sender, RoutedEventArgs e)
    Task.Factory.StartNew(() => {
    Messenger.Default.Send<String>("Hello World");
    What the above does is start up a new thread - that startnew does that.
    It sends of message of type string which the main window has subscribed to....it gets that and puts up a message box.
    The message is sent from the window to the window in that but this will work across any classes in a solution.
    Note that the receive acts on whichever thread the message is sent from.
    I would usually be altering properties of a viewmodel with such code which have no thread affinity.
    If you're then going to directly access properties of ui elements then you need to use Dispatcher.BeginInvoke to get back to the UI thread.
    I do that with an anonymous action in that bit of code but you can call a method or whatever instead if your logic is more complicated or you need it to be re-usable.
    http://social.technet.microsoft.com/wiki/contents/articles/26070.aspx
    Hope that helps.
    Technet articles: Uneventful MVVM;
    All my Technet Articles

Maybe you are looking for