Programming Thread

Viewing single post

Started by the-pi-guy, Mar 13, 2016, 10:39 PM

previous topic - next topic

the-pi-guy

Someone actually made a huge vocab list from this other textbook I have.  Which has ~7200 words.  This is going to be huge.  :o

I Learned ++int is not thread safe. Seems I had two different threads doing ++int and --int on the same value at the exact same time and only one or the other would be applied.

Explains why the glitch I was noticing was so rare.
In my second programming class, there was a little bit about multithreading. 
We had to use "synchronized", a keyword in Java.  Otherwise if we assigned one thread to add 2500 to a number, and another thread to add 2500 to the same number, it usually wouldn't quite make it up to 5000. 
Both threads would access the number, and change it at the same time.  Which would cause it to only go up by 1 instead of 2. 

"synchronized" made sure that the other thread would wait for the thread to finish accessing it. 

Although it was funny, because when the teacher tried demonstrating it, without the keyword (so he was expecting something like 4800 something.)  It actually got really lucky and made it all the way to 5000, 2 out of the first 5 times.  Only 2 times it actually worked.