Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Multithreading

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone please help me in Question number 20 at the following link webpage .  How can there be more than two A's,B's,C's in the output ?
clip_image010_0001.jpg
[Thumbnail for clip_image010_0001.jpg]
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it this way... Two people (let's say you and me) decided to write "A", "B", and "C" on the same sheet of paper. Then we have to read that paper.

So, you will write "A", "B", "C", and then you will read what you wrote. Additionally, you will also read what I wrote so far. I will do the same, I will read what I wrote, plus I will read what you wrote so far.

Depending on the ordering, it should be possible to see 3 or 4 of each letter. Although, based on the timing of the threads, I would speculate that it would favor 3 rather than 4.

Henry  
 
neerav arora
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Henry
 
Sheriff
Posts: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what Henry said, you might see different number of repetitions of each letter. If I were to represent the execution of both threads simultaneously, you'll see how this is possible


In the above example, A is printed 4 times but B and C are printed 3 times each. This is a very simple example, you might get confusing results if both threads are executing the for loop at the same time [Edit: I miss the part that the printAll method is synchronized so this can't happen]. You can add the thread name to the output to understand it better
 
Ranch Hand
Posts: 531
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

neerav arora wrote:Could anyone please help me in Question number 20 at the following link webpage .  How can there be more than two A's,B's,C's in the output ?



Hi, Neeraj,

Any one thread will always write at most up to 2 As, 2 Bs, and 2Cs; however, there are two of them, so you may get up to 4 As, 4 Bs, and 4 Cs in some weakly determined order but i would surmise always starting with an A?

With best regards,

Anton.
 
It's a pleasure to see superheros taking such an interest in science. And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic