0

I am a beginner and do not natively speak English, so please bear with me and my English.

I am trying to understand for loops in threads. I am having a problem with threads here. These threads represent methods that take money from a account and the other thread adds money to that account.

You start off with a 1000 euro's on that account.

The problem is that i don't understand why there has to be a for loop with these numbers. I get the i = 0; and i ++, but why the i <1200000?

What am i achieving with this i < 1200000;?

Here are my threads:

The first one takes money from the account.

public class AfThread extends Thread {
private Rekening deRekening;
public AfThread(Rekening r) {
 deRekening = r;
 }
public void run() {

for (int i = 0 ;i < 1200000 ;i++ ) {
  deRekening.neemOp(600.00);
if (deRekening.getSaldo() < 0)
System.out.print("rood "); // wordt met wait() nooit uitgevoerd
 }
  }
   }

Adding thread:

public class BijThread extends Thread {
private Rekening deRekening;
public BijThread(Rekening r) {

deRekening = r;
}

public void run() {
for (int i = 0 ;i < 1200000 ;i++ ) {
deRekening.stort(600.00);
}
 }
  }
1
  • 1
    First, your english is excellent. Second, the i<120000 is the exit condition for your loop. That is to say, the loop will continue WHILE i < 120000, then exit. Commented Jun 5, 2015 at 18:53

4 Answers 4

2

Pardon my very very very stale Dutch...

The first thread will try to take/deduct (neemOp) 600 from the account. The for loop has it do so 1200000 times. If the account falls below zero it will print out "rood", which means Red (I think???), which I presume means the account is negative "has gone into the red" (in English jargon).

The second thread will add/deposit (stort) 600 to the account. The for loop has it do so 1200000 times.

Why? A normal customer isn't going to the ATM 1200000 times. My guess is that these are part of some test. You are simulating a lot of independent deposits and deductions. Consider if you have 1000s of customers. Once in a while a customer might be withdrawing cash at the same exact time that a deposit (say, an automatic payment) is made. In the software program, the deposit is likely made on a different Thread than the withdraw. You want to be absolutely sure that this doesn't cause a problem, or you will have a lot of angry customers!

This test isn't perfect, but by mixing up millions of deposits and withdraws on two separate threads it attempts to verify that nothing strange happens. At the end of the test the account should end up with the same amount of money as it started.

alstublieft

Sign up to request clarification or add additional context in comments.

Comments

1

the i<1200000 is the upperbound condition for your loop. There is no explicit reason why it has to be 1200000 here. I'm guessing that the reason for the number is contextual as the program will run just fine with a different upperbound. It just won't run as long. Hope this helps.

Comments

1

The i < 1200000 is the condition for your loop. It will only run while i is less than the number provided.

In other words, what you are "achieving" is "setting a limit on your for loop so it doesn't go forever"

8 Comments

But why do i need it to be that? It that a random number?
It's just a condition for how many times you want the loop to run @user3599415 You can make it whatever number you want.
So i can assume that it's like a number that shows me how long i want the loop to run. The larger the number, the longer it will run?
The larger the number the more times it will run. How long it runs for depends on what you're doing inside the loop, but in general theory, technically speaking, yes, the larger the number the longer it'll run.
@user3599415 technically yes but that's not how you should use it. Don't think of it as how LONG it will run, but rather that is your condition as to what will make it keep going. A loop from 0 to 200000 for example will execute 200000 times. On my computer that could take 2 seconds, on yours it could take 2 minutes - speed is not a good benchmark for a loop unless you are specifically doing that for some reason.
|
0

Sorry for bad English but i will try to explain.
If you got this loop,

for (int i =0 ; i<1200000 ; i++)

int i = 0; Defines the variable i to 0
i < 120000 Sais for i is smaller than 120000
i++ Sais after the loop count i +1

So in this loop i counts from 0 to 1200000.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.