473,503 Members | 1,700 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 2266
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.newFixedThreadPool(100); to match the Thread.sleep(500); 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
3454
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)...
2
7793
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...
2
11415
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...
1
2730
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...
2
1119
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...
20
2412
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...
3
3268
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...
5
2299
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...
5
1694
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...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
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...
0
7328
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...
1
6988
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...
0
7456
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...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1510
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 ...

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.