473,804 Members | 3,383 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Whats wrong with this loop code?

9 New Member
Sometimes it works, sometimes it doesn't but I can't find the bug:

Expand|Select|Wrap|Line Numbers
  1. package Loop.Test;
  2.  
  3. import java.util.concurrent.ExecutorService;
  4. import java.util.concurrent.Executors;
  5.  
  6. public class TheLoopTest {
  7.  
  8.         private final ExecutorService executorService = Executors.newFixedThreadPool(100);
  9.         private static final int MAX = 1000;
  10.         private int zeroToTop = 0;
  11.  
  12.         TheLoopTest() throws InterruptedException {
  13.                 for (int i = 0; i < MAX; i++) {
  14.                         executorService.execute(new Runnable() {
  15.                                 public void run() {
  16.                                         incrementAllTheWay();
  17.                                 }
  18.                         });
  19.                 }
  20.                 executorService.shutdown();
  21.                 while (!executorService.isTerminated()) {
  22.                         Thread.sleep(500);
  23.                 }
  24.                 System.out.println(zeroTotop);
  25.         }
  26.  
  27.         private void incrementAllTheWay() {
  28.                 int obfusticatedIncremental = zeroToTop;
  29.                 obfusticatedIncremental = obfusticatedIncremental + 1;
  30.                 zeroToTop = obfusticatedIncremental;
  31.         }
  32.  
  33.         /**
  34.          * @param args
  35.          */
  36.         public static void main(String[] args) throws InterruptedException {
  37.                 new TheLoopTest();
  38.         }
  39.  
  40. }
  41.  
Aug 21 '09 #1
4 2279
JosAH
11,448 Recognized Expert MVP
Can you tells us what you think this thing is supposed to do and what it actually does? This is not a guessing forum.

kind regards,

Jos
Aug 21 '09 #2
wordone
9 New Member
Still not working, any other suggestions?
Aug 27 '09 #3
JosAH
11,448 Recognized Expert MVP
@wordone
Please answer my question (see my previous reply). If not I'm out of here; this is not a guessing forum.

kind regards,

Jos
Aug 27 '09 #4
wordone
9 New Member
It's about a race condition and it should print out '1000' every time but it doesn't always. I tried to change Executors.newFi xedThreadPool(1 00); to match the Thread.sleep(50 0); so the times are the same but that didn't seem to fix it.
Sep 15 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3475
by: Colin Steadman | last post by:
I have created a function to kill all session variables that aren't in a safe list. This is the function - Sub PurgeSessionVariables For Each Item In Session.Contents Select Case Trim(Item) Case "Authenticated" Case "CI_CODE" Case "organisation_description" Case "location_description"
2
7812
by: Jim | last post by:
Im getting way too many rows retured..what its trying to do is insert a 0 for revenue for months 7 - 12 (aka July through December) for each of these cost centers for each payor type..Im getting a lot of repeats and the concatenation field date always comes back as January 2003 instead of the month and date its supposed to --Fiscal Year
2
11437
by: Rasmus Grøndahl Olsen | last post by:
I have tried to write a wait function but it seems like it will not brake the while loop. I tried two different solutions. Can anyone tell me what I am doing wrong, and come with another suggestion? If I call the function like this: wait(500); it should wait 500ms right? function wait(time) { while(1){ setTimeout("break;",time); } }
1
2752
by: Matthew Wilson | last post by:
I need to write a function crc(msg, len) that gets a char array of length len and then calculates the crc32 for the code. I don't understand what's going wrong in the code I have. It goes bit-by-bit, XORing the most significant bit of the FCS with the msb of the character. All comments are welcome. Yes, this is for a school assignment. If I wanted to just grab some googled code I would; I want to actually understand what I am doing...
2
1140
by: Alfonso Morra | last post by:
I have a class that contains a nested class. The outer class is called outer, and the nested class is called inner. When I try to compile the following code, I get a number of errors. It is not obvious to me, where I'm going wrong (the compiler messages do not seem to make much sense). here is the code: outer class declared as ff in "outer.h":
20
2478
by: Snis Pilbor | last post by:
Whats the point of making functions which take arguments of a form like "const char *x"? It appears that this has no effect on the function actually working and doing its job, ie, if the function doesn't write to x, then it doesnt seem like the compiler could care less whether I specify the const part. Quite the opposite, if one uses const liberally and then later goes back and changes the functions, headaches will inevitably occur as...
3
3300
by: nahiyan13 | last post by:
hey there I have been given a problem where i have to write a function integerPower(base,exponent)that returns the value base^exponent(in words meaning if base is 2 and exponent is 3 then the program has to provide 2 to the power 3 which is 8 as an output)Our teacher asked to use a for loop to do this and by noother means.I have written the source code and the program runs but its giving me the wrong answers.Could sum 1 plz sort this out?Thanks...
5
2323
by: islayer | last post by:
can someone tell me what is wrong with the bold code? i am just learning perl. the program should create a perl file with a random name (5 letters, followed by a number), but the name is always just the number. whats wrong with my code? #!/usr/bin/perl use Fcntl; @array = (a..z); srand; foreach (1..5) { $name = int(rand scalar(@array));
5
1729
by: hiqu | last post by:
This issue is driving me nuts and not able to figure out whats wrong. I've this code in my firefox extension. Firefox always hangs and reports the script is busy. if I introduce a break statement in the for(;;) loop below, then no issue. Any help would be appreciated! function getStuff()
0
9575
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10320
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10308
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10073
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7609
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.