Problems with threads

Programs with multiple threads can have problems, like the threads of a story in which if proper synchronization does not occur, then things go wrong. What if our soldier went into battle before the battle or war even existed? Weird.

What if we have a variable, int x, that represents a key piece of data that three threads of our program use. What happens if one thread gets slightly ahead of itself and makes the data "wrong" for the other two? This problem is the problem of correctness caused by multiple threads racing to completion obliviously—because after all, they are just dumb code.

The problem of correctness can be solved by close oversight of the threads and locking, locking meaning temporarily preventing execution in ...

Get Android Programming for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.