0

We have 2 Threads in Java:

Thread 1:

…
public void run()
{
 while (Share.COUNTER<8)
    Share.COUNTER++;
}
 … 

Thread 2:

…
 public void run()
 {
  while (Share.COUNTER>-7)
   Share.COUNTER--;
  }
  … 

The question is: Which thread is going to be terminated by the while loop at first? The second question is: Is there a guarantee that the threads will terminate the run methods? Could you help me answering this questions since threads in Java is new to me.

Blnpwr
  • 1,793
  • 4
  • 22
  • 43
  • 2
    If there is no synchronisation around `Share.COUNTER`, then the behaviour is indeterminate. – Oliver Charlesworth May 02 '16 at 13:14
  • 2
    First of all, you'd probably get a concurrent access modification, as you could be accessing the same property at the same time. To answer your question, you have no guarantee which will finish first. If you were to run it, at times Thread 1 would be first, others Thread 2 would be first. It's a game of chance – Draken May 02 '16 at 13:14
  • It depends on which thread will be favoured by the system. Which thread will terminate first is unpredictable and there is not warranty that one of them will end (and consequently the other one). – Massimo May 02 '16 at 13:15
  • There is no guarantee of which Thread is going to end first. This is a basic question. You can find lots of info about it in internet. And yes, if the thread is started, it will terminate the run methods – BigBang May 02 '16 at 13:16
  • But what about the advantage of threads? Isn't it an advantage that threads can be run parallel ? So why can't thread 1 run while thread 2 is running, too? – Blnpwr May 02 '16 at 13:22
  • This is what's called a [Race condition](https://en.wikipedia.org/wiki/Race_condition). – Jorn Vernee May 02 '16 at 13:23
  • If you are using a synchronized block in your object then the first thread is serviced then the second. [Here is an example](http://www.javatpoint.com/synchronized-block-example) – mattyman May 02 '16 at 13:25
  • If Thread1 runs on one core and Thread2 runs on other, then both would terminate. This is because cores have their own cache and Thread1's local COUNTER would not be in sync with Thread2's COUNTER. But that's a wrong use case. – Yogesh Patil May 02 '16 at 13:27

1 Answers1

1

Which thread is going to be terminated by the while loop at first?

Most likely which ever thread starts first. A thread takes time to start and one thread could count to one million in the time it takes the other one to start.

Is there a guarantee that the threads will terminate the run methods?

Both threads should terminate almost immediately.

can't thread 1 run while thread 2 is running, too?

They can but they won't start at precisely the same time and since counting to 8 takes almost no time at all (it could be a fraction of a microsecond), it will terminate pretty quickly.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks, there are a lot of different answers, as you can see in the comments, so I am a bit unsure what is right and what not.. – Blnpwr May 02 '16 at 13:31
  • 1
    @Blnpwr all the comments appear to be saying the same thing to me. – Peter Lawrey May 02 '16 at 13:34
  • The problem I don't get is following: Both of the threads are using the same counter. So what about the synchro? If thread 1 increments the counter while thread 2 is decrementing the counter. That is what makes me a bit think about it. And there is no synchro in this example. Therefore, it is hard to me, to understand that they terminate.. – Blnpwr May 02 '16 at 13:38
  • If you assume that you have started both threads at the same time and are correctly accessing the property, then the threads will run and terminate. Which will end first is undefined, you have no concrete way to say which thread will have more CPU time, allowing it to finish first. So the threads will finish, but it is never guaranteed which will be first – Draken May 02 '16 at 13:43
  • But then, let's assume thread 1 is starting first, so thread 2 has to wait, until thread 1 finishes? But what about the parallelism of threads? In your comment, it seems, they don't run simultaneously, they run sequently. If they run simultaneously, they should not be finished, because they are accessing simultaneously the same counter, one is incrementing, second is decrementing. – Blnpwr May 02 '16 at 13:46
  • @Blnpwr the lack of synchronization/thread safety makes the problem easier. Each thread will run largely as if it was the only thread running. – Peter Lawrey May 02 '16 at 17:13
  • @Blnpwr nothing makes one thread wait for anything. There is no blocking operations. – Peter Lawrey May 02 '16 at 17:14
  • @Blnpwr they run sequentially because they don't run long enough to be running at the same time. It's like saying two people run a mile but are born in different centuries, you would expect one to finish before the other starts, though there is nothing to prevent this happening. It highly unlikely they will be running the same mile at the same time. – Peter Lawrey May 02 '16 at 17:16